public string Decrypt(string passCode, EncryptedValue data) { byte[] salt = Convert.FromBase64String(data.Base64Salt); byte[] iv = Convert.FromBase64String(data.Base64IV); byte[] key = null; byte[] encrypted = Convert.FromBase64String(data.Base64EncryptedValue); try { Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(passCode, salt); key = k1.GetBytes(16); using (var rijndael = new RijndaelManaged() { Mode = CipherMode.CBC , IV = iv , Key = key , Padding = PaddingMode.PKCS7 }) { using (var decryptor = rijndael.CreateDecryptor(key, iv)) using (var memoryStream = new MemoryStream(encrypted)) using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) using (var writer = new StreamReader(cryptoStream)) { //writer.Write(encrypted); //writer.Flush(); //cryptoStream.Flush(); //cryptoStream.FlushFinalBlock(); //return Convert.ToBase64String(memoryStream.ToArray()); return(writer.ReadToEnd()); } } } catch (Exception) { throw; } finally { ClearBytes(salt); ClearBytes(iv); ClearBytes(key); ClearBytes(encrypted); } }
public string Decrypt(string passCode, EncryptedValue data) { byte[] salt = Convert.FromBase64String(data.Base64Salt); byte[] iv = Convert.FromBase64String(data.Base64IV); byte[] key = null; byte[] encrypted = Convert.FromBase64String(data.Base64EncryptedValue); try { Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(passCode, salt); key = k1.GetBytes(16); using (var rijndael = new RijndaelManaged() { Mode = CipherMode.CBC , IV = iv , Key = key , Padding = PaddingMode.PKCS7 }) { using (var decryptor = rijndael.CreateDecryptor(key, iv)) using (var memoryStream = new MemoryStream(encrypted)) using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) using (var writer = new StreamReader(cryptoStream)) { //writer.Write(encrypted); //writer.Flush(); //cryptoStream.Flush(); //cryptoStream.FlushFinalBlock(); //return Convert.ToBase64String(memoryStream.ToArray()); return writer.ReadToEnd(); } } } catch (Exception) { throw; } finally { ClearBytes(salt); ClearBytes(iv); ClearBytes(key); ClearBytes(encrypted); } }