08-06-2010, 05:39 PM
Code:
Public Function encrypt(ByVal message As String) As String
Dim returnMessage As String
Dim Key As String = "PHDPqfwE3z25f2UYjwwfwg4XSqxvl8WYmy+2h8t6AUg="
Dim IV As String = "pd5mgMMfDI2Gxm/SKl5I8A=="
Dim Mode As CipherMode = CipherMode.CBC
Dim Padding As PaddingMode = PaddingMode.PKCS7
Dim cipherbytes() As Byte
Dim sa As SymmetricAlgorithm = Rijndael.Create()
sa.Key = Convert.FromBase64String(Key)
sa.IV = Convert.FromBase64String(IV)
sa.Mode = Mode
sa.Padding = Padding
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream _
(ms, sa.CreateEncryptor(), CryptoStreamMode.Write)
Dim plainbytes() As Byte = Encoding.ASCII.GetBytes(message)
cs.Write(plainbytes, 0, plainbytes.Length)
cs.Close()
cipherbytes = ms.ToArray()
ms.Close()
returnMessage = Convert.ToBase64String(cipherbytes)
Return returnMessage
End Function
Code:
Public Function decrypt(ByVal message As String) As String
Dim returnMessage As String
Dim Key As String = "PHDPqfwE3z25f2UYjwwfwg4XSqxvl8WYmy+2h8t6AUg="
Dim IV As String = "pd5mgMMfDI2Gxm/SKl5I8A=="
Dim Mode As CipherMode = CipherMode.CBC
Dim Padding As PaddingMode = PaddingMode.PKCS7
Dim cipherbytes() As Byte = ASCIIEncoding.ASCII.GetBytes(message)
cipherbytes = Convert.FromBase64String(message)
Dim sa As SymmetricAlgorithm = Rijndael.Create()
sa.Key = Convert.FromBase64String(Key)
sa.IV = Convert.FromBase64String(IV)
sa.Mode = Mode
sa.Padding = Padding
Dim ms As MemoryStream = New MemoryStream(cipherbytes)
Dim cs As CryptoStream = New CryptoStream _
(ms, sa.CreateDecryptor(), CryptoStreamMode.Read)
Dim plainbytes() As Byte = New Byte(cipherbytes.Length) {}
cs.Read(plainbytes, 0, plainbytes.Length)
cs.Close()
cipherbytes = ms.ToArray()
ms.Close()
returnMessage = Encoding.ASCII.GetString(plainbytes)
Return returnMessage
End Function
Ok I finally got this to work can someone PLEASE tell me if this would be a good en/decryption function, I really need feedback and ideas.
Thanks