|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cryptographic StreamingWhat is wrong about the following simple encoding/decoding code snippet pair? I have a CryptographicException ("Padding is invalid and cannot be removed.") at the very end. class Program { static void Main(string[] args) { byte[] cipherText = Encript("Test message", "qwerty"); string plainText = Decript(cipherText, "qwerty"); } private static byte[] Encript(string plainText, string password) { byte[] salt = Encoding.ASCII.GetBytes("This is my salt"); Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, salt); RijndaelManaged rm = new RijndaelManaged(); rm.IV = rfc2898.GetBytes(rm.BlockSize / 8); rm.Key = rfc2898.GetBytes(rm.KeySize / 8); using (MemoryStream ms = new MemoryStream()) using (ICryptoTransform ct = rm.CreateEncryptor()) using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(plainText); sw.Flush(); cs.FlushFinalBlock(); return ms.GetBuffer(); } } private static string Decript(byte[] cipherText, string password) { byte[] salt = Encoding.ASCII.GetBytes("This is my salt"); Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, salt); RijndaelManaged rm = new RijndaelManaged(); rm.IV = rfc2898.GetBytes(rm.BlockSize / 8); rm.Key = rfc2898.GetBytes(rm.KeySize / 8); using (MemoryStream ms = new MemoryStream(cipherText)) using (ICryptoTransform ct = rm.CreateDecryptor()) using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read)) using(StreamReader sr = new StreamReader(cs)) { return sr.ReadToEnd(); // <- Exception } } } Thanks a lot, -- dmitry Sorry, it was very easy:
return ms.GetBuffer(); => return ms.ToArray(); "Dmitry Nogin" <dmitryno***@hotmail.com> wrote in message news:D3E617E1-1B90-4756-8727-1627625D0C6F@microsoft.com... What is wrong about the following simple encoding/decoding code snippet pair?Hi, I have a CryptographicException ("Padding is invalid and cannot be removed.") at the very end. class Program { static void Main(string[] args) { byte[] cipherText = Encript("Test message", "qwerty"); string plainText = Decript(cipherText, "qwerty"); } private static byte[] Encript(string plainText, string password) { byte[] salt = Encoding.ASCII.GetBytes("This is my salt"); Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, salt); RijndaelManaged rm = new RijndaelManaged(); rm.IV = rfc2898.GetBytes(rm.BlockSize / 8); rm.Key = rfc2898.GetBytes(rm.KeySize / 8); using (MemoryStream ms = new MemoryStream()) using (ICryptoTransform ct = rm.CreateEncryptor()) using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(plainText); sw.Flush(); cs.FlushFinalBlock(); return ms.GetBuffer(); } } private static string Decript(byte[] cipherText, string password) { byte[] salt = Encoding.ASCII.GetBytes("This is my salt"); Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, salt); RijndaelManaged rm = new RijndaelManaged(); rm.IV = rfc2898.GetBytes(rm.BlockSize / 8); rm.Key = rfc2898.GetBytes(rm.KeySize / 8); using (MemoryStream ms = new MemoryStream(cipherText)) using (ICryptoTransform ct = rm.CreateDecryptor()) using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read)) using(StreamReader sr = new StreamReader(cs)) { return sr.ReadToEnd(); // <- Exception } } } Thanks a lot, -- dmitry
interop & performance
Impersonation problem Problem using obfuscation Dotnet 2.0 PCKS CheckSignature Error problem impersonating when remoting Encrypting connection strings - Threat model - Best practices Problem authenticating against renamed Active Directory account Directory Security API is taking time.... How to encrypt XML with multiple certificates |
|||||||||||||||||||||||