Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Post Migrated Here from stackoverflow.com (revisions)
Source Link
crjunk
crjunk

Encrypt Using AES

I'm using Microsoft's [example][1] [1]: http://msdn.microsoft.com/en-us/library/ms172831%28v=vs.100%29.aspx for encrypting/decripting a string. In their example, they are using Tripple DES. I'm trying to convert their code to use AES.

The modified code, listed below, works. However, I am wondering if I need to change anything in the TruncateHash function or in the AES.Key and AES.IV value.

This is all new to me, so I am still learning.

Imports System.Security.Cryptography

Public NotInheritable Class AesCrypto

' Private TripleDes As New TripleDESCryptoServiceProvider
' Changed from Triple DES to AES 

Private AES As New AesCryptoServiceProvider

Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()
    ' Creates a byte array of a specified length from the hash of the specified key.

    Dim sha1 As New SHA1CryptoServiceProvider        

    ' Hash the key.
    Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
    Dim hash() As Byte = sha1.ComputeHash(keyBytes)

    ' Truncate or pad the hash.
    ReDim Preserve hash(length - 1)
    Return hash
End Function


Sub New(ByVal key As String)
    ' Initialize the crypto provider.
    AES.Key = TruncateHash(key, AES.KeySize \ 8)
    AES.IV = TruncateHash("", AES.BlockSize \ 8)
End Sub

Public Function EncryptData(ByVal plaintext As String) As String
    ' Encrypt the data.

    ' Convert the plaintext string to a byte array.
    Dim plaintextBytes() As Byte =
        System.Text.Encoding.Unicode.GetBytes(plaintext)

    ' Create the stream.
    Dim ms As New System.IO.MemoryStream
    ' Create the encoder to write to the stream.
    Dim encStream As New CryptoStream(ms,
        AES.CreateEncryptor(),
        System.Security.Cryptography.CryptoStreamMode.Write)

    ' Use the crypto stream to write the byte array to the stream.
    encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
    encStream.FlushFinalBlock()

    ' Convert the encrypted stream to a printable string.
    Return Convert.ToBase64String(ms.ToArray)
End Function

Public Function DecryptData(ByVal encryptedtext As String) As String
    'Decrypt the data.

    ' Convert the encrypted text string to a byte array.
    Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

    ' Create the stream.
    Dim ms As New System.IO.MemoryStream
    ' Create the decoder to write to the stream.
    Dim decStream As New CryptoStream(ms,
        AES.CreateDecryptor(),
        System.Security.Cryptography.CryptoStreamMode.Write)

    ' Use the crypto stream to write the byte array to the stream.
    decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
    decStream.FlushFinalBlock()

    ' Convert the plaintext stream to a string.
    Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function

End Class