Length of the data to decrypt is invalid.

Discussion in 'Microsoft C# .NET' started by gane kol, Jun 17, 2005.

  1. gane kol

    gane kol Guest

    Hi

    I am using DES algorithm. I am getting an error message in a few cases of
    the querystring

    Error Message : Length of the data to decrypt is invalid.
    Error Method : System.String Decrypting(System.String)
    Error Line: strDecrpt = sr.ReadToEnd();

    QueryString Data:
    ---------------------
    id1: IVUTMOv8Hno0eG0=

    But it works for

    id1: 02FPObSRAf6bARvt5FM3wA==

    Any ideas appreciated,

    Thanks
    Gane

    code
    ------
    public string Encrypting(string Source)

    {

    if (System.Configuration.ConfigurationSettings.AppSettings["ENV"] != "DEV")

    {

    string Key;

    string strEncrpt = null;

    if ((Source != "") && (Source != null))

    {

    int i = 0;

    Key = CRYPTO_KEY;

    try

    {

    byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    byte[] bytKey = GetLegalKey(Key);

    mobjCryptoService.Key = bytKey;

    mobjCryptoService.IV = bytKey;

    ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

    CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

    cs.Write(bytIn, 0, bytIn.Length);

    cs.FlushFinalBlock();

    byte[] bytOut = ms.GetBuffer();

    for (i = 8; i <= bytOut.Length - 1; i++)

    {

    if ((bytOut == 0))

    {

    goto exitForStatement0;

    }

    }

    exitForStatement0: ;

    strEncrpt = System.Convert.ToBase64String(bytOut, 0, i);

    return strEncrpt;

    }

    catch (Exception Exp)

    {

    throw Exp;

    }

    }

    else

    {return strEncrpt; }

    }

    return Source;

    }


    public string Decrypting(string Source)

    {

    if (System.Configuration.ConfigurationSettings.AppSettings["ENV"] != "DEV")

    {

    string strDecrpt = null;

    if ((Source != "") && (Source != null))

    {

    string Key;

    Key = CRYPTO_KEY;

    Source = Source.Replace(" ","+");


    char padChar = Convert.ToChar("=");

    int length = Source.Length;

    if (length % 4 > 0)

    {

    Source = Source.PadRight(length + (4-(length % 4)),padChar);

    }

    try

    {

    byte[] bytIn = System.Convert.FromBase64String(Source);

    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0,
    bytIn.Length);

    byte[] bytKey = GetLegalKey(Key);

    mobjCryptoService.Key = bytKey;

    mobjCryptoService.IV = bytKey;

    ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();

    CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

    System.IO.StreamReader sr = new System.IO.StreamReader(cs);

    strDecrpt = sr.ReadToEnd();

    return strDecrpt;

    }

    catch (Exception Exp)

    {

    throw Exp;

    }

    }

    else

    {return strDecrpt;}

    }

    return Source;

    }
     
    gane kol, Jun 17, 2005
    #1
    1. Advertisements

  2. gane kol <> wrote:
    > I am using DES algorithm. I am getting an error message in a few cases of
    > the querystring
    >
    > Error Message : Length of the data to decrypt is invalid.
    > Error Method : System.String Decrypting(System.String)
    > Error Line: strDecrpt = sr.ReadToEnd();


    <snip>

    Well, this is quite odd code, to be honest. It could be made far more
    readable quite easily. Some comments though:

    1) You're truncating the encrypted data at the first 0 byte after the
    8th byte. Why? Are you trying to get to the end of the real stream
    data? If so, just use ms.ToArray() instead, or just use the length of
    the stream rather than testing things.

    2) You're converting the text data to binary using Encoding.ASCII - are
    you sure that your data will never contain non-ASCII characters?


    As an example of making the code simpler, here's what I think
    Encrypting should look like:

    public string Encrypting(string source)
    {
    if (ConfigurationSettings.AppSettings["ENV"] == "DEV")
    {
    return source;
    }

    if (source==null || source.Length==0)
    {
    return null;
    }

    byte[] keyData = GetLegalKey (CRYPTO_KEY);
    mobjCryptoService.Key = keyData;
    mobjCryptoService.IV = keyData;
    ICryptoTransform transform = mobjCryptoService.CreateEncryptor();
    using (MemoryStream ms = new MemoryStream())
    {
    using (CryptoStream cs = new CryptoStream(ms, transform
    CryptoStreamMode.Write))
    {
    using (StreamWriter writer = new StreamWriter(cs))
    {
    writer.Write (source);
    }
    cs.FlushFinalBlock();
    }
    return Convert.ToBase64String (ms.ToArray());
    }
    }

    (I suspect the call to FlushFinalBlock is actually unnecessary here, as
    disposing of the StreamWriter will close the crypto stream, which will
    flush it automatically.)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Jun 17, 2005
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jimski
    Replies:
    3
    Views:
    811
    DalePres
    Mar 15, 2005
  2. Dica
    Replies:
    7
    Views:
    1,339
    Jon Skeet [C# MVP]
    Oct 28, 2005
  3. Robson Siqueira

    Re: decrypt problem: Length of the data to decrypt is invalid

    Robson Siqueira, Dec 19, 2006, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    419
    Jon Skeet [C# MVP]
    Dec 19, 2006
  4. Jon Skeet [C# MVP]

    Re: decrypt problem: Length of the data to decrypt is invalid

    Jon Skeet [C# MVP], Dec 19, 2006, in forum: Microsoft C# .NET
    Replies:
    5
    Views:
    2,936
    Guest
    Dec 21, 2006
  5. Oleg Subachev
    Replies:
    4
    Views:
    574
    Oleg Subachev
    Dec 21, 2006
Loading...

Share This Page