加解密1-流

加密:

 1         /// <summary>
 2         /// 使用指定的加密算法加密流
 3         /// </summary>
 4         /// <param name="inStream"></param>
 5         /// <param name="provider"></param>
 6         /// <returns>加密后的流</returns>
 7         public static Stream GetEncryptedStream(Stream inStream, SymmetricAlgorithm provider) 
 8         {
 9             MemoryStream outStream = new MemoryStream();
10             //以加密方式写入到outStream
11             CryptoStream encryptStream = new CryptoStream(outStream, provider.CreateEncryptor(), CryptoStreamMode.Write);
12 
13             int numBytes;
14             byte[] inputBytes = new byte[1024];
15             while ((numBytes = inStream.Read(inputBytes, 0, inputBytes.Length)) != 0)
16             {
17                 encryptStream.Write(inputBytes, 0, numBytes);
18             }
19             encryptStream.FlushFinalBlock();
20 
21             //回到输出的流的起点
22             outStream.Position = 0;
23 
24             //加密后的数据
25             return outStream;
26         }
27 
28 
29        /// <summary>
30         /// 使用指定的加密算法加密流
31         /// </summary>
32         /// <param name="inStream"></param>
33         /// <param name="provider"></param>
34         /// <returns>加密后的流</returns>
35         public static Stream GetEncryptedStream(Stream inStream, SymmetricAlgorithm provider) 
36         {
37             MemoryStream outStream = new MemoryStream();
38             //以加密方式读取inStream
39             CryptoStream encryptStream = new CryptoStream(inStream, provider.CreateEncryptor(), CryptoStreamMode.Read);
40 
41             int numBytes;
42             byte[] inputBytes = new byte[1024];
43             while ((numBytes = encryptStream.Read(inputBytes, 0, inputBytes.Length)) != 0)
44             {
45                 outStream.Write(inputBytes, 0, numBytes);
46             }
47             //回到输出的流的起点
48             outStream.Position = 0;
49 
50             //加密后的数据
51             return outStream;
52         }

解密:

 1         /// <summary>
 2         /// 使用指定的算法提供器解密流
 3         /// </summary>
 4         /// <param name="inStream"></param>
 5         /// <param name="provider"></param>
 6         /// <returns>解密后的流</returns>
 7         public static Stream GetDecryptedStream(Stream inStream,SymmetricAlgorithm provider)
 8         {
 9             MemoryStream outStream = new MemoryStream();
10             //以解密方式读取inStream
11             CryptoStream decryptStream = new CryptoStream(inStream, provider.CreateDecryptor(), CryptoStreamMode.Read);
12             
13             int numBytes;
14             byte[] inputBytes = new byte[_bufferSize];
15             while ((numBytes = decryptStream.Read(inputBytes,0,inputBytes.Length)) !=0)
16             {
17                 outStream.Write(inputBytes, 0, numBytes);
18             }
19 
20             //回到输出的流的起点
21             outStream.Position = 0;
22 
23             return outStream;
24         }
25 
26         /// <summary>
27         /// 使用指定的算法提供器解密流
28         /// </summary>
29         /// <param name="inStream"></param>
30         /// <param name="provider"></param>
31         /// <returns>解密后的流</returns>
32         public static Stream GetDecryptedStream(Stream inStream,SymmetricAlgorithm provider)
33         {
34             MemoryStream outStream = new MemoryStream();
35             //以解密方式写入outStream 
36             CryptoStream decryptStream = new CryptoStream(outStream , provider.CreateDecryptor(), CryptoStreamMode.Write);
37             
38             int numBytes;
39             byte[] inputBytes = new byte[_bufferSize];
40             while ((numBytes = inStream.Read(inputBytes,0,inputBytes.Length)) !=0)
41             {
42                 decryptStream.Write(inputBytes, 0, numBytes);
43             }
44             decryptStream.FlushFinalBlock();
45             //回到输出的流的起点
46             outStream.Position = 0;
47 
48             return outStream;
49         }    

 

posted @ 2017-11-08 15:02  缘续成  阅读(91)  评论(0编辑  收藏  举报