473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Rijndael Encryption

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
0 1368

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mantorok | last post by:
Hi I have a couple of encryption methods but when I call decrypt I get the string back but with a load \0 escape characters on the end? Any idea why? It is actually causing problems in some...
5
by: ~~~ .NET Ed ~~~ | last post by:
Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all...
0
by: Jens Müller | last post by:
Hello, I try to program a Rijndael encryption in Windows which has to be compatible with php. In php I use the code below to encrypt with a 256 Bit Key and a 256 Bit block cipher. My windows...
1
by: RJ | last post by:
The 2.0 Framework SDK only has examples where the Rijndael class instance provides the key. I've tried setting my own key value, making sure my key length matches the KeySize property of my...
4
by: Sylvie | last post by:
http://www.obviex.com/samples/Encryption.aspx According to this link, I am using Rijndael Encryption & Decryption Algorithms, But I want my encrypted strings just CAPS string and just...
10
by: Iwan Budihalim | last post by:
Who can help? I'm trying to implement an encrypted (plain text) communication between a Delphi application and an ASP.NET. My choice is AES/rijndael-128. For both sides, i use standard modules: ...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
3
by: KBS Developer | last post by:
Hi, I can encrypt without any problem but while decrypting I got junk. I've read the other thread about getting junk but that is not my case. Here is the sample code: private Rijndael...
3
dreamfalcon
by: dreamfalcon | last post by:
Hi! I'm using this script in VB.NET to encrypt in 128 Bits using Rijndael encryption: http://www.freevbcode.com/ShowCode.Asp?ID=4520 And need something in PHP to decrypt the strings that are...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.