i need help with a cryptography exception!
-
I have this code for encryption-decryption some data, when i run it, it makes the exception: "Padding is invalid and cannot be removed" I did everything i found in google or forums but none helped me. I appreciate any ideas! Please help me! This is the Code:
using System; using System.IO; using System.Text; using System.Security.Cryptography; public sealed class Cryption { private RijndaelManaged Algorithm; private MemoryStream memStream; private ICryptoTransform EncryptorDecryptor; private CryptoStream crStream; private StreamWriter strWriter; private StreamReader strReader; private string m_key; private string m_iv; private byte[] key; private byte[] iv; private string pwd_str; private byte[] pwd_byte; public Cryption(string key_val, string iv_val) { key = new byte[32]; iv = new byte[32]; int i; m_key = key_val; m_iv = iv_val; for(i=0;i<m_key.length;i++)> { key[i] = Convert.ToByte(m_key[i]); } for(i=0;i<m_iv.length;i++)> { iv[i] = Convert.ToByte(m_iv[i]); } } public string Encrypt(string s) { Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; memStream = new MemoryStream(); EncryptorDecryptor = Algorithm.CreateEncryptor(key,iv); crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write); strWriter = new StreamWriter(crStream); strWriter.Write(s); strWriter.Flush(); crStream.FlushFinalBlock(); pwd_byte = new byte[memStream.Length]; memStream.Position = 0; memStream.Read(pwd_byte,0,(int)pwd_byte.Length); pwd_str= new UnicodeEncoding().GetString(pwd_byte); return pwd_str; } public string Decrypt(string s) { Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; MemoryStream memStream = new MemoryStream(new UnicodeEncoding().GetBytes(s)); ICryptoTransform EncryptorDecryptor = Algorithm.CreateDecryptor(key,iv); memStream.Position = 0; CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Read); strReader = new StreamReader(crStream); return strReader.ReadToEnd(); } }
-
I have this code for encryption-decryption some data, when i run it, it makes the exception: "Padding is invalid and cannot be removed" I did everything i found in google or forums but none helped me. I appreciate any ideas! Please help me! This is the Code:
using System; using System.IO; using System.Text; using System.Security.Cryptography; public sealed class Cryption { private RijndaelManaged Algorithm; private MemoryStream memStream; private ICryptoTransform EncryptorDecryptor; private CryptoStream crStream; private StreamWriter strWriter; private StreamReader strReader; private string m_key; private string m_iv; private byte[] key; private byte[] iv; private string pwd_str; private byte[] pwd_byte; public Cryption(string key_val, string iv_val) { key = new byte[32]; iv = new byte[32]; int i; m_key = key_val; m_iv = iv_val; for(i=0;i<m_key.length;i++)> { key[i] = Convert.ToByte(m_key[i]); } for(i=0;i<m_iv.length;i++)> { iv[i] = Convert.ToByte(m_iv[i]); } } public string Encrypt(string s) { Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; memStream = new MemoryStream(); EncryptorDecryptor = Algorithm.CreateEncryptor(key,iv); crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write); strWriter = new StreamWriter(crStream); strWriter.Write(s); strWriter.Flush(); crStream.FlushFinalBlock(); pwd_byte = new byte[memStream.Length]; memStream.Position = 0; memStream.Read(pwd_byte,0,(int)pwd_byte.Length); pwd_str= new UnicodeEncoding().GetString(pwd_byte); return pwd_str; } public string Decrypt(string s) { Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; MemoryStream memStream = new MemoryStream(new UnicodeEncoding().GetBytes(s)); ICryptoTransform EncryptorDecryptor = Algorithm.CreateDecryptor(key,iv); memStream.Position = 0; CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Read); strReader = new StreamReader(crStream); return strReader.ReadToEnd(); } }
Post the exception type and full stack trace. Also post any other details on the exception and any inner exceptions.
Simon
-
Post the exception type and full stack trace. Also post any other details on the exception and any inner exceptions.
Simon
here is the stack trace:
System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd() at Cryptography.Cryption.Decrypt(String s) in F:\Comp\Crypt\Cryption.cs:line 96 at CryptSample.Form1.btnDecrypt_Click(Object sender, EventArgs e) in F:\Comp\Crypt\Form1.cs:line 230
I think it's somehow about the streamreader/writer (Am I right?) but i don't know how to fix it!!! anyway THANK YOU for your attention. Roshanak -
here is the stack trace:
System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd() at Cryptography.Cryption.Decrypt(String s) in F:\Comp\Crypt\Cryption.cs:line 96 at CryptSample.Form1.btnDecrypt_Click(Object sender, EventArgs e) in F:\Comp\Crypt\Form1.cs:line 230
I think it's somehow about the streamreader/writer (Am I right?) but i don't know how to fix it!!! anyway THANK YOU for your attention. RoshanakThe 3 most likely reasons for this error: 1) You are using different padding schemes when encrypting and decrypting. 2) You are using different encoding formats for encrypting and decrypting. 3) When encrypting you haven't properly flushed the stream out to the file. Looking at your code it looks like you set the padding to pkcs7 when encrypting , but don't do the same when decrypting.
Simon
-
The 3 most likely reasons for this error: 1) You are using different padding schemes when encrypting and decrypting. 2) You are using different encoding formats for encrypting and decrypting. 3) When encrypting you haven't properly flushed the stream out to the file. Looking at your code it looks like you set the padding to pkcs7 when encrypting , but don't do the same when decrypting.
Simon
I set the padding to pkcs7 in both encryption and decryption and i build the project again but it still makes the exception. About encoding format, i'm using unicodeEncoding in both, you can see in encryption:
pwd_str= new UnicodeEncoding().GetString(pwd_byte); return pwd_str;
and in decryption:MemoryStream memStream = new MemoryStream(new UnicodeEncoding().GetBytes(s));
and for the streams:strWriter.Flush(); crStream.FlushFinalBlock();
i don't know how else i can flush them!!! Again THANKS Roshanak -
I set the padding to pkcs7 in both encryption and decryption and i build the project again but it still makes the exception. About encoding format, i'm using unicodeEncoding in both, you can see in encryption:
pwd_str= new UnicodeEncoding().GetString(pwd_byte); return pwd_str;
and in decryption:MemoryStream memStream = new MemoryStream(new UnicodeEncoding().GetBytes(s));
and for the streams:strWriter.Flush(); crStream.FlushFinalBlock();
i don't know how else i can flush them!!! Again THANKS RoshanakI think the problem is the way you are reading data out of the memory stream. Use a stream reader instead of getting the bytes and doing unicode encoding. I suspect that is screwing things up. Then use a stream writer to put the string back into the memory stream when decrypting. (This works fine on my PC if I make that change)
Roshanakak wrote:
i don't know how else i can flush them!!!
You're not calling dispose no any of your streams. You should be using 'using' to properly dispose of the streams. Anything that has a dispose method should be created within a using block (or if not, make sure you call Dispose() in some other way - like in a finally block) Like this:
using (MemoryStream memStream = new MemoryStream())
{
using (ICryptoTransform EncryptorDecryptor = Algorithm.CreateEncryptor(key, iv))
{
using (CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write))
{
using (StreamWriter strWriter = new StreamWriter(crStream))
{
strWriter.Write(s);strWriter.Flush(); crStream.FlushFinalBlock(); } } }
}
Also, you're using a bunch of member variables on the class that your just overwriting when you decrypt. Get rid of these and make them local variables in the method. That will prevent any crossover or leak of setup from one method to the other.
Simon