Search This Blog

Wednesday, November 9, 2011

Encryption Decryption Class

The purpose of this class is to provide the methods which are used for Encrypting and Decrypting the Passwords and other sensitive information to be stored in database or file system.

Many a times we require to encrypt the password to store in web.config file where we can't store the password without encryption because of security reasons.

Here is the class that we can use for encryption and decryption  of string values. This class supports both the string and byte[]


public class Cryptography
    {
        public static string Encrypt(string clearText, Random randomNumber)
        {
           String salt;
           byte[] clearBytes, encryptedData;
           PasswordDeriveBytes pdb;

           salt = Convert.ToString(randomNumber.Next(9999, DateTime.Now.Millisecond * 9999));

           clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
           pdb = new PasswordDeriveBytes(salt,new byte[] {0x49, 0x76, 0x61, 0x6e,
                                         0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
           encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
           return salt.Length + salt + Convert.ToBase64String(encryptedData);

        }

        public static string Encrypt(string clearText)
        {
            Random randomNumber = new Random();
            return Encrypt(clearText, randomNumber);
        }

        public static string Decrypt(string cipherText)
        {
            if (!string.IsNullOrEmpty(cipherText))
            {
                Int32 length;
                String salt;
                byte[] cipherBytes, decryptedData;
                PasswordDeriveBytes pdb;

                length = Convert.ToInt32(cipherText.Substring(0,1));
                salt = cipherText.Substring(1, length);
                cipherText = cipherText.Substring(length + 1);
                cipherBytes = Convert.FromBase64String(cipherText);
                pdb = new PasswordDeriveBytes(salt, new byte[] {0x49, 0x76, 0x61, 0x6e,
                                              0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            else
            {
                return string.Empty;
            }
        }

        private static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms;
            Rijndael alg;
            CryptoStream cs;
            byte[] decryptedData;

            ms = new MemoryStream();
            alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;

            cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(cipherData, 0, cipherData.Length);
            cs.Close();

            decryptedData = ms.ToArray();
            return decryptedData;
        }

        private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms;
            Rijndael alg;
            CryptoStream cs;
            byte[] encryptedData;

            ms = new MemoryStream();
            alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;

            cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            encryptedData = ms.ToArray();
            return encryptedData;
        }
    }


Download code from here : http://min.us/m9KsrNOZ1



No comments:

Post a Comment