Breaking News
Home / My Tools / My Encrypter & Decrypter Tool

My Encrypter & Decrypter Tool

These days security is becoming a bigger and bigger concern and depending on who you work for you can’t have connection string or other sensitive data in your code.

I created an Encryption tool to solve this problem for myself using .NET’s TripleDesCryptoServiceProvider class that uses the TripleDES algorithm.

The application contains 2 methods and the call info to use in console, web application or windows application.

You can use this code to create a console application, windows application or a web application.

Method1 – The Encrypter Method


static string Encrypt(string source, string key)
{
  using (TripleDESCryptoServiceProvider tripleDESCryptoService = new    TripleDESCryptoServiceProvider())
  {
   using (MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider())
   {
    byte[] byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
    tripleDESCryptoService.Key = byteHash;
    tripleDESCryptoService.Mode = CipherMode.ECB;
    byte[] data = Encoding.UTF8.GetBytes(source);
    return Convert.ToBase64String(tripleDESCryptoService.CreateEncryptor().TransformFinalBlock(data, 0, data.Length));
   }
 }
}

First we instantiate the provider to be able to access the TripeDES algorithm.

using (TripleDESCryptoServiceProvider tripleDESCryptoService = new TripleDESCryptoServiceProvider())

Next we instantiate Crypto service provider to compute input to MD5 hash value.

using (MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider())

Here we convert our secret key to a byte array(byteHash) via the compute hash method

byte[] byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));

Now add the byteHash to the secret key for the TripleDES algorithm and set the mode for operation of the symmetric algorithm to ECB. ECB encrypts each block individually. There are 4 other Cipher Modes you can explore but I chose ECB because my current security needs were not that high.


tripleDESCryptoService.Key = byteHash;
tripleDESCryptoService.Mode = CipherMode.ECB;

Now convert our source string to a byte array called “data”

byte[] data = Encoding.UTF8.GetBytes(source);

Finally we encrypt our source byte array by running it through the CreateEncryptor method of our TripleDESCryptoServiceProvider and return the encrypted string.

return Convert.ToBase64String(tripleDESCryptoService.CreateEncryptor().TransformFinalBlock(data, 0, data.Length));

Method2 – The Decrypter Method


static string Decrypt(string encrypt, string key)
{
  using (TripleDESCryptoServiceProvider tripleDESCryptoService = new               TripleDESCryptoServiceProvider())
  {
    using (MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider())
    {
      byte[] byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
      tripleDESCryptoService.Key = byteHash;
      tripleDESCryptoService.Mode = CipherMode.ECB;
      byte[] data = Convert.FromBase64String(encrypt);
      return Encoding.UTF8.GetString(tripleDESCryptoService.CreateDecryptor().TransformFinalBlock(data, 0, data.Length));
    }
  }
}

The only difference between the encrypter and decrypter is the encrypted text is passed and the source byte array is run through the CreateDecryptor method of our TripleDESCryptoServiceProvider and return the decrypted string.

return Encoding.UTF8.GetString(tripleDESCryptoService.CreateDecryptor().TransformFinalBlock(data, 0, data.Length));

The Calls

Now we get to use out methods.


static void Main(string[] args)
{
    string encrypted = "", decrypted = "";
         //YOUR STRING TO ENCRYPT
    string connString = "Server=xxxxxxx;Database=xxxxxx;;User     Id=xxxxxxx;Password=xxxxxx";

    encrypted = Encrypt(connString, "baddog");
    Console.WriteLine("Encrypted: " + encrypted);

    Console.WriteLine();

    decrypted = Decrypt(encrypted, "baddog");
    Console.WriteLine("Decrypted: " + decrypted);
}

 

Have you created any nice encryption tools?

About Brian

Leave a Reply

Your email address will not be published. Required fields are marked *