流扩展:StreamExtensions-创新互联
10年积累的做网站、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有回民免费网站建设让你可以放心的选择与我们合作。>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Beehive.CloudReader.Utility
{
/// /// Stream 扩展
/// public static class StreamExtensions
{
/// ///
/// /// /// /// /// public static System.IO.Stream Encrypt(this System.IO.Stream stream, byte[] key)
where Algorithm : SymmetricAlgorithm
{
var alg = Activator.CreateInstance();
PasswordDeriveBytes pdb= new PasswordDeriveBytes(key, null);
alg.Key= pdb.GetBytes(alg.KeySize / 8);
alg.GenerateIV();
alg.IV= pdb.GetBytes(alg.IV.Length);
ICryptoTransform encryptor= alg.CreateEncryptor();
return new CryptoStream(stream, encryptor, CryptoStreamMode.Read);
}
/// ///
/// /// /// /// /// public static System.IO.Stream Decrypt(this System.IO.Stream stream, byte[] key)
where Algorithm : SymmetricAlgorithm
{
var alg = Activator.CreateInstance();
PasswordDeriveBytes pdb= new PasswordDeriveBytes(key, null);
alg.Key= pdb.GetBytes(alg.KeySize / 8);
alg.GenerateIV();
alg.IV= pdb.GetBytes(alg.IV.Length);
ICryptoTransform encryptor= alg.CreateDecryptor();
return new CryptoStream(stream, encryptor, CryptoStreamMode.Read);
}
/// ///
/// /// /// public static byte[] ToByteArray(this System.IO.Stream stream)
{
byte[] buffer = new byte[1000];
int readLength = 1;
using (MemoryStream ms = new MemoryStream())
{
while (readLength > 0)
{
readLength= stream.Read(buffer, 0, buffer.Length);
if (readLength > 0)
ms.Write(buffer,0, readLength);
}
return ms.ToArray();
}
}
}
}
文章标题:流扩展:StreamExtensions-创新互联
转载来于:http://pwwzsj.com/article/esogg.html