public static EncryptString ( string plaintext, string passphrase ) : string | ||
plaintext | string | |
passphrase | string | |
return | string |
public static string EncryptString(string plaintext, string passphrase)
{
try
{
var salt = GenerateRandomBytes(SaltLength); // Random salt
var iv = GenerateRandomBytes(16); // AES is always a 128-bit block size
var key = DeriveKeyFromPassphrase(passphrase, salt); // Derive the key from the passphrase
// Encrypt
var ciphertext = DoCryptoOperation(Encoding.UTF8.GetBytes(plaintext), key, iv, true);
// Return the formatted string
return String.Format("{0}:{1}:{2}", Convert.ToBase64String(iv), Convert.ToBase64String(salt), Convert.ToBase64String(ciphertext));
}
catch (Exception ex)
{
Utilities.OnError(Utilities.GetCurrentMethod(), ex);
}
return "";
}
/// <summary> /// Encrypt some text using AES encryption and a password key. /// The encrypted text can be saved to a file. /// Note that if you forget the password there is NO WAY to decrypt! /// </summary> /// <param name="source">The text to encrypt.</param> /// <param name="password">The password key for the encryption.</param> /// <returns>The encrypted text (cypher).</returns> public static Primitive AESEncrypt(Primitive source, Primitive password) { return(StringEncryption.EncryptString(source, password)); }