Calling Encrypt/Decrypt Methods: /****************************/ private void RijndaelEncrypt() /****************************/ { // Set up Encryption parameters int intKeySize = Convert.ToInt32(this.cboKeySize.SelectedItem.ToString()); byte[] bytKey = null; if (this.txtKey.Text.Length != 0) bytKey = CryptoniteAlgorithmMethods.GetByteArrayFromBase64String(this.txtKey.Text); byte[] bytIV = null; if (this.txtIV.Text.Length != 0) bytIV = CryptoniteAlgorithmMethods.GetByteArrayFromBase64String(this.txtIV.Text); string strEncryptedData = ""; if (this.optASCII.Checked) strEncryptedData = CryptoniteAlgorithmMethods.RijndaelEncryptData(this.txtPlainText.Text, intKeySize, ref bytKey, ref bytIV); else strEncryptedData = CryptoniteAlgorithmMethods.RijndaelEncryptData(this.txtPlainText.Text, false, intKeySize, ref bytKey, ref bytIV); this.txtEncrypted.Text = strEncryptedData; this.txtKey.Text = Convert.ToBase64String(bytKey); this.txtIV.Text = Convert.ToBase64String(bytIV); } /****************************/ private void RijndaelDecrypt() /****************************/ { // Set up Encryption parameters byte[] bytKey = CryptoniteAlgorithmMethods.GetByteArrayFromBase64String(this.txtKey.Text); byte[] bytIV = CryptoniteAlgorithmMethods.GetByteArrayFromBase64String(this.txtIV.Text); string strDecryptedData = ""; if (this.optASCII.Checked) strDecryptedData = CryptoniteAlgorithmMethods.RijndaelDecryptData(this.txtEncrypted.Text, bytKey, bytIV); else strDecryptedData = CryptoniteAlgorithmMethods.RijndaelDecryptData(this.txtEncrypted.Text, false, bytKey, bytIV); this.txtPlainText.Text = strDecryptedData; } Auxiliary String/ByteArray Method: /********************************************************************/ public static byte[] GetByteArrayFromBase64String(string strB64String) /********************************************************************/ { return Convert.FromBase64String(strB64String); } Encrypt/Decrypt Algorithms and Overloads: /********************************************************************************************************************/ public static string RijndaelEncryptData(byte[] bytDataToEncrypt, int intKeySize, ref byte[] bytKey, ref byte[] bytIV) /********************************************************************************************************************/ { // Instantiate a memory stream to hold the encrypted data MemoryStream ms = new MemoryStream(); // Create the Rijndael encryptor RijndaelManaged encryptor = new RijndaelManaged(); encryptor.KeySize = intKeySize; // Use provided Key/IV or Generate if (bytKey == null) { encryptor.GenerateKey(); bytKey = encryptor.Key; } else encryptor.Key = bytKey; if (bytIV == null) { encryptor.GenerateIV(); bytIV = encryptor.IV; } else encryptor.IV = bytIV; // Instantiate a Crypto Stream to encrypt the data CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write); cs.Write(bytDataToEncrypt, 0, bytDataToEncrypt.Length); cs.FlushFinalBlock(); // Make sure all bytes written cs.Close(); return Convert.ToBase64String(ms.ToArray()); // Convert to (Base-64) string } /**************************************************************************************************************************************/ public static string RijndaelEncryptData(string strDataToEncrypt, bool ASCIIString, int intKeySize, ref byte[] bytKey, ref byte[] bytIV) /**************************************************************************************************************************************/ { // Byte array to hold buffered data to encrypt byte[] byt; if (ASCIIString) byt = Encoding.ASCII.GetBytes(strDataToEncrypt); else byt = Encoding.Unicode.GetBytes(strDataToEncrypt); return RijndaelEncryptData(byt, intKeySize, ref bytKey, ref bytIV); } /********************************************************************************************************************/ public static string RijndaelEncryptData(string strDataToEncrypt, int intKeySize, ref byte[] bytKey, ref byte[] bytIV) /********************************************************************************************************************/ { return RijndaelEncryptData(strDataToEncrypt, true, intKeySize, ref bytKey, ref bytIV); } /**************************************************************************************************************/ public static string RijndaelDecryptData(byte[] bytDataToDecrypt, bool ASCIIString, byte[] bytKey, byte[] bytIV) /**************************************************************************************************************/ { // Instantiate a memory stream to hold the decrypted data MemoryStream ms = new MemoryStream(); // Create the Rijndael decryptor RijndaelManaged decryptor = new RijndaelManaged(); // Instantiate a Crypto stream to decrypt the data CryptoStream cs = new CryptoStream(ms, decryptor.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write); cs.Write(bytDataToDecrypt, 0, bytDataToDecrypt.Length); cs.FlushFinalBlock(); // Make sure all bytes written cs.Close(); if (ASCIIString) return Encoding.ASCII.GetString(ms.ToArray()); // Convert to ASCII string else return Encoding.Unicode.GetString(ms.ToArray()); // Convert to Unicode string } /**************************************************************************************************************/ public static string RijndaelDecryptData(string strDataToDecrypt, bool ASCIIString, byte[] bytKey, byte[] bytIV) /**************************************************************************************************************/ { // Byte array to hold buffered data to decrypt byte[] byt = Convert.FromBase64String(strDataToDecrypt); return RijndaelDecryptData(byt, ASCIIString, bytKey, bytIV); } /********************************************************************************************/ public static string RijndaelDecryptData(string strDataToDecrypt, byte[] bytKey, byte[] bytIV) /********************************************************************************************/ { // Byte array to hold buffered data to decrypt byte[] byt = Convert.FromBase64String(strDataToDecrypt); return RijndaelDecryptData(byt, true, bytKey, bytIV); }