Library Encrypt and Decrypt Methods Using TripleDES and MD5
posted on Monday, January 09, 2006 by bobby @ 7:01 am
For you encrypting enjoyment:
Copy code to clipboard in IE or select code for Firefox
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);
}


And in case you ever want to decrypt that string you just encrypted:
Copy code to clipboard in IE or select code for Firefox
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);
}


CommentsComments
posted on Friday, February 24, 2006  by Robert Hurlbut @ 8:30 AM

This is not bad, but it is missing one key piece. Both MD5CryptoServiceProvider and
TripleDESCryptoServiceProvider are managed wrappers around unmanaged resources. You need to use Clear (which in turn calls Dispose) or Dispose directly, ideally in a finally block of a try/catch.
posted on Wednesday, March 01, 2006  by Roman Pushkin @ 12:13 AM

Greeting, Bobby! Nice code! Thanks for it.
posted on Tuesday, May 16, 2006  by moshiur @ 5:38 AM

yes.. Good Stuff. But for the sake of best practise u needed to do some extra lines. like instead of

Copy code to clipboard in IE or select code for Firefox
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
write: byteCode=UTF8Encoding.UTF8.GetBytes(key);
keyArray = hashmd5.ComputeHash(byteCode);


and finall a serious mistake not to be done. DONOT forget to clear/flush the en/de-crypton
like :::: hashmd5.clear();
and tdes.clear();

any way nice and helpful stuff
posted on Tuesday, May 16, 2006  by bobby @ 5:42 AM

Why is it necessary to put the bytes into another, seperate variable, which will just be hashed into the key array on the next line? What is the benefit?

Cheers
posted on Tuesday, May 16, 2006  by moshiur @ 5:54 AM

i just mentioned abt the best practices of the industry....
you may ommit so many code... for example lets say you wrote.. in encryption and decryption too..

Copy code to clipboard in IE or select code for Firefox
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;


all these lines can be converted to one line code IF YOU PASS all these as constructor params....
The reason i am saying this is You should practise the BEST approach and go deep and know more what is going under the hood.....
ANYWAY it was nice code
posted on Sunday, June 25, 2006  by Anonymous @ 4:31 PM

can someone decrypt a PKCS7 string for me?
posted on Friday, July 07, 2006  by Nicola @ 2:26 AM

very very usefully, great!!!
posted on Wednesday, May 23, 2007  by Kumar @ 9:10 PM

It is very very userful. Thanks.
posted on Friday, September 07, 2007  by MJ @ 12:19 AM

Thank you for posting this, it was quite helpful. I was working through using the AesCryptoServiceProvider in VS 2008 (.Net 3.x), and was having a hell of a problem. MSDN didn't have what I needed, and trying to implement their workings of DES (which would be a logical precursor and process base for AES...but what the heck do I know?), and having no luck.

So now for your info, this general scheme works for AES as well!
posted on Friday, November 02, 2007  by Andy @ 11:05 AM

Nice...As always, the BEST practice is the practice that WORKS...this works very well.
posted on Wednesday, April 02, 2008  by Anonymous @ 12:25 PM

ok

enter enter
posted on Monday, November 10, 2008  by Anonymous @ 2:11 PM

fantastic. exactly what we needed
posted on Wednesday, December 03, 2008  by Juanmanuelz @ 12:48 PM

Hi, just a comment for a related topic.
if you need to crypt in PHP and decrypt in C# using 3DES, you can do something like that:

Copy code to clipboard in IE or select code for Firefox
$secret_key   = "THESECRETKEYFOR3DESABCDEF";
    $iv = base64_decode("5Fl6lc4xjwA=");
    
    $crypttext = mcrypt_encrypt(MCRYPT_3DES, $secret_key, $ID, MCRYPT_MODE_CBC, $iv);
    $id_encriptado = urlencode(base64_encode($crypttext));

The url_encode and base64_encode is just for web compatibility issues.

Then, you can decrypt the generated strings in C# using the following code:
Copy code to clipboard in IE or select code for Firefox
try{
        TripleDESCryptoServiceProvider tripleDESProvider = newTripleDESCryptoServiceProvider();
        
        tripleDESProvider.Key = Encoding.UTF8.GetBytes("THESECRETKEYFOR3DESABCDEF");
        tripleDESProvider.IV = Convert.FromBase64String( "5Fl6lc4xjwA=");
        tripleDESProvider.Mode = CipherMode.CBC;
        tripleDESProvider.Padding = PaddingMode.Zeros;//THE SUPERNATURAL STUFF
        
        ICryptoTransform ict = tripleDESProvider.CreateDecryptor();
        byte [] fromBase64 = Convert.FromBase64String(textToDecrypt);
        
        
        byte [] decryptedData = ict.TransformFinalBlock( fromBase64 ,0, fromBase64.Length );
        return Encoding.ASCII.GetString(decryptedData);
        
    }
    catch(Exception s){
        Console.WriteLine(s.ToString());
    }

    return "";


In order to prevent problems with the IV Key it's better to store it in base64.


New Post Notification

Search Posts

Recent Posts


About Meeself
People call me Bobby DeRosa
I live somewhere in San Diego, CA
MCSD, MCAD, MCP

This theme was adapted from fUnique by fahlstad        Icons by FamFamFam        XHTML 1.0 Strict; tuned for Mozilla-powered browsers

Admin Login Administrator Login
Invalid login attempts are logged.
  Username:
  Password: