Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Rijndael Encryption in 2.0
Message
De
08/05/2007 12:02:52
 
 
À
08/05/2007 08:51:03
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Cryptographie
Versions des environnements
Environment:
VB 8.0
Divers
Thread ID:
01223342
Message ID:
01223437
Vues:
15
>Does anybody have a working sample of using Rijndael encryption in .NET 2.0? I found a sample on MSDN, but it doesn't seem to allow me to use my own key, salt, etc.
>
>I could modify it to use supplied parameters, but I want to make sure that it is portable. I got stuck in the past using encryption that wasn't portable, and that was an ordeal to fix.
>
>Thanks!

Hi Mike,

Following are some methods I use in an application for Rijndael encryption. They may not be what you're looking for but may help get you going in the right direction.
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);
		}
William A. Caton III
Software Engineer
MAXIMUS
Atlanta, Ga.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform