Error on Rijndael if Password is incorrect.

From: Kelly Elias (Elias_at_discussions.microsoft.com)
Date: 07/28/04


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



Relevant Pages

  • Re: Reflection in 1.1 and 2.0 - generic, fast copy delegate?
    ... Your delegate solution is is still significantly more efficient that reflection. ... Also you need permissions to emit code with the LCG version whereas using delegates does not appear to require permissions beyond those for public reflection and so you can have a fast solution even in low trust environments. ... private string _firstName; ...
    (microsoft.public.dotnet.framework.clr)
  • Re: EJB3 Newbie & Persistence
    ... private String name; ... CustomerCMP customer; ... public void setAddress{ ...
    (comp.lang.java.beans)
  • Re: Reflection in 1.1 and 2.0 - generic, fast copy delegate?
    ... private string _firstName; ... private int _age; ... I'm well aware of LCG, and in fact, I'm not even sure why you need it in 2.0. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: variable length fields for flexibility in subroutines
    ... private string _IntBlock; ... public string IBreturn ... public string IBstreet ...
    (comp.lang.cobol)
  • Re: Method.property string
    ... GetData.Col.RowOverload Col to set or get the ... Then would I instantiate the Col class from within the GetData Class to call ... private string name; ... public string GetName() ...
    (microsoft.public.dotnet.framework)