public async Task <EncryptionKey> DecriptAsync(byte[] encryptedKey) { using var sourceMs = new MemoryStream(encryptedKey); using var destinationMs = new MemoryStream(); await StaticEncryptor.DecryptAsync(sourceMs, destinationMs, _key).ConfigureAwait(false); return(EncryptionKey.CreateFromSerializedVersion(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); }