By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Rijndael Encryption

P: n/a
I need to encrypt some strings in serveral programs I have. So I grabbed
some code from the MS help.
I wrote an app that would help me generate the encrypted strings. Its s form
with 4 text boxes

txtBefore - the original string
txtIV, txtKey - the Rijndael IV and Key
txtAfter - the encrypted string

For testing, I enter text into txtBefore, then press btnEncryptText
Then I press btnDecryptText to re generate txtBefore. Unfortunately I get an
error at
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
The message is:
"Padding is invalid and cannot be removed."

I'm new to this whole encryption thing, and can't figure out what I'm doing
wrong.
Is there some padding generated by the text box that I should be removing?

Thanks
Vayse

Private Sub btnEncryptText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEncryptText.Click

Try
Dim original As String = Me.txtBefore.Text
Dim textConverter As New ASCIIEncoding()
Dim myRijndael As New RijndaelManaged()
Dim encrypted() As Byte
Dim toEncrypt() As Byte
Dim mykey() As Byte
Dim myIV() As Byte

' Clear the text boxes
Me.txtAfter.Clear()
Me.txtIV.Clear()
Me.txtKey.Clear()

'Create a new key and initialization vector.
myRijndael.GenerateKey()
myRijndael.GenerateIV()

'Get the key and IV.
mykey = myRijndael.Key
myIV = myRijndael.IV

Me.txtKey.Text = textConverter.GetString(mykey)
Me.txtIV.Text = textConverter.GetString(myIV)

'Get an encryptor.
Dim encryptor As ICryptoTransform =
myRijndael.CreateEncryptor(mykey, myIV)

'Encrypt the data.
Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write)

'Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original)

'Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()

'Get encrypted array of bytes.
encrypted = msEncrypt.ToArray()

Me.txtAfter.Text = textConverter.GetString(encrypted)

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub btnDecryptText_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnDecryptText.Click

Try
Dim textConverter As New ASCIIEncoding()
Dim myRijndael As New RijndaelManaged()
Dim fromEncrypt() As Byte
Dim encrypted() As Byte
Dim key() As Byte
Dim IV() As Byte

Me.txtBefore.Clear()

'Create a new key and initialization vector.
myRijndael.Key = textConverter.GetBytes(Me.txtKey.Text)
myRijndael.IV = textConverter.GetBytes(Me.txtIV.Text)

'Get the key and IV.
key = myRijndael.Key
IV = myRijndael.IV

'Get a decryptor that uses the same key and IV as the encryptor.
Dim decryptor As ICryptoTransform =
myRijndael.CreateDecryptor(key, IV)

'Now decrypt the previously encrypted message using the
decryptor
' obtained in the above step.
encrypted = textConverter.GetBytes(Me.txtAfter.Text)
Dim msDecrypt As New MemoryStream(encrypted)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read)

fromEncrypt = New Byte(encrypted.Length) {}

'Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

'Convert the byte array back into a string.
Me.txtBefore.Text = textConverter.GetString(fromEncrypt)

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
May 9 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.