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 <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 Can_Encrypt_And_Decrypt_bytes_with_password() { const string content = "this test will be encrypted"; byte[] stringContent = Encoding.UTF8.GetBytes(content); var encrypted = await StaticEncryptor.AesEncryptWithPasswordAsync( stringContent, Password).ConfigureAwait(false); //Now decrypt var decrypted = await StaticEncryptor.AesDecryptWithPasswordAsync( encrypted, Password).ConfigureAwait(false); Assert.Equal(stringContent, decrypted); }
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); }
public FolderBasedKeyValueStore( string keyMaterialFolderStore, string password) { InternalUtils.EnsureDirectory(keyMaterialFolderStore); var keyName = Path.Combine(keyMaterialFolderStore, "1.key"); if (!File.Exists(keyName)) { //create the first key _key = EncryptionKey.CreateDefault(); var serializedKey = _key.Serialize(); var encryptedSerializedKey = StaticEncryptor.AesEncryptWithPasswordAsync(serializedKey, password).Result; File.WriteAllBytes(keyName, encryptedSerializedKey); } else { var encryptedSerializedKey = File.ReadAllBytes(keyName); var serializedKey = StaticEncryptor.AesDecryptWithPasswordAsync(encryptedSerializedKey, password).Result; _key = EncryptionKey.CreateFromSerializedVersion(serializedKey); } }
public async Task Can_Encrypt_And_Decrypt_Stream_with_password() { 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(); await StaticEncryptor.AesEncryptWithPasswordAsync( sourceStream, encryptedStream, Password).ConfigureAwait(false); //Now decrypt var decryptedMemoryStream = new MemoryStream(); var readingEncryptedStream = new MemoryStream(encryptedStream.ToArray()); await StaticEncryptor.AesDecryptWithPasswordAsync( readingEncryptedStream, decryptedMemoryStream, Password).ConfigureAwait(false); var decryptedString = Encoding.UTF8.GetString(decryptedMemoryStream.ToArray()); Assert.Equal(decryptedString, content); }