} // End Function Encrypt public string DeCrypt(string encryptedInput) { string returnValue = null; if (string.IsNullOrEmpty(encryptedInput)) { throw new System.ArgumentNullException("encryptedInput", "encryptedInput may not be string.Empty or NULL, because these are invid values."); } using (System.Security.Cryptography.Aes objRijndael = System.Security.Cryptography.Aes.Create()) { byte[] baCipherTextBuffer = HexStringToByteArray(encryptedInput); byte[] baDecryptionKey = HexStringToByteArray(this.m_key); byte[] baInitializationVector = HexStringToByteArray(this.m_iv); // This is where the message would be transmitted to a recipient // who already knows your secret key. Optionally, you can // also encrypt your secret key using a public key algorithm // and pass it to the mesage recipient along with the RijnDael // encrypted message. //Get a decryptor that uses the same key and IV as the encryptor. using (System.Security.Cryptography.ICryptoTransform transform = objRijndael.CreateDecryptor(baDecryptionKey, baInitializationVector)) { //Now decrypt the previously encrypted message using the decryptor // obtained in the above step. using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(baCipherTextBuffer)) { using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream( msDecrypt , transform , System.Security.Cryptography.CryptoStreamMode.Read) ) { byte[] baPlainTextBuffer = new byte[baCipherTextBuffer.Length]; //Read the data out of the crypto stream. csDecrypt.Read(baPlainTextBuffer, 0, baPlainTextBuffer.Length); //Convert the byte array back into a string. returnValue = this.m_encoding.GetString(baPlainTextBuffer); System.Array.Clear(baPlainTextBuffer, 0, baPlainTextBuffer.Length); baPlainTextBuffer = null; if (!string.IsNullOrEmpty(returnValue)) { returnValue = returnValue.Trim('\0'); } csDecrypt.Clear(); } // End Using csDecrypt } // End Using msDecrypt } // End Using transform } // End Using aes return(returnValue); } // End Function DeCrypt
} // End Function GenerateKey public string Encrypt(string plainText) { string retValue = null; using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create()) { byte[] baCipherTextBuffer = null; byte[] baPlainTextBuffer = null; byte[] baEncryptionKey = HexStringToByteArray(this.m_key); byte[] baInitializationVector = HexStringToByteArray(this.m_iv); aes.Key = baEncryptionKey; aes.IV = baInitializationVector; //Get an encryptor. using (System.Security.Cryptography.ICryptoTransform transform = aes.CreateEncryptor(baEncryptionKey, baInitializationVector)) { //Encrypt the data. using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream( msEncrypt , transform , System.Security.Cryptography.CryptoStreamMode.Write ) ) { //Convert the data to a byte array. baPlainTextBuffer = this.m_encoding.GetBytes(plainText); //Write all data to the crypto stream and flush it. csEncrypt.Write(baPlainTextBuffer, 0, baPlainTextBuffer.Length); csEncrypt.FlushFinalBlock(); //Get encrypted array of bytes. baCipherTextBuffer = msEncrypt.ToArray(); csEncrypt.Clear(); } // End Using csEncrypt } // End Using msEncrypt } // End Using transform retValue = ByteArrayToHexString(baCipherTextBuffer); System.Array.Clear(baCipherTextBuffer, 0, baCipherTextBuffer.Length); System.Array.Clear(baPlainTextBuffer, 0, baPlainTextBuffer.Length); System.Array.Clear(baEncryptionKey, 0, baEncryptionKey.Length); System.Array.Clear(baInitializationVector, 0, baInitializationVector.Length); baCipherTextBuffer = null; baPlainTextBuffer = null; baEncryptionKey = null; baInitializationVector = null; } // End Using aes return(retValue); } // End Function Encrypt