/// <exception cref="CryptographicException">The value of the <see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Mode" /> parameter is not <see cref="F:System.Security.Cryptography.CipherMode.ECB" />, <see cref="F:System.Security.Cryptography.CipherMode.CBC" />, or <see cref="F:System.Security.Cryptography.CipherMode.CFB" />.</exception> public static string Encrypt(string inputText, ISymCryptKey cryptKey) { if (inputText == null) { throw new ArgumentNullException("inputText"); } if (cryptKey == null) { throw new ArgumentNullException("cryptKey"); } using (var rijndaelCipher = new RijndaelManaged()) { var plainText = Encoding.Unicode.GetBytes(inputText); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(cryptKey.Key, cryptKey.Salt)) using (var memoryStream = new MemoryStream()) using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); var cipherBytes = memoryStream.ToArray(); return Convert.ToBase64String(cipherBytes); } } }
/// <exception cref="CryptographicException">The value of the <see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Mode" /> parameter is not <see cref="F:System.Security.Cryptography.CipherMode.ECB" />, <see cref="F:System.Security.Cryptography.CipherMode.CBC" />, or <see cref="F:System.Security.Cryptography.CipherMode.CFB" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="s" /> is null. </exception> /// <exception cref="FormatException">The length of <paramref name="s" />, ignoring white-space characters, is not zero or a multiple of 4. -or-The format of <paramref name="s" /> is invalid. <paramref name="s" /> contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.</exception> public static string Decrypt(string inputText, ISymCryptKey cryptKey) { if (inputText == null) { throw new ArgumentNullException("inputText"); } if (cryptKey == null) { throw new ArgumentNullException("cryptKey"); } using (var rijndaelCipher = new RijndaelManaged()) { var encryptedData = Convert.FromBase64String(inputText); using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(cryptKey.Key, cryptKey.Salt)) using (var memoryStream = new MemoryStream(encryptedData)) using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { var plainText = new byte[encryptedData.Length]; int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length); return Encoding.Unicode.GetString(plainText, 0, decryptedCount); } } }
/// <summary> /// The decrypt. /// </summary> /// <param name="InputText"> /// The input text. /// </param> /// <param name="cryptKey"> /// The crypt Key. /// </param> /// <returns> /// The decrypt. /// </returns> public static string Decrypt(string InputText, ISymCryptKey cryptKey) { using (var rijndaelCipher = new RijndaelManaged()) { byte[] encryptedData = Convert.FromBase64String(InputText); using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(cryptKey.Key, cryptKey.Salt)) { using (var memoryStream = new MemoryStream(encryptedData)) { using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { var plainText = new byte[encryptedData.Length]; int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length); return(Encoding.Unicode.GetString(plainText, 0, decryptedCount)); } } } } }
/// <summary> /// The encrypt. /// </summary> /// <param name="inputText"> /// The input text. /// </param> /// <param name="cryptKey"> /// The crypt Key. /// </param> /// <returns> /// The encrypt. /// </returns> public static string Encrypt(string inputText, ISymCryptKey cryptKey) { using (var rijndaelCipher = new RijndaelManaged()) { byte[] plainText = Encoding.Unicode.GetBytes(inputText); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(cryptKey.Key, cryptKey.Salt)) { using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); byte[] cipherBytes = memoryStream.ToArray(); return(Convert.ToBase64String(cipherBytes)); } } } } }