Encryption Error "Length of the data to decrypt is invalid"

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

    Encryption Error "Length of the data to decrypt is invalid"

    Hello all,


    I am having a problem where I get an error message when I call
    FlushFinalBlock when decrypting my encrypted text. I am using the
    Rijndael algorithm.

    The error message is "Length of the data to decrypt is invalid" and
    occurs on the csDecrypt.Flush FinalBlock.

    Please find below the two routines I use to encrypt my text and decrypt
    the text. I have used the two routines in a very simple app that takes
    a string from a label and then calls EncryptString and displays it on
    another label. I then call DecryptString and pass it the contents of
    the encrypted label text property.

    The CryptKey is byte[32] and is set on creation of the object
    containing these methods.

    Could somebody please enlighten me as to what I am doing wrong!


    Thanks in advance

    Jimski



    public string EncryptString(s tring pText)
    {
    string strResult = "";

    try
    {
    // Create the rijndael encryptor
    ICryptoTransfor m encryptor =
    _RijndaelEncryp tor.CreateEncry ptor();

    // Encrypt the data to the memory stream
    MemoryStream msEncrypt = new MemoryStream(pT ext.Length);
    CryptoStream csEncrypt = new CryptoStream(ms Encrypt, encryptor,
    CryptoStreamMod e.Write);

    // Convert the string to a byte array.
    byte[] beforeEncrypt = Encoding.UTF8.G etBytes(pText);

    // Write all the data to the crypto stream and flush it to the
    MemoryStream.
    csEncrypt.Write (beforeEncrypt, 0, beforeEncrypt.L ength);
    csEncrypt.Flush FinalBlock();

    // Read encrypted array of bytes from the MemoryStream
    byte[] afterEncrypt = new byte[msEncrypt.Lengt h];
    msEncrypt.Posit ion = 0;
    msEncrypt.Read( afterEncrypt, 0, afterEncrypt.Le ngth);

    csEncrypt.Close ();

    // Returns the encrypted text as a string
    strResult = Encoding.UTF8.G etString(afterE ncrypt);
    }
    catch (Exception ex)
    {
    // An error occurred during encryption
    }
    return strResult;
    }


    public string DecryptString(s tring pEncryptedText)
    {
    string strResult = "";

    try
    {
    // Convert the string to a byte array.
    byte[] beforeDecrypt = Encoding.UTF8.G etBytes(pEncryp tedText);

    // Create the rijndael decryptor
    ICryptoTransfor m decryptor =
    _RijndaelEncryp tor.CreateDecry ptor();

    // decrypt the data to the memory stream
    MemoryStream msDecrypt = new
    MemoryStream(be foreDecrypt.Len gth);
    CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
    CryptoStreamMod e.Write);

    // Write all the data to the crypto stream and flush it.
    csDecrypt.Write (beforeDecrypt, 0, beforeDecrypt.L ength);
    // ERROR occurs in this next line.
    csDecrypt.Flush FinalBlock();

    // Read decrypted array of bytes from the MemoryStream
    byte[] afterDecrypt = new byte[msDecrypt.Lengt h];
    msDecrypt.Posit ion = 0;
    msDecrypt.Read( afterDecrypt, 0, afterDecrypt.Le ngth);

    csDecrypt.Close ();

    // Returns the decrypted text as a string
    strResult = Encoding.UTF8.G etString(afterD ecrypt);
    }
    catch (Exception ex)
    {
    // Show error - which in this case is "Length of the data to
    decrypt is invalid"
    }

    return strResult;
    }

  • DalePres

    #2
    Re: Encryption Error "Length of the data to decrypt is invalid"

    Here's a sample that can should help:



    DalePres
    MCAD, MCDBA, MCSE

    "Jimski" <jimski.brock@g mail.com> wrote in message
    news:1110818078 .312470.128830@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > Hello all,
    >
    >
    > I am having a problem where I get an error message when I call
    > FlushFinalBlock when decrypting my encrypted text. I am using the
    > Rijndael algorithm.
    >
    > The error message is "Length of the data to decrypt is invalid" and
    > occurs on the csDecrypt.Flush FinalBlock.
    >
    > Please find below the two routines I use to encrypt my text and decrypt
    > the text. I have used the two routines in a very simple app that takes
    > a string from a label and then calls EncryptString and displays it on
    > another label. I then call DecryptString and pass it the contents of
    > the encrypted label text property.
    >
    > The CryptKey is byte[32] and is set on creation of the object
    > containing these methods.
    >
    > Could somebody please enlighten me as to what I am doing wrong!
    >
    >
    > Thanks in advance
    >
    > Jimski
    >
    >
    >
    > public string EncryptString(s tring pText)
    > {
    > string strResult = "";
    >
    > try
    > {
    > // Create the rijndael encryptor
    > ICryptoTransfor m encryptor =
    > _RijndaelEncryp tor.CreateEncry ptor();
    >
    > // Encrypt the data to the memory stream
    > MemoryStream msEncrypt = new MemoryStream(pT ext.Length);
    > CryptoStream csEncrypt = new CryptoStream(ms Encrypt, encryptor,
    > CryptoStreamMod e.Write);
    >
    > // Convert the string to a byte array.
    > byte[] beforeEncrypt = Encoding.UTF8.G etBytes(pText);
    >
    > // Write all the data to the crypto stream and flush it to the
    > MemoryStream.
    > csEncrypt.Write (beforeEncrypt, 0, beforeEncrypt.L ength);
    > csEncrypt.Flush FinalBlock();
    >
    > // Read encrypted array of bytes from the MemoryStream
    > byte[] afterEncrypt = new byte[msEncrypt.Lengt h];
    > msEncrypt.Posit ion = 0;
    > msEncrypt.Read( afterEncrypt, 0, afterEncrypt.Le ngth);
    >
    > csEncrypt.Close ();
    >
    > // Returns the encrypted text as a string
    > strResult = Encoding.UTF8.G etString(afterE ncrypt);
    > }
    > catch (Exception ex)
    > {
    > // An error occurred during encryption
    > }
    > return strResult;
    > }
    >
    >
    > public string DecryptString(s tring pEncryptedText)
    > {
    > string strResult = "";
    >
    > try
    > {
    > // Convert the string to a byte array.
    > byte[] beforeDecrypt = Encoding.UTF8.G etBytes(pEncryp tedText);
    >
    > // Create the rijndael decryptor
    > ICryptoTransfor m decryptor =
    > _RijndaelEncryp tor.CreateDecry ptor();
    >
    > // decrypt the data to the memory stream
    > MemoryStream msDecrypt = new
    > MemoryStream(be foreDecrypt.Len gth);
    > CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
    > CryptoStreamMod e.Write);
    >
    > // Write all the data to the crypto stream and flush it.
    > csDecrypt.Write (beforeDecrypt, 0, beforeDecrypt.L ength);
    > // ERROR occurs in this next line.
    > csDecrypt.Flush FinalBlock();
    >
    > // Read decrypted array of bytes from the MemoryStream
    > byte[] afterDecrypt = new byte[msDecrypt.Lengt h];
    > msDecrypt.Posit ion = 0;
    > msDecrypt.Read( afterDecrypt, 0, afterDecrypt.Le ngth);
    >
    > csDecrypt.Close ();
    >
    > // Returns the decrypted text as a string
    > strResult = Encoding.UTF8.G etString(afterD ecrypt);
    > }
    > catch (Exception ex)
    > {
    > // Show error - which in this case is "Length of the data to
    > decrypt is invalid"
    > }
    >
    > return strResult;
    > }
    >[/color]


    Comment

    • Jimski

      #3
      Re: Encryption Error &quot;Length of the data to decrypt is invalid&quot;

      Thanks Dale,

      Your code identified an area that I was implementing wrong.

      In my decryption routine I should have populated the memory stream with
      the bytes and then created the CryptoStream. Writing them afterwards
      was causing problems.

      Cheers for you help.
      Jimski

      Comment

      • DalePres

        #4
        Re: Encryption Error &quot;Length of the data to decrypt is invalid&quot;

        I am glad it helped. That's what makes it worth the time to write it.

        DalePres

        "Jimski" <jimski.brock@g mail.com> wrote in message
        news:1110892354 .205938.309100@ l41g2000cwc.goo glegroups.com.. .[color=blue]
        > Thanks Dale,
        >
        > Your code identified an area that I was implementing wrong.
        >
        > In my decryption routine I should have populated the memory stream with
        > the bytes and then created the CryptoStream. Writing them afterwards
        > was causing problems.
        >
        > Cheers for you help.
        > Jimski
        >[/color]


        Comment

        Working...