public class clsEncryption { private byte[] _Key; private byte[] _IV; public clsEncryption() { } public clsEncryption(string KeyName) { try { using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Encryption", true)) { if (rk.GetSubKeyNames().Contains(KeyName)) { rk.OpenSubKey(@"Software\Wow6432Node\Key\" + KeyName, false); _Key = (byte[])rk.GetValue("enKey"); _IV = (byte[])rk.GetValue("IV"); } else { //if the Registry Key does not exist, return an error throw (new Exception("Specified encryption key does not exist.")); } } } catch (Exception ex) { throw (new Exception(ex.Message)); } } public byte[] Encrypt(string plainText) { if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (_Key == null || _Key.Length <= 0) throw new ArgumentNullException("Key"); if (_IV == null || _IV.Length <= 0) throw new ArgumentNullException("IV"); byte[] result = null; byte[] dataToEncrypt = Encoding.UTF8.GetBytes(plainText); try { // Create a RijndaelManaged object with the specified key and IV. using (RijndaelManaged aesAlg = new RijndaelManaged()) { aesAlg.Mode = CipherMode.CBC; aesAlg.Key = _Key; aesAlg.IV = _IV; // Create an encryptor to perform the stream transform. using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)) { // Create the stream used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { csEncrypt.Write(dataToEncrypt, 0, dataToEncrypt.Length); // encrypt the data and write it to the memory stream. csEncrypt.FlushFinalBlock(); // update the state and clear the buffer csEncrypt.Close(); } result = msEncrypt.ToArray(); msEncrypt.Close(); } } aesAlg.Clear(); } } catch (Exception ex) { throw (new Exception(ex.Message)); } // Return the encrypted bytes from the memory stream. return result; } public string Decrypt(byte[] cipherText) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (_Key == null || _Key.Length <= 0) throw new ArgumentNullException("Key"); if (_IV == null || _IV.Length <= 0) throw new ArgumentNullException("IV"); string result = string.Empty; try { // Create a RijndaelManaged object with the specified key and IV. using (RijndaelManaged aesAlg = new RijndaelManaged()) { aesAlg.Mode = CipherMode.CBC; aesAlg.Key = _Key; aesAlg.IV = _IV; // Create a decrytor to perform the stream transform. using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)) { // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write)) { // decrypt the data and write it to the memory stream. csDecrypt.Write(cipherText, 0, cipherText.Length); csDecrypt.FlushFinalBlock(); // update the state and clear the buffer csDecrypt.Close(); } result = Encoding.UTF8.GetString(msDecrypt.ToArray()); msDecrypt.Close(); } } aesAlg.Clear(); } } catch (Exception ex) { throw (new Exception(ex.Message)); } //Return the decrypted string return result; } public void CreateNewEncryptionKey(string KeyName) { try { using (RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Key", true)) { if (!rk.GetSubKeyNames().Contains(KeyName)) { using (RijndaelManaged objRJ = new RijndaelManaged()) { objRJ.KeySize = 256; _Key = objRJ.Key; _IV = objRJ.IV; rk.CreateSubKey(KeyName); rk.SetValue("enKey", _Key, RegistryValueKind.Binary); rk.SetValue("IV", _IV, RegistryValueKind.Binary); objRJ.Clear(); } } else { throw (new Exception("Specified encryption key already exist")); } rk.Dispose(); } } catch (Exception ex) { throw new Exception(ex.Message); } } }
No one has followed this question yet.