流扩展: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 { /// <summary> /// Stream 扩展 /// </summary> public static class StreamExtensions { /// <summary> /// /// </summary> /// <typeparam name="Algorithm"></typeparam> /// <param name="stream"></param> /// <param name="key"></param> /// <returns></returns> public static System.IO.Stream Encrypt<Algorithm>(this System.IO.Stream stream, byte[] key) where Algorithm : SymmetricAlgorithm { var alg = Activator.CreateInstance<Algorithm>(); 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); } /// <summary> /// /// </summary> /// <typeparam name="Algorithm"></typeparam> /// <param name="stream"></param> /// <param name="key"></param> /// <returns></returns> public static System.IO.Stream Decrypt<Algorithm>(this System.IO.Stream stream, byte[] key) where Algorithm : SymmetricAlgorithm { var alg = Activator.CreateInstance<Algorithm>(); 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); } /// <summary> /// /// </summary> /// <param name="stream"></param> /// <returns></returns> 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-创新互联
分享链接:https://www.cdcxhl.com/article16/esogg.html

成都网站建设公司_创新互联,为您提供搜索引擎优化软件开发Google电子商务定制网站手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站