Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Security.

Cryptography; namespace WindowsFormsApplication2 { class AES { byte[] key; byte[] iVector; /// <summary> /// property to get key for algorithm /// </summary> public byte[] Key { get { return key; } } /// <summary> /// property to get initialization vector /// </summary> public byte[] IV { get { return iVector; } } /// <summary> /// default constructor will generate random key and iv for encryption decryption. /// </summary> public AES() { RijndaelManaged rij = new RijndaelManaged(); rij.GenerateKey(); rij.GenerateIV(); key = rij.Key; iVector = rij.IV; } /// <summary> /// otherwise provide byte[] of key and iv both /// </summary> /// <param name="key"></param> /// <param name="iv"></param> public AES(byte[] key, byte[] iv) { if (key == null || key.Length <= 0) throw new ArgumentNullException("key"); if (iv == null || iv.Length <= 0) throw new ArgumentNullException("IV");

this.key = key; this.iVector = iv; } /// <summary> /// method which encrypts given text to cipher bytes /// </summary> /// <param name="plainText"></param> /// <returns>byte array of cipher bytes</returns> private byte[] EncryptText(string plainText) { if (plainText == "" || plainText.Length <= 0) throw new ArgumentNullException("plainText"); byte[] cipherBytes; using (RijndaelManaged objRM = new RijndaelManaged()) { objRM.Key = key; objRM.IV = iVector; ICryptoTransform encryptor = objRM.CreateEncryptor(objRM.Key, objRM.IV); using (MemoryStream objMS = new MemoryStream()) { using (CryptoStream objCS = new CryptoStream(objMS, encryptor, CryptoStreamMode.Write)) { using (StreamWriter objSW = new StreamWriter(objCS)) { objSW.Write(plainText); } cipherBytes = objMS.ToArray(); } } } return cipherBytes; } /// <summary> /// returns string from cipher bytes /// </summary> /// <param name="cipherBytes"></param> /// <returns>plain text</returns> private string DecryptBytes(Byte[] cipherBytes,byte[] key1,byte[] iv1) { if (cipherBytes == null || cipherBytes.Length <= 0) throw new ArgumentNullException("cipherBytes"); string plainText = ""; using (RijndaelManaged objRM = new RijndaelManaged()) { objRM.Key = key1; objRM.IV = iv1; objRM.Padding = PaddingMode.None; ICryptoTransform decryptor = objRM.CreateDecryptor(objRM.Key, objRM.IV); using (MemoryStream objMS = new MemoryStream(cipherBytes)) { using (CryptoStream objCS = new CryptoStream(objMS, decryptor, CryptoStreamMode.Read))

{ using (StreamReader objSR = new StreamReader(objCS)) { plainText = objSR.ReadToEnd(); } } } } return plainText; } /// <summary> /// public method that returns encrypted text /// </summary> /// <param name="plainText"></param> /// <returns></returns> public string AESEncryption(string plainText) { if (plainText == "" || plainText.Length <= 0) throw new ArgumentNullException("plainText"); byte[] cipherBytes = EncryptText(plainText); StringBuilder cipherText = new StringBuilder(cipherBytes.Length); foreach (byte b in cipherBytes) cipherText.Append(b.ToString("X2")); return cipherText.ToString(); } /// <summary> /// public method that returns decrypted text /// </summary> /// <param name="str"></param> /// <returns></returns> public string AESDecryption(string cipherText,byte[] key,byte [] iv) { if (cipherText == "" || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); byte[] b = new byte[cipherText.Length / 2]; string[] hexValues = new string[cipherText.Length / 2]; for (int i = 0, j = 0; i < cipherText.Length / 2; j += 2) { hexValues[i] = cipherText.Substring(j, 2); i++; } for (int i = 0; i < hexValues.Length; i++) { b[i] = Convert.ToByte(hexValues[i], 16); } return DecryptBytes(b,key,iv); } } }

You might also like