本文整理汇总了C#中System.Security.Cryptography.AesManaged.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# AesManaged.GetType方法的具体用法?C# AesManaged.GetType怎么用?C# AesManaged.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.AesManaged
的用法示例。
在下文中一共展示了AesManaged.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: File
private async static Task File(string inFile, string outFile, RSACryptoServiceProvider key)
{
using (AesManaged aesManaged = new AesManaged())
{
// Create instance of AesManaged for
// symetric encryption of the data.
aesManaged.KeySize = 256;
aesManaged.BlockSize = 128;
aesManaged.Mode = CipherMode.CBC;
using (ICryptoTransform transform = aesManaged.CreateEncryptor())
{
RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(key);
byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType());
// Create byte arrays to contain
// the length values of the key and IV.
byte[] LenK = new byte[4];
byte[] LenIV = new byte[4];
int lKey = keyEncrypted.Length;
LenK = BitConverter.GetBytes(lKey);
int lIV = aesManaged.IV.Length;
LenIV = BitConverter.GetBytes(lIV);
// Write the following to the FileStream
// for the encrypted file (outFs):
// - length of the key
// - length of the IV
// - ecrypted key
// - the IV
// - the encrypted cipher content
using (FileStream outFs = new FileStream(outFile, FileMode.Create))
{
await outFs.WriteAsync(LenK, 0, 4);
await outFs.WriteAsync(LenIV, 0, 4);
await outFs.WriteAsync(keyEncrypted, 0, lKey);
await outFs.WriteAsync(aesManaged.IV, 0, lIV);
// Now write the cipher text using
// a CryptoStream for encrypting.
using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
{
// By encrypting a chunk at
// a time, you can save memory
// and accommodate large files.
int count = 0;
int offset = 0;
// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aesManaged.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
int bytesRead = 0;
using (FileStream inFs = new FileStream(inFile, FileMode.Open))
{
do
{
count = await inFs.ReadAsync(data, 0, blockSizeBytes);
offset += count;
await outStreamEncrypted.WriteAsync(data, 0, count);
bytesRead += blockSizeBytes;
}
while (count > 0);
}
}
}
}
}
}