ReadAsync() public method

public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
return Task
Ejemplo n.º 1
0
 public async static Task<string> DecryptAsync(string cipherText, string passPhrase)
 {
     var cipherTextBytes = Convert.FromBase64String(cipherText);
     using (var password = new Rfc2898DeriveBytes(passPhrase, InitVectorBytes))
     {
         var keyBytes = password.GetBytes(Keysize / 8);
         using (var symmetricKey = new RijndaelManaged())
         {
             symmetricKey.Mode = CipherMode.CBC;
             symmetricKey.Padding = PaddingMode.PKCS7;
             using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, InitVectorBytes))
             {
                 using (var memoryStream = new MemoryStream(cipherTextBytes))
                 {
                     using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                     {
                         var plainTextBytes = new byte[cipherTextBytes.Length];
                         var decryptedByteCount = await cryptoStream.ReadAsync(plainTextBytes, 0, plainTextBytes.Length);
                         return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public static async Task<string> DecryptStringAsync(string encryptedData, byte[] key, byte[] iv, CancellationToken cancellationToken = default(CancellationToken))
 {
     var cipherTextBytes = Convert.FromBase64String(encryptedData);
     var plainTextBytes = new byte[cipherTextBytes.Length];
     using (var rijndael = Rijndael.Create())
     using (var memoryStream = new MemoryStream(cipherTextBytes))
     using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read))
     {
         var decryptedByteCount = await cryptoStream.ReadAsync(plainTextBytes, 0, plainTextBytes.Length, cancellationToken).ConfigureAwait(false);
         memoryStream.Close();
         cryptoStream.Close();
         rijndael.Clear();
         return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
     }
 }
Ejemplo n.º 3
0
        public async Task<bool> ValidateHash(byte[] expectedHash)
        {
            var hashAlgorithm = MD5.Create();

            using (var fileStream = File.OpenRead(_storage.FilePath))
            {
                using (var cs = new CryptoStream(fileStream, hashAlgorithm, CryptoStreamMode.Read))
                {
                    for (;;)
                    {
                       var bytesRead = await cs.ReadAsync(_buffer, 0, _buffer.Length);
                       if (bytesRead == 0) break;
                    }  
                }
                
                return HashesAreEqual(expectedHash, hashAlgorithm.Hash);
            }
        }
Ejemplo n.º 4
0
        private async static Task DecryptFile(string inputFilename, string outputFilename, string password)
        {
            var buffer = new byte[1024*1024*1];
            var sha256 = SHA256.Create();
            var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));

            using (FileStream inputRaw = new FileStream(inputFilename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                BinaryReader br = new BinaryReader(inputRaw);
                var ivl = br.ReadInt32();
                var iv = br.ReadBytes(ivl);

                using (FileStream output = new FileStream(outputFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                {
                    var aes = Aes.Create();
                    var decryptor = aes.CreateDecryptor(hash, iv);
                    using (var input = new CryptoStream(inputRaw, decryptor, CryptoStreamMode.Read))
                    {
                        int bytesRead;

                        do
                        {
                            bytesRead = await input.ReadAsync(buffer, 0, buffer.Length);
                            if (bytesRead > 0)
                            {
                                await output.WriteAsync(buffer, 0, bytesRead);
                            }
                        }
                        while (bytesRead > 0);
                    }
                }
            }
        }