The Only Problem with your code is you forgot to specify an IV for the encryption algo.
I just through this together you can use it if you want. I have to Add Encryption and Decryption for Files using crypto streams and such.
Code:Imports System.Security.Cryptography
Module Encryption
Public Function AESEncrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim AESEncryptor As ICryptoTransform = AES.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = AESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return EncryptedData
End Function
Public Function AESDecrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim AESDecryptor As ICryptoTransform = AES.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = AESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
End Function
Public Function RC2Encrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim RC2Encryptor As ICryptoTransform = RC2.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = RC2Encryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return EncryptedData
End Function
Public Function RC2Decrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim RC2Decryptor As ICryptoTransform = RC2.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = RC2Decryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
End Function
Public Function DESEncrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim DESEncryptor As ICryptoTransform = DES.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = DESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return EncryptedData
End Function
Public Function DESDecrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim DESDecryptor As ICryptoTransform = DES.CreateDecryptor
Dim Databuffer As Byte()
Dim DecryptedData As Byte()
Databuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = DESDecryptor.TransformFinalBlock(Databuffer, 0, Databuffer.Length)
Return DecryptedData
End Function
Public Function TripleDESEncrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim TripleDESEncryptor As ICryptoTransform = TripleDes.CreateEncryptor
Dim Databuffer As Byte()
Dim EncryptedData As Byte()
Databuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = TripleDESEncryptor.TransformFinalBlock(Databuffer, 0, Databuffer.Length)
Return EncryptedData
End Function
Public Function TripleDESDecrypt(ByVal Key As String, ByVal Data As String) As Byte()
On Error Resume Next
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim TripleDESDecryptor As ICryptoTransform = TripleDes.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = TripleDESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
End Function
Public Function ToBase64String(ByVal Bytes As Byte()) As String
Return Convert.ToBase64String(Bytes)
End Function
End Module