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 ICryptoTransfor m = cipher.CreateEn cryptor(key, iv)
Dim data As Byte() = Encoding.Unicod e.GetBytes(plai nText)
Return Convert.ToBase6 4String(encrypt or.TransformFin alBlock(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 ICryptoTransfor m = cipher.CreateDe cryptor(key, iv)
Dim data As Byte() = Convert.FromBas e64String(encry ptedText)
Return Encoding.Unicod e.GetString(dec ryptor.Transfor mFinalBlock(dat a, 0,
data.Length))
End Function
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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(plainTe xt, key, iv)
MsgBox(encrypte dText)
plainText = ""
plainText = Decrypt(encrypt edText, key, iv)
MsgBox(plainTex t)
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]
"larry mckay" <la***@larrymck ay.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi,
Does anyone have any simple text string encryption routines that are
easy to implement? I'm trying to prevent users and system
administrators from figuring out how I implement things.
thanks
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!