Solved

Encrypt/Decrypt in VS.NET 2005

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now