本文整理汇总了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);
}
}
}
}
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}
}