473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ 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.Len gth)
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.EventArg s) Handles btnEncryptText. Click

Try
Dim original As String = Me.txtBefore.Te xt
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.Cle ar()
Me.txtIV.Clear( )
Me.txtKey.Clear ()

'Create a new key and initialization vector.
myRijndael.Gene rateKey()
myRijndael.Gene rateIV()

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

Me.txtKey.Text = textConverter.G etString(mykey)
Me.txtIV.Text = textConverter.G etString(myIV)

'Get an encryptor.
Dim encryptor As ICryptoTransfor m =
myRijndael.Crea teEncryptor(myk ey, myIV)

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

'Convert the data to a byte array.
toEncrypt = textConverter.G etBytes(origina l)

'Write all data to the crypto stream and flush it.
csEncrypt.Write (toEncrypt, 0, toEncrypt.Lengt h)
csEncrypt.Flush FinalBlock()

'Get encrypted array of bytes.
encrypted = msEncrypt.ToArr ay()

Me.txtAfter.Tex t = textConverter.G etString(encryp ted)

Catch ex As Exception
MsgBox(ex.Messa ge)
End Try

End Sub

Private Sub btnDecryptText_ Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) 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.Cl ear()

'Create a new key and initialization vector.
myRijndael.Key = textConverter.G etBytes(Me.txtK ey.Text)
myRijndael.IV = textConverter.G etBytes(Me.txtI V.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 ICryptoTransfor m =
myRijndael.Crea teDecryptor(key , IV)

'Now decrypt the previously encrypted message using the
decryptor
' obtained in the above step.
encrypted = textConverter.G etBytes(Me.txtA fter.Text)
Dim msDecrypt As New MemoryStream(en crypted)
Dim csDecrypt As New CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read)

fromEncrypt = New Byte(encrypted. Length) {}

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

'Convert the byte array back into a string.
Me.txtBefore.Te xt = textConverter.G etString(fromEn crypt)

Catch ex As Exception
MsgBox(ex.Messa ge)
End Try

End Sub
May 9 '07 #1
0 1396

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

Similar topics

4
5540
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 places, here are my methods: public static string Encrypt(string input, byte key, byte iv)
5
4185
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 resembling the input file. Rijndael rijndaelAlg = Rijndael.Create(); rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES rijndaelAlg.Padding = PaddingMode.PKCS7;
0
2105
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 code has the same specs, but the outcome is different. So far I use no iv to facilitate.
1
1492
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 Rijndael instance. I always get "invalid key". Am I misunderstanding the concept - if I don't set and keep my own key, how will I be able to decrypt later? Any ideas or example would be much appreciated. RJ
4
4800
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 alphanumeric values ABC...Z and 123...90, no other chars I want, what should I do ? or what other algos I must use, Thanks
10
7831
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: Delphi: TDCP_rijndael Component (DCPcrypt Cryptographic Component Library v2) from cityinthesky (www.cityinthesky.com) ASP.NET : Standard library of Rijndael both sides are working, BUT they do different! The results in encryption
4
5705
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 = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256;
3
2572
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 GetKBSAlgorithm() { Rijndael rijndaelAlgorithm = Rijndael.Create(); rijndaelAlgorithm.IV = Convert.FromBase64String(tbIV.Text);
3
3642
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 generated by VB. I tried several functions that I found in the internet, but all with different results. Can anyone help me?
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.