/// <summary> /// Decrypts a string that was encrypted using RijndalAES algorithm with the specified post-encryption encoding. /// </summary> /// <param name="data">Encrypted data generated from EncryptData method.</param> /// <param name="password">Password used to decrypt the string.</param> /// <param name="characterEncoding"> /// Determines what the post encryption encoding of <paramref name="data"/> is. /// When encrypting, this value needs to be exactly the same as used for decryption. /// </param> /// <param name="applySalt"> /// Determines whether the key is salted (transformed) before being used for extra security against brute force attacks. /// When encrypting, this value needs to be exactly the same as used for decryption. /// </param> /// <returns>Decrypted string.</returns> /// <exception cref="ArgumentNullException">If <paramref name="data"/> or <paramref name="password"/> is null.</exception> /// <exception cref="Exception">If the decryption fails for any reason.</exception> public static string Decrypt(string password, string data, Encoding.CharacterEncoding characterEncoding, bool applySalt) { if (data == null) { throw new ArgumentNullException("data"); } if (password == null) { throw new ArgumentNullException("password"); } string decodedString = null; try { byte[] encBytes; switch (characterEncoding) { case Encoding.CharacterEncoding.Base64: encBytes = Convert.FromBase64String(data); break; case Encoding.CharacterEncoding.Hex: encBytes = HexUtil.GetBytes(data); break; default: encBytes = System.Text.Encoding.GetEncoding(1252).GetBytes(data); break; } byte[] decBytes = DecryptData(encBytes, password, PaddingMode.ISO10126, applySalt); decodedString = System.Text.Encoding.GetEncoding(1252).GetString(decBytes); //When salting, remove the datetime we added at the end of the string. if (applySalt) { decodedString = decodedString.Substring(0, decodedString.Length - 19); } } catch (Exception ex) { throw new Exception("The encrypted string was not in a valid format.", ex); } return(decodedString); }
/// <summary> /// Use AES to encrypt data string and optionally encode it afterwards. /// The same <paramref name="password"/> and <see cref="Salt"/> must be used to decrypt the string. /// </summary> /// <param name="data">Clear string to encrypt.</param> /// <param name="password">Password used to encrypt the string.</param> /// <param name="characterEncoding"> /// Determines what the post encryption encoding of <paramref name="data"/> is. /// Setting this value to anything but <see cref="CharacterEncoding.None"/> will encode the resulting /// encrypted value with the specified encoding. /// When encrypting, this value needs to be exactly the same as used for decryption. /// </param> /// <param name="applySalt"> /// Determines whether the key is salted (transformed) before being used for extra security against brute force attacks. /// When decrypting, this value needs to be exactly the same as used for encryption. /// </param> /// <returns>Encrypted result encode as specified in <paramref name="characterEncoding"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="data"/> or <paramref name="password"/> is null.</exception> public static string Encrypt(string password, string data, Encoding.CharacterEncoding characterEncoding, bool applySalt) { if (data == null) { throw new ArgumentNullException("data"); } if (password == null) { throw new ArgumentNullException("password"); } //In addition to applying a salt value, we're also changing the data by appending the datetime. This will be truncated //when decrypting but just makes the encryption a bit more random. if (applySalt) { data += DateTime.Now.ToString("dd MM yyyy HH:mm:ss"); } byte[] encBytes = EncryptData(System.Text.Encoding.GetEncoding(1252).GetBytes(data), password, PaddingMode.ISO10126, applySalt); string retVal = string.Empty; switch (characterEncoding) { case Encoding.CharacterEncoding.Base64: retVal = Convert.ToBase64String(encBytes); break; case Encoding.CharacterEncoding.Hex: retVal = HexUtil.ToString(encBytes); break; default: retVal = System.Text.Encoding.GetEncoding(1252).GetString(encBytes); break; } return(retVal); }