当前位置: 首页>>代码示例>>C#>>正文


C# CryptoStream.ReadAsync方法代码示例

本文整理汇总了C#中System.Security.Cryptography.CryptoStream.ReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.ReadAsync方法的具体用法?C# CryptoStream.ReadAsync怎么用?C# CryptoStream.ReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.CryptoStream的用法示例。


在下文中一共展示了CryptoStream.ReadAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DecryptAsync

 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);
                     }
                 }
             }
         }
     }
 }
开发者ID:xsysfan,项目名称:ussd-framework,代码行数:25,代码来源:StringCipher.cs

示例2: DecryptStringAsync

 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);
     }
 }
开发者ID:CamiloBernal,项目名称:RijndaelCSharpDemo,代码行数:15,代码来源:Program.cs

示例3: ValidateHash

        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);
            }
        }
开发者ID:klym1,项目名称:ex.ua-client,代码行数:18,代码来源:IntegrityValidator.cs

示例4: DecryptFile

        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);
                    }
                }
            }
        }
开发者ID:clemensv,项目名称:EncryptFile,代码行数:33,代码来源:Program.cs


注:本文中的System.Security.Cryptography.CryptoStream.ReadAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。