Solved

Encrypt/Decrypt in VS.NET 2005

Posted on 2006-11-07
2
1,895 Views
Last Modified: 2008-07-29
Hi,

In VB.NET 2003, I have following code:
        Dim password As PasswordDeriveBytes
        password = New PasswordDeriveBytes(mstrPassPhrase, _
                                           saltValueBytes, _
                                           'MD5', _
                                           mintPasswordIterations)

        ' Use the password to generate pseudo-random bytes for the encryption
        ' key. Specify the size of the key in bytes (instead of bits).
        Dim keyBytes As Byte()
        keyBytes = password.GetBytes(CInt(mintKeySize / 8))

        ' Create uninitialized Rijndael encryption object.
        Dim symmetricKey As RijndaelManaged
        symmetricKey = New RijndaelManaged

=======================
But when I open this project in 2005, it shows that PasswordDeriveBytes is obselete. I tried to use the new method and wrote the code as follows. But where do I specify the algorithm like the did above?

The new code is:
 Dim password As Rfc2898DeriveBytes
        'Dim password As PasswordDeriveBytes
        password = New Rfc2898DeriveBytes(mstrPassPhrase, saltValueBytes, mintPasswordIterations)

If you see the above code, there is no parameter to specifiy 'MD5'.

Regards,
MI
0
Comment
Question by:vj_mi
2 Comments
 
LVL 10

Expert Comment

by:gangwisch
ID: 17891232
encryption:
       Dim fStream As FileStream = File.Open("encrypted.txt", FileMode.OpenOrCreate)
        Dim RijndaelAlg As Rijndael = Rijndael.Create
        Dim cStream As New CryptoStream(fStream, _
                                       RijndaelAlg.CreateEncryptor(RijndaelAlg.Key, RijndaelAlg.IV), _
                                       CryptoStreamMode.Write)
        Dim sWriter As New StreamWriter(cStream)

        sWriter.WriteLine("Text to encrypt")
        sWriter.Close()
        cStream.Close()
        fStream.Close()

decryption:
 Dim fStream As FileStream = File.Open("encrypted.txt", FileMode.OpenOrCreate)
        Dim RijndaelAlg As Rijndael = Rijndael.Create
        Dim cStream As New CryptoStream(fStream, _
                                        RijndaelAlg.CreateDecryptor(RijndaelAlg.Key, RijndaelAlg.IV), _
                                        CryptoStreamMode.Read)

        Dim sReader As New StreamReader(cStream)
        Dim plainText As String = sReader.ReadLine()

        sReader.Close()
        cStream.Close()
        fStream.Close()
0
 
LVL 5

Accepted Solution

by:
dynamicrevolutions earned 500 total points
ID: 17895356
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography

Public Class Encryption64
    Private Shared key() As Byte = {}
    Private shared IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Shared Function Decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        Try

            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider
            inputByteArray = Convert.FromBase64String(stringToDecrypt)
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

    Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String
        Try
            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

End Class
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question