AES Encryption

AES Lib is taken from :

http://msdn.microsoft.com/msdnmag/code/?url=http://msdn.microsoft.com/msdnmag/issues/03/11/AES/Default.aspx

using this lib, it gives junk characters for larger texts as the encryption key size (128,192,256) increased.

The Encrypt and Decrypt Code is as follows:

textBox1 - TEXt you input in.
textBox2 - Encrypted text.
textBox2 - Decrypted text.

void Encrypt()
{
if (radioButton1.Checked)
keysize = Aes.KeySize.Bits128;
else if (radioButton2.Checked)
keysize = Aes.KeySize.Bits192;
else
keysize = Aes.KeySize.Bits256;

byte[] plainText = new byte[16];
byte[] cipherText = new byte[16];

int i = 0; string sPart = "";
string sText = textBox1.Text.Trim();
int nMod = sText.Length % 8;
if (nMod != 0) {
int nQuotient = sText.Length / 8;
int nTobePadded = 8 * (nQuotient + 1);
sText = sText.PadRight(nTobePadded , ' ');
}
for (i = 0; i <= (sText.Length-8); i = i + 8)
{
sPart = sText.Substring(i, 8);
plainText = Encoding.Unicode.GetBytes(sPart);
AesLib.Aes a = new Aes(keysize, new byte[16]);
a.Cipher(plainText, cipherText);
textBox2.Text = textBox2.Text + Encoding.Unicode.GetString(cipherText);
}
}

void Decrypt()
{
if (radioButton1.Checked)
keysize = Aes.KeySize.Bits128;
else if (radioButton2.Checked)
keysize = Aes.KeySize.Bits192;
else
keysize = Aes.KeySize.Bits256;
byte[] cipherText = new byte[16];
byte[] decipheredText = new byte[16];

int i = 0; string sPart = "";
for (i = 0; i <= (textBox2.Text.Length - 8); i = i + 8)
{
sPart = textBox2.Text.Substring(i, 8);
cipherText = Encoding.Unicode.GetBytes(sPart);
AesLib.Aes a = new Aes(keysize, new byte[16]);
a.InvCipher(cipherText, decipheredText);
textBox3.Text = textBox3.Text + Encoding.Unicode.GetString(decipheredText); ;
}
}

Please See the attachment for the junk it gives for larger texts! Any help would be appreciated!

PLEASE HELP!
[2411 byte] By [Charu0306] at [2007-11-20 11:49:35]
# 1 Re: AES Encryption
I see dglienna had replied but cant see his reply? Deleted? Hope I get an answer for this silly problem.
Charu0306 at 2007-11-9 11:37:10 >
# 2 Re: AES Encryption
Sorry, I found a VB.Net sample, but didn't realize it was the wrong forum.

I see that it didn't loop thru each character to encrypt. That seems like your problem. Just encrypt the whole textbox at once. Then, decrypt the same way.
dglienna at 2007-11-9 11:38:11 >
# 3 Re: AES Encryption
Iam sorry, it takes only 8 characters at a time. Thats why the padding, it encrypts correct I think :confused:

You can see the code online in the MSDN link above. Thanks :)
Charu0306 at 2007-11-9 11:39:10 >
# 4 Re: AES Encryption
That's fine, even if its a VB.NET code, can you give me pointers?
Charu0306 at 2007-11-9 11:40:05 >
# 5 Re: AES Encryption
Found one in C#

http://www.codeproject.com/useritems/Simple_Cryptographer.asp
dglienna at 2007-11-9 11:41:15 >