DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Aniruddha has posted 40 posts at DZone. View Full User Profile

Encrypt And Decrypt Using C# - Symmetric Encryption

10.29.2010
| 5499 views |
  • submit to reddit
        
Encryption and Decryption using RijndaelManaged class in .NET
public static string EncryptString(string plainText, string encryptionKey)
        {
            string encryptedString = string.Empty;
            RijndaelManaged algo = new RijndaelManaged();
            try
            {
                // for Convenience, use some static string as salt.
                byte[] salt = Encoding.ASCII.GetBytes("GhostBusters");
                Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(encryptionKey, salt);
                algo.Key = key.GetBytes(algo.KeySize / 8);
                algo.IV = key.GetBytes(algo.BlockSize / 8);
                ICryptoTransform encryptor = algo.CreateEncryptor();
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream encryptStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(encryptStream))
                        {
                            sw.Write(plainText);
                            //encryptStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                        }
                    }
                    encryptedString = Convert.ToBase64String(ms.ToArray());
                }
            }
            finally
            {
                if (algo != null)
                    algo.Clear();
            }
            return encryptedString;
        }

        public static string DecryptString(string encryptedText, string encryptionKey)
        {
            string plainString = string.Empty;
            RijndaelManaged algo = new RijndaelManaged();
            try
            {
                // make sure that you use the same salt.
                byte[] salt = Encoding.ASCII.GetBytes("GhostBusters");
                Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(encryptionKey, salt);
                algo.Key = key.GetBytes(algo.KeySize / 8);
                algo.IV = key.GetBytes(algo.BlockSize / 8);
                ICryptoTransform decryptor = algo.CreateDecryptor();
                byte[] bytes = Convert.FromBase64String(encryptedText);
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (CryptoStream decryptStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(decryptStream))
                        {
                            plainString = sr.ReadToEnd();
                        }
                    }
                }
            }
            finally
            {
                if (algo != null)
                    algo.Clear();
            }
            return plainString;
        }