/// <summary> /// Simple Encryption (AES) then Authentication (HMAC) of a UTF8 message /// using Keys derived from a Password (PBKDF2). /// </summary> /// <param name="secretMessage">The secret message.</param> /// <param name="password">The password.</param> /// <param name="nonSecretPayload">The non secret payload.</param> /// <returns> /// Encrypted Message /// </returns> /// <exception cref="System.ArgumentException">password</exception> /// <remarks> /// Significantly less secure than using random binary keys. /// Adds additional non secret payload for key generation parameters. /// </remarks> public static string SimpleEncryptWithPassword(string secretMessage, string password, byte[] nonSecretPayload = null) { ExceptionCore ex = new ExceptionCore(); if (string.IsNullOrEmpty(secretMessage)) { //ex.EncryptException("Dados para encryptação é NULL", 1); throw new ArgumentException("Secret Message Required!", "secretMessage"); } var plainText = Encoding.UTF8.GetBytes(secretMessage); var cipherText = SimpleEncryptWithPassword(plainText, password, nonSecretPayload); return(Convert.ToBase64String(cipherText)); }
/// <summary> /// Simple Authentication (HMAC) and then Descryption (AES) of a UTF8 Message /// using keys derived from a password (PBKDF2). /// </summary> /// <param name="encryptedMessage">The encrypted message.</param> /// <param name="password">The password.</param> /// <param name="nonSecretPayloadLength">Length of the non secret payload.</param> /// <returns> /// Decrypted Message /// </returns> /// <exception cref="System.ArgumentException">Encrypted Message Required!;encryptedMessage</exception> /// <remarks> /// Significantly less secure than using random binary keys. /// </remarks> public static string SimpleDecryptWithPassword(string encryptedMessage, string password, int nonSecretPayloadLength = 0) { ExceptionCore ex = new ExceptionCore(); if (string.IsNullOrWhiteSpace(encryptedMessage)) { ex.EncryptException("Dados para decodificação é NULL", 2);//throw new ArgumentException("Encrypted Message Required!", "encryptedMessage"); } var cipherText = Convert.FromBase64String(encryptedMessage); var plainText = SimpleDecryptWithPassword(cipherText, password, nonSecretPayloadLength); return(plainText == null ? null : Encoding.UTF8.GetString(plainText)); }