|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
encryption probI have a problem with my decryption, the encryption seems to work fine but when I try to decrypt the encrypted string I get an exception "Padding is invalid and cannot be removed." thrown on sreader.ReadToEnd(); so could anyone tell me what I'm doing wrong? public class MyRijndael { private byte[] _iv; public MyRijndael(byte[] iv) { this._iv = iv; } public string Encrypt(string input, byte[] key) { MemoryStream output = new MemoryStream(); Rijndael rn = Rijndael.Create(); CryptoStream scrypt = new CryptoStream(output, rn.CreateEncryptor(key, this._iv), CryptoStreamMode.Write); scrypt.FlushFinalBlock(); StreamWriter swriter = new StreamWriter(scrypt); swriter.Write(input); swriter.Flush(); byte[] buffer = output.ToArray(); swriter.Close(); scrypt.Close(); output.Close(); return Convert.ToBase64String(buffer); } public string Decrypt(string input64, byte[] key) { MemoryStream input = new MemoryStream(Convert.FromBase64String(input64)); Rijndael rn = Rijndael.Create(); CryptoStream scrypt = new CryptoStream(input, rn.CreateDecryptor(key, this._iv), CryptoStreamMode.Read); StreamReader sreader = new StreamReader(scrypt); string res = sreader.ReadToEnd(); <-- error "Padding is invalid and cannot be removed." sreader.Close(); scrypt.Close(); input.Close(); return res; } }
Show quote
Hide quote
"Heron" <nospam@nospam.com> wrote in message Sure - you're calling FlushFinalBlock before you've written anything to the news:OpDPrFg9GHA.3344@TK2MSFTNGP03.phx.gbl... > I have a problem with my decryption, the encryption seems to work fine but > when I try to decrypt the encrypted string I get an exception "Padding is > invalid and cannot be removed." thrown on sreader.ReadToEnd(); so could > anyone tell me what I'm doing wrong? > > public class MyRijndael > { > private byte[] _iv; > public MyRijndael(byte[] iv) > { > this._iv = iv; > } > > public string Encrypt(string input, byte[] key) > { > MemoryStream output = new MemoryStream(); > > Rijndael rn = Rijndael.Create(); > > CryptoStream scrypt = new CryptoStream(output, > rn.CreateEncryptor(key, this._iv), CryptoStreamMode.Write); > scrypt.FlushFinalBlock(); > > StreamWriter swriter = new StreamWriter(scrypt); > swriter.Write(input); > swriter.Flush(); stream. You are supposed to write everything to the stream, then call FlushFinalBlock, then close the stream. Alun. ~~~~ -- Texas Imperial Software | Web: http://www.wftpd.com/ 23921 57th Ave SE | Blog: http://msmvps.com/alunj/ Woodinville WA 98072-8661 | WFTPD, WFTPD Pro are Windows FTP servers. Fax/Voice +1(425)807-1787 | Try our NEW client software, WFTPD Explorer.
Creating MSI for installing .NET security policies
ActiveDirectory group membership in offline profile SecurityException thrown when serializing custom exception class Question on the use of CryptoStream How to deploy a VS2005 VB app without signing the clickonce manifest and assy COM+ Security error Why am I not trusted? Seeking Advice on RSA Credentials Double Hop How do I determine if a windows identity is authenticated to the network domain |
|||||||||||||||||||||||