/// <summary> /// Initializes a new instance of the <see cref="AuthenticatedEncryptionProvider"/> class used for encryption and decryption. /// </summary> /// <param name="key">The <see cref="SecurityKey"/> that will be used for crypto operations.</param> /// <param name="algorithm">The encryption algorithm to apply.</param> /// <exception cref="ArgumentNullException">'key' is null.</exception> /// <exception cref="ArgumentNullException">'algorithm' is null or whitespace.</exception> /// <exception cref="ArgumentOutOfRangeException">key size is not large enough.</exception> /// <exception cref="ArgumentException">'algorithm' is not supported.</exception> /// <exception cref="ArgumentException">a symmetricSignatureProvider is not created.</exception> public AuthenticatedEncryptionProvider(SecurityKey key, string algorithm) { if (key == null) { throw LogHelper.LogArgumentNullException(nameof(key)); } if (string.IsNullOrWhiteSpace(algorithm)) { throw LogHelper.LogArgumentNullException(nameof(algorithm)); } Key = key; Algorithm = algorithm; _cryptoProviderFactory = key.CryptoProviderFactory; if (SupportedAlgorithms.IsSupportedEncryptionAlgorithm(algorithm, key)) { if (SupportedAlgorithms.IsAesGcm(algorithm)) { #if NETSTANDARD2_0 if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { throw LogHelper.LogExceptionMessage(new PlatformNotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10713, LogHelper.MarkAsNonPII(algorithm)))); } #endif InitializeUsingAesGcm(); } else { InitializeUsingAesCbc(); } } else { throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX10668, LogHelper.MarkAsNonPII(_className), LogHelper.MarkAsNonPII(algorithm), key))); } }
/// <summary> /// Checks if an 'key, algorithm' pair is supported /// </summary> /// <param name="key">the <see cref="SecurityKey"/></param> /// <param name="algorithm">the algorithm to check.</param> /// <returns>true if 'key, algorithm' pair is supported.</returns> protected virtual bool IsSupportedAlgorithm(SecurityKey key, string algorithm) { return(SupportedAlgorithms.IsSupportedEncryptionAlgorithm(algorithm, key)); }