Solved

Encrypt/Decrypt in VS.NET 2005

Posted on 2006-11-07
2
1,890 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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