Error on Rijndael if Password is incorrect.
From: Kelly Elias (Elias_at_discussions.microsoft.com)
Date: 07/28/04
- Next message: Dave: "Re: Query AD using Integrated Authentication?"
- Previous message: Raterus: "Re: accessing mapped drives in ASP.NET application"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Wed, 28 Jul 2004 09:03:07 -0700
I'm using the RijndaelManaged class to encrypt some data, and do decription. it works all works fine if I specify my password correctly. If I enter the wrong password the key is obviously incorrect and I get an error on the line "byte[] b = ms.ToArray();" shown below. The error is:
'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Additional information: PKCS7 padding is invalid and cannot be removed.
I even tried to wrap the line in a try...catch and it refuses to be caught. (I have no idea why).
Obviously there should be a better method for me to determine if my passPhrase passed into the "Go" procedure is correct. Can anyone tell me how? Also anyone have an idea why I'm getting the error in the code below and cannot trap it?
private byte[] _keyBytes;
private byte[] _IVBytes;
public string Go(string passPhrase)
{
byte[] saltValueBytes = null;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(passPhrase,saltValueBytes,"SHA1",5);
_keyBytes = pdb.GetBytes(32);
_IVBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(IV);
return Transform(encryptedData,true);
}
private string Transform(string data, bool decrypt)
{
RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
rj.Mode= CipherMode.CBC;
ICryptoTransform trans;
if (decrypt)
{
trans= rj.CreateDecryptor(_keyBytes, _IVBytes);
}
else
{
trans= rj.CreateEncryptor(_keyBytes, _IVBytes);
}
byte[] byteData = Convert.FromBase64String(data);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write);
cs.Write(byteData, 0, byteData.Length);
cs.FlushFinalBlock();
string retval;
try
{
byte[] b = ms.ToArray();
retval= Convert.ToBase64String(b);
}
catch(System.Security.Cryptography.CryptographicException ex)
{
retval="";
}
return retval;
}
- Next message: Dave: "Re: Query AD using Integrated Authentication?"
- Previous message: Raterus: "Re: accessing mapped drives in ASP.NET application"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|