public byte[] RijEncryptString(string plainText, byte[] key, byte[] iv) { byte[] encrypted; using (Rijndael rijAlg = Rijndael.Create()) { rijAlg.Key = key; rijAlg.IV = iv; ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //write data to the stream swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } //returns the encrypted bytes from the memory stream return encrypted; } public string RijDecryptString(byte[] cipherText, byte[] key, byte[] iv) { string plainText = string.Empty; using (Rijndael rijAlg = Rijndael.Create()) { rijAlg.Key = key; rijAlg.IV = iv; //creates a decryptor to preform the stream transform ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { //read the decrypted bytes and put them in a string plainText = srDecrypt.ReadToEnd(); } } } //return the decrypted text return plainText; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)