Geeks With Blogs
Annie Bougie

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);

}

Posted on Tuesday, March 2, 2010 4:41 AM C# Code | Back to top


Comments on this post: Encrypt and Decrypt Strings

# re: Encrypt and Decrypt Strings
Requesting Gravatar...
thank you for an interesting post. I've always been wondering how encryption works. now have a general understatement, still, in order to finish my paperwriter.topproject will have to read into the question a little bit more, but still you have been a huge help!!
Left by Callie on Jan 28, 2018 11:58 PM

Your comment:
 (will show your gravatar)


Copyright © Annie Bougie | Powered by: GeeksWithBlogs.net