logo

invalid length while decrypting TripleDESCryptoServiceProvider (Visual Studio .NET)
abc V posted at Thursday, June 12, 2008 7:13 PM


public static string DESDecrypt(string sToDecrypt)

{

if (0 == sToDecrypt.Length) return "";

byte[] Code = Convert.FromBase64String(sToDecrypt);

string decrypted = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(Code, 0, Code.Length));

return decrypted;

}

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0
Insufficient code in your sample
Peter Bromberg replied to abc V on Thursday, June 12, 2008 9:09 PM

What is "des" and where is it declared?

Typical implementation of TripleDES:

public static string Encrypt(string toEncrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    if(useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateEncryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

public static string Decrypt(string toDecrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

    if(useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return UTF8Encoding.UTF8.GetString(resultArray);
}

Reply    Reply Using Power Editor
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Pete Tweets at peterbromberg
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0

Error while decrypting using TripleDESCryptoServiceProvider
Rakesh Vikram replied to abc V on Friday, June 13, 2008 12:43 AM

Hi, go through the below link. this might help you a bit.

http://66.129.67.4/t/1251859.aspx

http://www.derkeiler.com/pdf/Newsgroups/microsoft.public.dotnet.security/2004-07/0024.pdf

http://forums.msdn.microsoft.com/en-US/netfxnetcom/thread/97141091-8ee9-4bb8-a24d-2c4b5fa2b8ff/

You can also find more info. here.

http://www.codeproject.com/KB/dotnet/Cryptography_MD5_TriDES.aspx

http://www.codeproject.com/KB/security/encryption_decryption.aspx

Hope this helps you a bit. All the Best..!!!

Rakesh Vikram

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0

Key should be same....
Vasanthakumar D provided a rated reply to abc V on Friday, June 13, 2008 3:01 AM

Hi,

you need to use the same key used in encryption for decryption also.

while encrypting store the key created in database.

and while decryption fetch the key from the datbase and use the same...

or use common key for encryption all time. Its better to store the key in web.config and use it...

or you can hard code key directly in your code....

public static string DESEncrypt(string data)
     {
         try
         {
             string results = "";
             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
             Byte[] keybytes = Encoding.ASCII.GetBytes("???hf@?");
             Byte[] vectorbytes = Encoding.ASCII.GetBytes("V$?????");
             ICryptoTransform encryptor = des.CreateEncryptor(keybytes, vectorbytes);
             Byte[] databytes = Encoding.Default.GetBytes(data);
             Byte[] cipherbytes = encryptor.TransformFinalBlock(databytes, 0, databytes.Length);
             results = Convert.ToBase64String(cipherbytes);
             return results;
         }
         catch(Exception ex)
         {
             return string.Empty;
         }
     }

    public static string DESDecrypt(String cipher)
    {
        try
        {
            string results = "";
            Byte[] clearbytes;
            Byte[] vectorbytes = Encoding.ASCII.GetBytes("V$?????");
            Byte[] keybytes = Encoding.ASCII.GetBytes("???hf@?");
            Byte[] cipherbytes = Convert.FromBase64String(cipher);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            ICryptoTransform decryptor = des.CreateDecryptor(keybytes, vectorbytes);
            clearbytes = decryptor.TransformFinalBlock(cipherbytes, 0, cipherbytes.Length);
            results = Encoding.Default.GetString(clearbytes);
            return results;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0

Reply
alice johnson provided a rated reply to abc V on Tuesday, June 17, 2008 10:41 PM


use the same key used in encryption for decryption ,store the key in web.config and use it:
public static string DESEncrypt(string data)
     {
         try
         {
             string results = "";
             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
             Byte[] keybytes = Encoding.ASCII.GetBytes("???hf@?");
             Byte[] vectorbytes = Encoding.ASCII.GetBytes("V$?????");
             ICryptoTransform encryptor = des.CreateEncryptor(keybytes, vectorbytes);
             Byte[] databytes = Encoding.Default.GetBytes(data);
             Byte[] cipherbytes = encryptor.TransformFinalBlock(databytes, 0, databytes.Length);
             results = Convert.ToBase64String(cipherbytes);
             return results;
         }
         catch(Exception ex)
         {
             return string.Empty;
         }
     }

    public static string DESDecrypt(String cipher)
    {
        try
        {
            string results = "";
            Byte[] clearbytes;
            Byte[] vectorbytes = Encoding.ASCII.GetBytes("V$?????");
            Byte[] keybytes = Encoding.ASCII.GetBytes("???hf@?");
            Byte[] cipherbytes = Convert.FromBase64String(cipher);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            ICryptoTransform decryptor = des.CreateDecryptor(keybytes, vectorbytes);
            clearbytes = decryptor.TransformFinalBlock(cipherbytes, 0, cipherbytes.Length);
            results = Encoding.Default.GetString(clearbytes);
            return results;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos










  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010