public ArgonAesEncryptionProvider(string passphrase, string salt, EncryptionMethod method, EncryptionOptions options = null) { Guard.AgainstNull(passphrase, nameof(passphrase)); Guard.AgainstNull(salt, nameof(salt)); _method = method; _options = options; _argonHashKey = ArgonHash.GetHashKeyAsync(passphrase, salt, Encryption.Constants.Aes256.KeySize, _options?.HashOptions).GetAwaiter().GetResult(); }
public static byte[] Aes256Decrypt(ReadOnlyMemory <byte> encryptedData, ReadOnlyMemory <byte> key, EncryptionOptions options = null) { if (key.Length != Constants.Aes256.KeySize || encryptedData.Length == 0) { return(null); } using var cipherStream = new MemoryStream(encryptedData.ToArray()); using var cipherReader = new BinaryReader(cipherStream); var nonce = cipherReader.ReadBytes(options?.NonceSize ?? Constants.Aes256.NonceSize); var cipher = new GcmBlockCipher(new AesEngine()); var parameters = new AeadParameters(new KeyParameter(key.ToArray()), options?.MacBitSize ?? Constants.Aes256.MacBitSize, nonce); cipher.Init(false, parameters); var cipherText = cipherReader.ReadBytes(encryptedData.Length - nonce.Length); var plainText = new byte[cipher.GetOutputSize(cipherText.Length)]; try { cipher.DoFinal(plainText, cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0)); } catch (InvalidCipherTextException) { return(null); } return(plainText); }
public static byte[] Aes256Encrypt(ReadOnlyMemory <byte> data, ReadOnlyMemory <byte> key, EncryptionOptions options = null) { if (key.Length != Constants.Aes256.KeySize || data.Length == 0) { return(null); } var nonce = new byte[options?.NonceSize ?? Constants.Aes256.NonceSize]; Random.NextBytes(nonce); var cipher = new GcmBlockCipher(new AesEngine()); cipher.Init(true, new AeadParameters(new KeyParameter(key.ToArray()), options?.MacBitSize ?? Constants.Aes256.MacBitSize, nonce)); var cipherText = new byte[cipher.GetOutputSize(data.Length)]; cipher.DoFinal(cipherText, cipher.ProcessBytes(data.ToArray(), 0, data.Length, cipherText, 0)); using var cs = new MemoryStream(); using (var bw = new BinaryWriter(cs)) { bw.Write(nonce); bw.Write(cipherText); } return(cs.ToArray()); }
public static byte[] Decrypt(ReadOnlyMemory <byte> data, EncryptionMethod method, ReadOnlyMemory <byte> hashKey, EncryptionOptions options = null) { GuardAgainstBadHashKey(method, hashKey); return(method switch { EncryptionMethod.AES256_ARGON2ID => AesEncrypt.Aes256Decrypt(data, hashKey, options), _ => AesEncrypt.Aes256Decrypt(data, hashKey) });