Home All Groups Group Topic Archive Search About

Cryptographic Streaming

Author
13 Feb 2009 12:49 AM
Dmitry Nogin
Hi,

What 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

Author
13 Feb 2009 3:24 AM
Dmitry Nogin
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...
  Hi,

  What 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