zoukankan      html  css  js  c++  java
  • 数据加密之文件/流加密解密

     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

  • 相关阅读:
    leetcode 第二题Add Two Numbers java
    二叉树中的那些常见的面试题(转)
    运行的指令
    Python常见经典 python中if __name__ == '__main__': 的解析
    软件测试基本概念
    JAVA Android王牌教程
    17个新手常见Python运行时错误
    QTP
    链表有关的常见面试题
    Robot Framework and Ride
  • 原文地址:https://www.cnblogs.com/nsky/p/4481976.html
Copyright © 2011-2022 走看看