/// <summary> /// Initializes a new instance of the <see cref="ASymmetricEncryption"/> class. /// </summary> /// <param name="key"> /// The private key that will be used to decrypt the password used to decrypt the password. /// </param> /// <param name="password">The password to use during the symmetric part of the encryption.</param> /// <exception cref="ArgumentNullException"> /// password - A password must be provided! or key - An RSA public key must be provided. /// </exception> public ASymmetricEncryption(RsaPublicKey key, EncryptionData password) { password = Check.NotNull(password, nameof(password)); if (password.IsEmpty) { throw new ArgumentNullException(nameof(password), "A password must be provided!"); } if (password.Text.Length > 32) { throw new ArgumentOutOfRangeException( nameof(password), "RSA Encryption limits the password to 32 characters"); } _publicKey = key ?? throw new ArgumentNullException( nameof(key), "An RSA private key must be provided!"); _password = Equals(password.EncodingToUse, Encoding.UTF8) ? password : new EncryptionData(password.Text, Encoding.UTF8); EncryptedPassword = new RsaEncryption().Encrypt(_password, _publicKey); }
/// <summary> /// Initializes a new instance of the <see cref="ASymmetricEncryption"/> class. /// </summary> /// <param name="key"> /// The private key that will be used to decrypt the password used to decrypt the password. /// </param> /// <exception cref="ArgumentNullException">An RSA public key must be provided.</exception> public ASymmetricEncryption(RsaPublicKey key) { _publicKey = key ?? throw new ArgumentNullException( nameof(key), "An RSA public key must be provided!"); var random = CryptoRandomNumber.Next(); var generator = new PasswordGenerator(random) { IncludeExtended = false }; _password = new EncryptionData(generator.Generate(32)); EncryptedPassword = new RsaEncryption().Encrypt(_password, _publicKey); }