Rijndael Decrypt returning escape characters at end of string

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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)

{

byte[] inputStringBytes = Encoding.ASCII.GetBytes(input);

byte[] outputBytes;

MemoryStream ms = new MemoryStream(inputStringBytes.Length);

RijndaelManaged rijndael = new RijndaelManaged();

ICryptoTransform rdTransform = rijndael.CreateEncryptor((byte[])key.Clone(),
(byte[])iv.Clone());

CryptoStream cs = new CryptoStream(ms, rdTransform, CryptoStreamMode.Write);

cs.Write(inputStringBytes, 0, inputStringBytes.Length);

cs.FlushFinalBlock();

outputBytes = ms.ToArray();

ms.Close();

cs.Close();

rdTransform.Dispose();

rijndael.Clear();

return Convert.ToBase64String(outputBytes);

}



public static string Decrypt(string input, byte[] key, byte[] iv)

{

byte[] inputStringBytes = Convert.FromBase64String(input);

byte[] outputTextBytes = new byte[inputStringBytes.Length];

RijndaelManaged rijndael = new RijndaelManaged();

MemoryStream ms = new MemoryStream(inputStringBytes);

ICryptoTransform rdTransform = rijndael.CreateDecryptor((byte[])key.Clone(),
(byte[])iv.Clone());

CryptoStream cs = new CryptoStream(ms, rdTransform, CryptoStreamMode.Read);

cs.Read(outputTextBytes, 0, outputTextBytes.Length);

ms.Close();

cs.Close();

rdTransform.Dispose();

rijndael.Clear();

return Encoding.ASCII.GetString(outputTextBytes);

}



Thanks

Kev


.



Relevant Pages