/// <summary> /// Encrypt a string with the specified password, symmetric and hash algorithms and IV. /// A key is generated from the password using specified IV and algorithms. /// /// The IV can be inserted at the beginning of the encrypted string. If you do not include IV, /// you have to share and/or store password and IV to be able to decrypt string later. /// </summary> /// <param name="unicodeValue">Unicode string</param> /// <param name="password">Password</param> /// <param name="algorithm">Symmetric algorithm</param> /// <param name="hash">Hash algorithm</param> /// <param name="iv">IV</param> /// <param name="includeIV">True to include IV in encrypted string</param> /// <returns>Base64 encrypted string</returns> public static string Encrypt(string unicodeValue, string password, SymmAlgorithm algorithm, HashAlgorithm hash, byte[] iv, bool includeIV) { if (string.IsNullOrEmpty(unicodeValue)) { throw new ArgumentNullException("unicodeValue"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } string encrypted = string.Empty; string algName = Enum.GetName(typeof(SymmAlgorithm), algorithm); using (SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algName)) { if (provider != null) { provider.Key = GenerateKeyFromPassword(password, algorithm, provider.KeySize, hash, iv); provider.IV = iv; encrypted = EncryptTransform(provider, unicodeValue, includeIV); } } return(encrypted); }
/// <summary> /// Return the required symmetric algorithm to encrypt/decrypt data. /// This method allow you to control each parameter of encryption. /// /// You have to define your own Key and IV. By default the provider /// generate random key and IV. Provider methods GenerateKey and GenerateIV /// can be used to generate new values. Alternatively, the class static /// method GenerateKeyFromPassword can be used to generate key from password. /// /// You should clear and dispose the provider after use. /// </summary> /// <param name="algorithm">Algorithm to create</param> /// <returns>An instance of the desired algorithm</returns> public static SymmetricAlgorithm GetProvider(SymmAlgorithm algorithm) { // Create a crypto service provider from the algorithm name string algName = Enum.GetName(typeof(SymmAlgorithm), algorithm); return(SymmetricAlgorithm.Create(algName)); }
/// <summary> /// Decrypt a string with the specified password, symmetric and hash algorithms. /// A key is generated from the password using the specified algorithms. /// The IV is extracted from the beginning of the encrypted string. /// </summary> /// <param name="base64Value">Base64 encrypted string</param> /// <param name="password">Password</param> /// <param name="algorithm">Symmetric algorithm</param> /// <param name="hash">Hash algorithm</param> /// <returns>Decrypted unicode string</returns> public static string Decrypt(string base64Value, string password, SymmAlgorithm algorithm, HashAlgorithm hash) { if (string.IsNullOrEmpty(base64Value)) { throw new ArgumentNullException("base64Value"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } return(Decrypt(base64Value, password, algorithm, hash, null, true)); }
/// <summary> /// Generate a key from a password with the specified /// algorithms, key size and IV. /// </summary> /// <param name="password">Password</param> /// <param name="algorithm">Symmetric algorithm</param> /// <param name="keySize">Symmetric key size</param> /// <param name="hash">Hash algorithm</param> /// <param name="iv">IV</param> /// <returns>Key</returns> public static byte[] GenerateKeyFromPassword(string password, SymmAlgorithm algorithm, int keySize, HashAlgorithm hash, byte[] iv) { if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } string algName = Enum.GetName(typeof(SymmAlgorithm), algorithm); string hashName = Enum.GetName(typeof(HashAlgorithm), hash); // Generate a key from params PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null); byte[] key = pdb.CryptDeriveKey(algName, hashName, keySize, iv); return(key); }
/// <summary> /// Decrypt a string with the specified password, symmetric and hash algorithms and IV. /// A key is generated from the password using specified algorithms and IV. /// /// The IV can be extracted from the beginning of the encrypted string. /// Otherwise, you have to specify the same IV used to encrypt this data. /// </summary> /// <param name="base64Value">Base64 encrypted string</param> /// <param name="password">Password</param> /// <param name="algorithm">Symmetric algorithm</param> /// <param name="hash">Hash algorithm</param> /// <param name="iv">IV</param> /// <param name="includeIV">True to extract IV from encrypted string</param> /// <returns>Decrypted unicode string</returns> public static string Decrypt(string base64Value, string password, SymmAlgorithm algorithm, HashAlgorithm hash, byte[] iv, bool includeIV) { if (string.IsNullOrEmpty(base64Value)) { throw new ArgumentNullException("base64Value"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } if (iv == null || iv.Length == 0) { throw new ArgumentNullException("iv"); } string decrypted = string.Empty; string algName = Enum.GetName(typeof(SymmAlgorithm), algorithm); using (SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algName)) { if (provider != null) { byte[] buffer; if (includeIV) { Extract(base64Value, out iv, provider.BlockSize / 8, out buffer); provider.IV = iv; provider.Key = GenerateKeyFromPassword(password, algorithm, provider.KeySize, hash, iv); } else { provider.IV = iv; provider.Key = GenerateKeyFromPassword(password, algorithm, provider.KeySize, hash, iv); buffer = Convert.FromBase64String(base64Value); } decrypted = DecryptTransform(provider, buffer); } } return(decrypted); }