CryptoStream.Read Method error when decrypting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    CryptoStream.Read Method error when decrypting

    Hi,
    I am developing a ActiveX Control which will be used in a web page.
    The control will encrypt some value then decrypt it when the web page opens
    next time.
    I tested the control in a windows application and it works fine and no error
    jumps.
    However, when I tried to use it in a web page, problems came.
    There is no problem for encrypting but the decrypting can't be finished.
    The error jumped from the line below

    csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);

    and the error message reads as below:
    PKCS7 padding is invalid and cannot be removed.

    I tested again in the windows application but I couldn't trigger the error.
    Does any one know why this CryptoStream.Re ad Method behaves differently
    in a web browser?


    Please see code details below.

    protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
    {
    ASCIIEncoding textConverter= new ASCIIEncoding() ;
    RijndaelManaged myRijndael=new RijndaelManaged ();
    byte[] fromEncrypt;
    byte[] encrypted;
    string decryptedStr;

    //Get a decryptor that uses the same key and IV as the encryptor.
    ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);

    encrypted=Conve rt.FromBase64St ring(Ink64Strin g);

    MemoryStream msDecrypt = new MemoryStream(en crypted);
    CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
    CryptoStreamMod e.Read);

    fromEncrypt = new byte[encrypted.Lengt h];
    try
    {
    //problem is here!!
    csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
    }
    catch (System.Excepti on exp)
    {
    MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
    return String.Empty;
    }
    decryptedStr=te xtConverter.Get String(fromEncr ypt);

    ///remove \0 or null reference from the end of the string.
    decryptedStr=de cryptedStr.Trim End(Char.Parse( "\0"));
    //return the decrypted string.
    return decryptedStr;
    }
  • James

    #2
    RE: CryptoStream.Re ad Method error when decrypting

    I found the problem.
    I passed wrong key and IV which caused an error.


    "James" wrote:
    [color=blue]
    > Hi,
    > I am developing a ActiveX Control which will be used in a web page.
    > The control will encrypt some value then decrypt it when the web page opens
    > next time.
    > I tested the control in a windows application and it works fine and no error
    > jumps.
    > However, when I tried to use it in a web page, problems came.
    > There is no problem for encrypting but the decrypting can't be finished.
    > The error jumped from the line below
    >
    > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
    >
    > and the error message reads as below:
    > PKCS7 padding is invalid and cannot be removed.
    >
    > I tested again in the windows application but I couldn't trigger the error.
    > Does any one know why this CryptoStream.Re ad Method behaves differently
    > in a web browser?
    >
    >
    > Please see code details below.
    >
    > protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
    > {
    > ASCIIEncoding textConverter= new ASCIIEncoding() ;
    > RijndaelManaged myRijndael=new RijndaelManaged ();
    > byte[] fromEncrypt;
    > byte[] encrypted;
    > string decryptedStr;
    >
    > //Get a decryptor that uses the same key and IV as the encryptor.
    > ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);
    >
    > encrypted=Conve rt.FromBase64St ring(Ink64Strin g);
    >
    > MemoryStream msDecrypt = new MemoryStream(en crypted);
    > CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
    > CryptoStreamMod e.Read);
    >
    > fromEncrypt = new byte[encrypted.Lengt h];
    > try
    > {
    > //problem is here!!
    > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
    > }
    > catch (System.Excepti on exp)
    > {
    > MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
    > return String.Empty;
    > }
    > decryptedStr=te xtConverter.Get String(fromEncr ypt);
    >
    > ///remove \0 or null reference from the end of the string.
    > decryptedStr=de cryptedStr.Trim End(Char.Parse( "\0"));
    > //return the decrypted string.
    > return decryptedStr;
    > }[/color]

    Comment

    • David Whitchurch-Bennett

      #3
      RE: CryptoStream.Re ad Method error when decrypting

      I'm not sure if you are having these problems, but I have solved a similar
      problem that has been troubling me all afternoon....

      If you are converting bytes to text using an ASCIIEncoder, or any other
      8-bit encoder, it will only convert using 8-bits, i.e. a byte value of 129
      will become 1!!

      I had this problem because the MSDN sample uses an ASCII encoder! How
      annoying!!!!! I hope this helps.

      "James" wrote:
      [color=blue]
      > I found the problem.
      > I passed wrong key and IV which caused an error.
      >
      >
      > "James" wrote:
      >[color=green]
      > > Hi,
      > > I am developing a ActiveX Control which will be used in a web page.
      > > The control will encrypt some value then decrypt it when the web page opens
      > > next time.
      > > I tested the control in a windows application and it works fine and no error
      > > jumps.
      > > However, when I tried to use it in a web page, problems came.
      > > There is no problem for encrypting but the decrypting can't be finished.
      > > The error jumped from the line below
      > >
      > > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
      > >
      > > and the error message reads as below:
      > > PKCS7 padding is invalid and cannot be removed.
      > >
      > > I tested again in the windows application but I couldn't trigger the error.
      > > Does any one know why this CryptoStream.Re ad Method behaves differently
      > > in a web browser?
      > >
      > >
      > > Please see code details below.
      > >
      > > protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
      > > {
      > > ASCIIEncoding textConverter= new ASCIIEncoding() ;
      > > RijndaelManaged myRijndael=new RijndaelManaged ();
      > > byte[] fromEncrypt;
      > > byte[] encrypted;
      > > string decryptedStr;
      > >
      > > //Get a decryptor that uses the same key and IV as the encryptor.
      > > ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);
      > >
      > > encrypted=Conve rt.FromBase64St ring(Ink64Strin g);
      > >
      > > MemoryStream msDecrypt = new MemoryStream(en crypted);
      > > CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
      > > CryptoStreamMod e.Read);
      > >
      > > fromEncrypt = new byte[encrypted.Lengt h];
      > > try
      > > {
      > > //problem is here!!
      > > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
      > > }
      > > catch (System.Excepti on exp)
      > > {
      > > MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
      > > return String.Empty;
      > > }
      > > decryptedStr=te xtConverter.Get String(fromEncr ypt);
      > >
      > > ///remove \0 or null reference from the end of the string.
      > > decryptedStr=de cryptedStr.Trim End(Char.Parse( "\0"));
      > > //return the decrypted string.
      > > return decryptedStr;
      > > }[/color][/color]

      Comment

      • David Whitchurch-Bennett

        #4
        RE: CryptoStream.Re ad Method error when decrypting

        I'm not sure if you are having these problems, but I have solved a similar
        problem that has been troubling me all afternoon....

        If you are converting bytes to text using an ASCIIEncoder, or any other
        8-bit encoder, it will only convert using 8-bits, i.e. a byte value of 129
        will become 1!!

        I had this problem because the MSDN sample uses an ASCII encoder! How
        annoying!!!!! I hope this helps.

        "James" wrote:
        [color=blue]
        > I found the problem.
        > I passed wrong key and IV which caused an error.
        >
        >
        > "James" wrote:
        >[color=green]
        > > Hi,
        > > I am developing a ActiveX Control which will be used in a web page.
        > > The control will encrypt some value then decrypt it when the web page opens
        > > next time.
        > > I tested the control in a windows application and it works fine and no error
        > > jumps.
        > > However, when I tried to use it in a web page, problems came.
        > > There is no problem for encrypting but the decrypting can't be finished.
        > > The error jumped from the line below
        > >
        > > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
        > >
        > > and the error message reads as below:
        > > PKCS7 padding is invalid and cannot be removed.
        > >
        > > I tested again in the windows application but I couldn't trigger the error.
        > > Does any one know why this CryptoStream.Re ad Method behaves differently
        > > in a web browser?
        > >
        > >
        > > Please see code details below.
        > >
        > > protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
        > > {
        > > ASCIIEncoding textConverter= new ASCIIEncoding() ;
        > > RijndaelManaged myRijndael=new RijndaelManaged ();
        > > byte[] fromEncrypt;
        > > byte[] encrypted;
        > > string decryptedStr;
        > >
        > > //Get a decryptor that uses the same key and IV as the encryptor.
        > > ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);
        > >
        > > encrypted=Conve rt.FromBase64St ring(Ink64Strin g);
        > >
        > > MemoryStream msDecrypt = new MemoryStream(en crypted);
        > > CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
        > > CryptoStreamMod e.Read);
        > >
        > > fromEncrypt = new byte[encrypted.Lengt h];
        > > try
        > > {
        > > //problem is here!!
        > > csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
        > > }
        > > catch (System.Excepti on exp)
        > > {
        > > MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
        > > return String.Empty;
        > > }
        > > decryptedStr=te xtConverter.Get String(fromEncr ypt);
        > >
        > > ///remove \0 or null reference from the end of the string.
        > > decryptedStr=de cryptedStr.Trim End(Char.Parse( "\0"));
        > > //return the decrypted string.
        > > return decryptedStr;
        > > }[/color][/color]

        Comment

        Working...