public async Task <byte[]> EncryptAsync(EncryptionKey key) { using var destinationMs = new MemoryStream(); using var sourceMs = new MemoryStream(key.Serialize()); await StaticEncryptor.EncryptAsync(sourceMs, destinationMs, _key).ConfigureAwait(false); return(destinationMs.ToArray()); }
public async Task <byte[]> EncryptAsync(EncryptionKey key) { using var destinationMs = new MemoryStream(); using var sourceMs = new MemoryStream(key.Serialize()); //we need to generate another IV to avoid encrypting always with the very same value. await StaticEncryptor.EncryptAsync(sourceMs, destinationMs, _key).ConfigureAwait(false); return(destinationMs.ToArray()); }
public async Task CanEncryptAndDecryptStreamWithGenericKey() { const string content = "this test will be encrypted"; byte[] stringContent = Encoding.UTF8.GetBytes(content); using var sourceStream = new MemoryStream(stringContent); using var encryptedStream = new MemoryStream(); using var key = new AesEncryptionKey(); await StaticEncryptor.EncryptAsync(sourceStream, encryptedStream, key).ConfigureAwait(false); //Now decrypt var decryptedMemoryStream = new MemoryStream(); var readingEncryptedStream = new MemoryStream(encryptedStream.ToArray()); await StaticEncryptor.DecryptAsync(readingEncryptedStream, decryptedMemoryStream, key).ConfigureAwait(false); var decryptedString = Encoding.UTF8.GetString(decryptedMemoryStream.ToArray()); Assert.Equal(decryptedString, content); }