Esempio n. 1
0
    /// <summary>
    /// Encrypt <paramref name="value"/> with AES Algorithm and key.
    /// !!! Milvasoft Corporation is not responsible of possible data loss.
    /// </summary>
    /// <param name="value"></param>
    public async Task <string> EncryptAsync(string value)
    {
        byte[] inputValue = Encoding.UTF8.GetBytes(value);

        using var aesProvider = CreateCryptographyProvider();

        aesProvider.GenerateIV();

        byte[] initializationVector = aesProvider.IV;

        using ICryptoTransform encryptor = aesProvider.CreateEncryptor(_key, initializationVector);

        using var memoryStream = new MemoryStream();

        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
            await memoryStream.WriteAsync(initializationVector.AsMemory(0, initializationVector.Length)).ConfigureAwait(false);

            await cryptoStream.WriteAsync(inputValue.AsMemory(0, inputValue.Length)).ConfigureAwait(false);

            await cryptoStream.FlushFinalBlockAsync().ConfigureAwait(false);
        }

        return(Convert.ToBase64String(memoryStream.ToArray()));
    }
Esempio n. 2
0
        public static async Task <Stream> Encrypt(string password, Stream content)
        {
            var bytes = Encoding.UTF8.GetBytes($"{password}");
            var hash  = SHA256.HashData(bytes);

            var resultStream = new MemoryStream();

            using (var aes = Aes.Create())
            {
                aes.Key     = hash;
                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using var cryptoTransform       = aes.CreateEncryptor(aes.Key, aes.IV);
                await using var encryptedStream = new MemoryStream();
                await using var cryptoStream    = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
                await content.CopyToAsync(cryptoStream);

                await content.DisposeAsync();

                await cryptoStream.FlushFinalBlockAsync();

                encryptedStream.Position = 0;

                await resultStream.WriteAsync(aes.IV);

                await encryptedStream.CopyToAsync(resultStream);
            }

            resultStream.Position = 0;
            return(resultStream);
        }
Esempio n. 3
0
 public ValueTask FlushFinalBlockAsync()
 {
     if (cryptoStream != null)
     {
         return(cryptoStream.FlushFinalBlockAsync());
     }
     return(ValueTask.CompletedTask);
 }
Esempio n. 4
0
        public static async Task FlushFinalBlockAsync_Canceled()
        {
            ICryptoTransform encryptor = new IdentityTransform(1, 1, true);

            using (MemoryStream output = new MemoryStream())
                using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
                {
                    await encryptStream.WriteAsync(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);

                    ValueTask waitable = encryptStream.FlushFinalBlockAsync(new Threading.CancellationToken(canceled: true));
                    Assert.True(waitable.IsCanceled);
                    Assert.False(encryptStream.HasFlushedFinalBlock);
                }
        }
Esempio n. 5
0
        public static async Task FlushFinalBlockAsync()
        {
            ICryptoTransform encryptor = new IdentityTransform(1, 1, true);

            using (MemoryStream output = new MemoryStream())
                using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
                {
                    await encryptStream.WriteAsync(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);

                    await encryptStream.FlushFinalBlockAsync();

                    Assert.True(encryptStream.HasFlushedFinalBlock);
                    Assert.Equal(5, output.ToArray().Length);
                }
        }
Esempio n. 6
0
        public static async Task <Stream> EncryptAndBase64(string password, Stream content)
        {
            await using var encryptedBytes = await Encrypt(password, content);

            using var transformer = new ToBase64Transform();
            var destination = new MemoryStream();

            await using var ms           = new MemoryStream();
            await using var cryptoStream = new CryptoStream(ms, transformer, CryptoStreamMode.Write);
            await encryptedBytes.CopyToAsync(cryptoStream);

            await cryptoStream.FlushFinalBlockAsync();

            ms.Position = 0;
            await ms.CopyToAsync(destination);

            destination.Position = 0;
            return(destination);
        }