Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > Cryptographic Streaming

Reply
Thread Tools

Cryptographic Streaming

 
 
Dmitry Nogin
Guest
Posts: n/a
 
      02-13-2009
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 / ;

rm.Key = rfc2898.GetBytes(rm.KeySize / ;



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 / ;

rm.Key = rfc2898.GetBytes(rm.KeySize / ;



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

 
Reply With Quote
 
 
 
 
Dmitry Nogin
Guest
Posts: n/a
 
      02-13-2009
Sorry, it was very easy:

return ms.GetBuffer(); => return ms.ToArray();
"Dmitry Nogin" <(E-Mail Removed)> wrote in message news(E-Mail Removed)...
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 / ;

rm.Key = rfc2898.GetBytes(rm.KeySize / ;



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 / ;

rm.Key = rfc2898.GetBytes(rm.KeySize / ;



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cryptographic image for 2950/2955 - is it for free? Flemming Haurum Cisco 0 03-23-2006 08:55 AM
cryptographic ios 12.2.25 SEB3 d.reinhart@vedior.nl Cisco 0 08-25-2005 02:49 PM
CS1548: Cryptographic failure while signing assembly '....' An internal error occurred Gad Rosenthal ASP .Net 0 04-21-2004 06:09 PM
Can't find a cryptographic service provider (CSP) in this algorythm... Andrés Giraldo ASP .Net 0 07-03-2003 09:58 PM



Advertisments