Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other Visual Studio .NET Posts   Ask New Question  Ask New Question With Power Editor

invalid length while decrypting TripleDESCryptoServiceProvider
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
November 0 $0.00 0
October 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.
Please post questions at forums, not via email!
  Rank Winnings Points
November 0 $0.00 0
October 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
November 0 $0.00 0
October 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
November 0 $0.00 0
October 6 $42.00 145

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
November 0 $0.00 0
October 0 $0.00 0