1

I have the following code for encryption and decryption. The problem is that at decryption besides the decrypted text i have some "aaaaa" after the text. why? need some help. THX!

public static byte[] Encrypt(byte[] PlainTextBytes, string key , string InitialVector) { try { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] KeyBytes = encoding.GetBytes(key); byte[] InitialVectorBytes = encoding.GetBytes(InitialVector); RijndaelManaged SymmetricKey = new RijndaelManaged(); ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes); MemoryStream MemStream = new MemoryStream(); CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write); CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); CryptoStream.FlushFinalBlock(); byte[] CipherTextBytes = MemStream.ToArray(); return CipherTextBytes; 

//decrytion

public static string Decrypt(byte[] PlainTextBytes1, string key, string InitialVector) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] KeyBytes = encoding.GetBytes(key); RijndaelManaged SymmetricKey = new RijndaelManaged(); byte[] InitialVectorBytes = Encoding.UTF8.GetBytes(InitialVector); ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes); MemoryStream MemStream1 = new MemoryStream(PlainTextBytes1); CryptoStream CryptoStream = new CryptoStream(MemStream1, Decryptor, CryptoStreamMode.Read); Byte[] pltxt = new byte[PlainTextBytes1.Length]; CryptoStream.Read(pltxt, 0, pltxt.Length); ASCIIEncoding textConverter = new ASCIIEncoding(); round = textConverter.GetString(pltxt); return round; } 

where am i wrong?

2
  • Are the byte[] the same sizes? Commented May 19, 2011 at 7:31
  • need some help..please give a solution! Commented May 19, 2011 at 8:05

1 Answer 1

5

In your decrypt function you have:

Byte[] pltxt = new byte[PlainTextBytes1.Length]; 

This is wrong because the cypher text is longer than the plain text, because it's padded to get a multiple of the block size.

 CryptoStream.Read(pltxt, 0, pltxt.Length); 

Read returns how many bytes were actually returned. It doesn't guarantee that it will return as many bytes as you requested.


And then there are multiple other defects/bad style things:

  1. The parameter passed into Decrypt is called PlainTextBytes1 it should be called cyphertext.
  2. The way you create the key/initvec bytes from the string. ASCII encoding is a bad choice here. ASCII can't represent any byte string. Perhaps you want to hex en/decode a string of twice the size of the key instead?
  3. ASCII en/decoding the plaintext will only work for ASCII characters and silently corrupt all other characters. Why don't you use UTF-8 instead?
  4. .net naming conventions say you should use lowercase names for parameters
Sign up to request clarification or add additional context in comments.

4 Comments

thx for the answer! can you please tell me how to correct the pltxt in order to obtain the correct result? plz:). thx. i know it's too much to ask for u to rewrite the code!
plus MemoryStream and CryptoStream both implement IDisposable and should therefore be wrapped in using statements
what is the solution to the padding lenght?
The msdn documentation has an example: msdn.microsoft.com/en-us/library/… - It's for encrypting/decrypting files but the same principle

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.