First of all, and sorry Guy, I don't mean to pick on ya, I know you're
trying to help
but if data is worth encrypting, NEVER, EVER use
encoding and don't make up your own encryption schemes. There's a HUGE
security difference between encoding (simple shifting of text data in
predictable ways) and standard encryption.
Luckily, the .NET framework has a bunch of encryption stuff built in that
you can use.
Try this code out, and post any questions you may have about it (I wrote
this in a windows form):
Function Encrypt(ByVal plainText As String, ByVal key As Byte(), ByVal iv As
Byte()) As String
Dim cipher As New RijndaelManaged
Dim encryptor As ICryptoTransform = cipher.CreateEncryptor(key, iv)
Dim data As Byte() = Encoding.Unicode.GetBytes(plainText)
Return Convert.ToBase64String(encryptor.TransformFinalBlock(data, 0,
data.Length))
End Function
Function Decrypt(ByVal encryptedText As String, ByVal key As Byte(), ByVal
iv As Byte()) As String
Dim cipher As New RijndaelManaged
Dim decryptor As ICryptoTransform = cipher.CreateDecryptor(key, iv)
Dim data As Byte() = Convert.FromBase64String(encryptedText)
Return Encoding.Unicode.GetString(decryptor.TransformFinalBlock(data, 0,
data.Length))
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim key As Byte() = {170, 56, 39, 124, 31, 136, 211, 100, 180, 51, 111,
88, 217, 92, 214, 36, 164, 188, 71, 51, 36, 187, 195, 205, 87, 167, 81, 248,
173, 7, 194, 10}
Dim iv As Byte() = {33, 162, 253, 195, 255, 140, 120, 198, 25, 99, 222,
141, 182, 152, 94, 28}
Dim plainText As String = "This is a test"
Dim encryptedText As String
encryptedText = Encrypt(plainText, key, iv)
MsgBox(encryptedText)
plainText = ""
plainText = Decrypt(encryptedText, key, iv)
MsgBox(plainText)
End Sub
Of course, you'll want to create your own key and IV values. You must use
the same key (and IV) to decrypt a value as the key (and IV) that was used
to encrypt it. Other keys will not work, and will just cause an error, or
return garbage. If you use the Rijndael encryption algorithm (like i did
here), the key should be 32 bytes in length, and the IV 16 bytes.
-Rob Teixeira [MVP]