代码改变世界

数据加密之文件/流加密解密

  糯米粥  阅读(788)  评论(0编辑  收藏  举报
复制代码
 1   /// <summary>  
 2     /// 为EndCallback提供数据  
 3     /// </summary>  
 4     public class EndState
 5     {
 6         internal EndState(bool isCancel, object state)
 7         {
 8             IsCancel = isCancel;
 9             State = state;
10         }
11 
12         /// <summary>  
13         /// 是否取消退出的  
14         /// </summary>  
15         public bool IsCancel { get; private set; }
16 
17         /// <summary>  
18         /// 获得传递的参数  
19         /// </summary>  
20         public object State { get; private set; }
21     }
22 
23 
24     /// <summary>  
25     /// 为ProgressCallback提供数据  
26     /// </summary>  
27     public class ProgressState
28     {
29         internal ProgressState(long byteRead, long totalBytesLength, object state)
30         {
31             BytesRead = byteRead;
32             TotalBytesLength = totalBytesLength;
33             State = state;
34         }
35 
36         /// <summary>  
37         /// 获得已经计算完成的字节数  
38         /// </summary>  
39         public long BytesRead { get; private set; }
40 
41         /// <summary>  
42         /// 获得总字节数  
43         /// </summary>  
44         public long TotalBytesLength { get; private set; }
45 
46         /// <summary>  
47         /// 获得传递的参数  
48         /// </summary>  
49         public object State { get; private set; }
50     }
复制代码

 

测试代码:

复制代码
 1 //------下面为对文件/流加密解密的需要实例化的部分------  
 2 
 3         /// <summary>  
 4         /// 完成计算的回调  
 5         /// </summary>  
 6         public delegate void EndCallback(EndState endState);
 7 
 8         /// <summary>  
 9         /// 进行计算时的回调  
10         /// </summary>  
11         public delegate void ProgressCallback(ProgressState progressState);
12 
13         private bool _cancel; //取消计算  
14 
15         /// <summary>  
16         /// 停止计算  
17         /// </summary>  
18         public void Stop()
19         {
20             _cancel = true;
21         }
22         public void DecryptData(Stream inStream, Stream outStream, SymmetricAlgorithm alg, ProgressCallback progressCallback, EndCallback endCallback, object state)
23         {
24             const int bufferSize = 100;
25             _cancel = false;
26 
27             long bytesRead = 0L;
28             long totalBytesLength = inStream.Length;
29             var buffer = new byte[bufferSize];
30 
31             using (var encStream = new CryptoStream(inStream, alg.CreateDecryptor(), CryptoStreamMode.Read))
32             {
33                 int num;
34                 do
35                 {
36                     num = encStream.Read(buffer, 0, bufferSize);
37                     outStream.Write(buffer, 0, num);
38                     if (progressCallback == null) continue;
39                     bytesRead += num;
40                     progressCallback(new ProgressState(bytesRead, totalBytesLength, state)); //进度回调  
41                 } while (num > 0 && !_cancel);
42                 if (endCallback != null) endCallback(new EndState(_cancel, state)); //计算结束回调  
43             }
44         }
45 
46         public void DecryptData(string inFileName, string outFileName, SymmetricAlgorithm alg, ProgressCallback progressCallback, EndCallback endCallback, object state)
47         {
48             using (var infs = new FileStream(inFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
49             {
50                 using (var outfs = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
51                 {
52                     DecryptData(infs, outfs, alg, progressCallback, endCallback, state);
53                 }
54             }
55         }
复制代码

 

 

参考:http://blog.csdn.net/oyi319/article/details/5836010

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
'
点击右上角即可分享
微信分享提示