public byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] initVector) { var provider = _symmetricKeyAlgorithmProviderFactory.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7); var internalKey = provider.CreateSymmetricKey(key); byte[] iv = initVector; // this is optional, but must be the same for both encrypting and decrypting byte[] cipherText = _cryptographicEngine.Encrypt(internalKey, Encoding.UTF8.GetBytes(plainText), iv); return(cipherText); }
/// <summary>Returns a crypto key management for a specified algorithm.</summary> /// <param name="factory">The factory.</param> /// <param name="algorithm">The algorithm.</param> /// <returns>An instance of <see cref="ISymmetricKeyAlgorithmProvider"/>.</returns> public static ISymmetricKeyAlgorithmProvider OpenAlgorithm(this ISymmetricKeyAlgorithmProviderFactory factory, SymmetricAlgorithm algorithm) { Requires.NotNull(factory, nameof(factory)); return(factory.OpenAlgorithm(algorithm.GetName(), algorithm.GetMode(), algorithm.GetPadding())); }