加密解密算法

 1 using System;
 2 using System.IO;
 3 using System.Security.Cryptography;
 4 
 5 namespace COMMON
 6 {
 7     public class CodeCommon
 8     {
 9         #region 加密
10         const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
11 
12         const string IV_64 = "VavicApp";
13         static public string Encode(string data)
14         {
15             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
16             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
17 
18             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
19             int i = cryptoProvider.KeySize;
20             MemoryStream ms = new MemoryStream();
21             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
22 
23             StreamWriter sw = new StreamWriter(cst);
24             sw.Write(data);
25             sw.Flush();
26             cst.FlushFinalBlock();
27             sw.Flush();
28             return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
29 
30         }
31         #endregion
32 
33         #region 解密
34         static public string Decode(string data)
35         {
36             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
37             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
38 
39             byte[] byEnc;
40             try
41             {
42                 byEnc = Convert.FromBase64String(data);
43             }
44             catch
45             {
46                 return null;
47             }
48 
49             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
50             MemoryStream ms = new MemoryStream(byEnc);
51             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
52             StreamReader sr = new StreamReader(cst);
53             return sr.ReadToEnd();
54         }
55         #endregion
56     }
57 }
原文地址:https://www.cnblogs.com/Sunflower-/p/5531076.html