Solved

Encrypt/Decrypt in VS.NET 2005

Posted on 2006-11-07
2
1,905 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 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