posts - 17 , comments - 52 , trackbacks - 0

My Links

News

Contact Me anne.bougie@gmail.com

Tag Cloud

Article Categories

Archives

Post Categories

Encrypt and Decrypt Strings

Here is a quick solution to encrypting and decrypting passwords, or any strings, easily. Write a test that runs the GetNewKey method to generate a unique key. I put the key right in the class here for demonstration purposes, but you can store it anyplace. Write another test to encrypt your password so you know the value to save in your configuration settings, and you're all set. Just remember not to save your test with the clear text password in it!

public static class Helper

{

   private static string _key = "DFQoC2KFQNdbs7fLUNjZQB9qDsbHLylC";

 

   public static string EncryptPassword(this string clearText)

   {

      byte[] originalAsBytes = Encoding.ASCII.GetBytes(clearText);

      byte[] result;

      byte[] keyArray = Convert.FromBase64String(_key);

      using (var tdes = new TripleDESCryptoServiceProvider())

      {

         tdes.Key = keyArray;

         tdes.Mode = CipherMode.ECB;

         tdes.Padding = PaddingMode.Zeros;

 

         ICryptoTransform transform = tdes.CreateEncryptor();

         result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);

      }

 

      string encrypted = Convert.ToBase64String(result);

      return encrypted;

   }

 

   public static string DecryptPassword(this string encrypted)

   {

      byte[] originalAsBytes = Convert.FromBase64String(encrypted);

      byte[] keyArray = Convert.FromBase64String(_key);

      var tdes = new TripleDESCryptoServiceProvider { Key = keyArray, Mode = CipherMode.ECB, Padding = PaddingMode.Zeros };

 

      ICryptoTransform transform = tdes.CreateDecryptor();

      byte[] result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);

      result = StripZeroPadding(result);

      string clearText = Encoding.ASCII.GetString(result);

      return clearText;

   }

 

   public static string GetNewKey()

   {

      var tdes = new TripleDESCryptoServiceProvider { KeySize = 192 };

      tdes.GenerateKey();

      byte[] keyArray = tdes.Key;

      string key = Convert.ToBase64String(keyArray);

      return key;

   }

 

   private static byte[] StripZeroPadding(byte[] array)

   {

      int i = array.Length - 1;

      int j = array.Length;

      while (array[i] == 0)

      {

         j = i;

         i--;

      }

 

      var result = new byte[j];

      for (i = 0; i < j; i++)

         result[i] = array[i];

      return result;

   }

}

 

Here are the NUnit tests:

 

[Test]

public void GetNewKeyTest()

{

   string key = Helper.GetNewKey();

   Console.WriteLine(key);

}

 

[Test]

public void EncryptDecryptPasswordTest()

{

   string password = "password";

   string encrypted = password.EncryptPassword();

   Console.WriteLine(encrypted);

   Assert.AreNotEqual(password, encrypted);

 

   string decrypted = encrypted.DecryptPassword();

   Assert.AreEqual(password, decrypted);

}

Print | posted on Tuesday, March 2, 2010 4:41 AM | Filed Under [ C# Code ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: