加密解密 谔谔
开发中需要一个加密解密的双向过程
MD5等Hash类型的单向加密不能符合使用的要求
所以自己写了一个类,当然还是调用 System.Security.Cryptography的方法
DESCryptoServiceProvider
加密过程
通过GetKey()、GetIV()方法产生和密钥key、随机变量IV
解密过程
用key、IV作为参数 通过SetKey()、SetIV()给Sercurity类的Key和IV负值
key、IV都是byte[]
遇到的问题:string-〉byte[]的转化
解决: Convert.FromBase64String(string s) 返回byte[]
1using System;
2using System.IO;
3using System.Text;
4using System.Security.Cryptography;
5
6namespace mySecurity
7{
8 /// <summary>
9 /// MySecurity 的摘要说明。
10 /// </summary>
11 public class Security
12 {
13 private SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
14
15 //private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
16
17 /// <summary>
18 /// 加密
19 /// </summary>
20 /// <param name="Value"></param>
21 /// <returns></returns>
22 public string EncryptString(string Value)
23 {
24 ICryptoTransform ct;
25 MemoryStream ms;
26 CryptoStream cs;
27 byte[] byt;
28
29 ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
30
31 byt = Encoding.UTF8.GetBytes(Value);
32
33 ms = new MemoryStream();
34 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
35 cs.Write(byt, 0, byt.Length);
36 cs.FlushFinalBlock();
37
38 cs.Close();
39
40 return Convert.ToBase64String(ms.ToArray());
41 }
42
43 /// <summary>
44 /// 解密
45 /// </summary>
46 /// <param name="Value"></param>
47 /// <returns></returns>
48 public string DecryptString(string Value)
49 {
50 ICryptoTransform ct;
51 MemoryStream ms;
52 CryptoStream cs;
53 byte[] byt;
54
55 ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
56
57 byt = Convert.FromBase64String(Value);
58
59 ms = new MemoryStream();
60 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
61 cs.Write(byt, 0, byt.Length);
62 cs.FlushFinalBlock();
63
64 cs.Close();
65
66 return Encoding.UTF8.GetString(ms.ToArray());
67 }
68
69
70 public Security(){}
71
72 #region 设置、获取Key、IV
73 /// <summary>
74 /// 获取产生的Key值,加密过程之前需调用该方法
75 /// </summary>
76 /// <returns></returns>
77 public string GetKey()
78 {
79 mCSP.GenerateKey();
80 return Convert.ToBase64String(mCSP.Key);
81 }
82
83 /// <summary>
84 /// 获取产生的随机变量值,加密过程之前需调用该方法
85 /// </summary>
86 /// <returns></returns>
87 public string GetIV()
88 {
89 mCSP.GenerateIV();
90 return Convert.ToBase64String(mCSP.IV);
91 }
92
93 /// <summary>
94 /// 设置Key的值,解密过程之前需要调用
95 /// </summary>
96 /// <param name="key"></param>
97 /// <returns></returns>
98 public string SetKey(string key)
99 {
100 mCSP.Key = Convert.FromBase64String(key);
101 return Convert.ToBase64String(mCSP.Key);
102 }
103 /// <summary>
104 /// 设置随机变量IV的值,解密过程之前需要调用
105 /// </summary>
106 /// <param name="IV"></param>
107 /// <returns></returns>
108 public string SetIV(string IV)
109 {
110 mCSP.IV = Convert.FromBase64String(IV);
111 return Convert.ToBase64String(mCSP.IV);
112 }
113 #endregion
114
115
116 }
117}
118
2using System.IO;
3using System.Text;
4using System.Security.Cryptography;
5
6namespace mySecurity
7{
8 /// <summary>
9 /// MySecurity 的摘要说明。
10 /// </summary>
11 public class Security
12 {
13 private SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
14
15 //private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
16
17 /// <summary>
18 /// 加密
19 /// </summary>
20 /// <param name="Value"></param>
21 /// <returns></returns>
22 public string EncryptString(string Value)
23 {
24 ICryptoTransform ct;
25 MemoryStream ms;
26 CryptoStream cs;
27 byte[] byt;
28
29 ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
30
31 byt = Encoding.UTF8.GetBytes(Value);
32
33 ms = new MemoryStream();
34 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
35 cs.Write(byt, 0, byt.Length);
36 cs.FlushFinalBlock();
37
38 cs.Close();
39
40 return Convert.ToBase64String(ms.ToArray());
41 }
42
43 /// <summary>
44 /// 解密
45 /// </summary>
46 /// <param name="Value"></param>
47 /// <returns></returns>
48 public string DecryptString(string Value)
49 {
50 ICryptoTransform ct;
51 MemoryStream ms;
52 CryptoStream cs;
53 byte[] byt;
54
55 ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
56
57 byt = Convert.FromBase64String(Value);
58
59 ms = new MemoryStream();
60 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
61 cs.Write(byt, 0, byt.Length);
62 cs.FlushFinalBlock();
63
64 cs.Close();
65
66 return Encoding.UTF8.GetString(ms.ToArray());
67 }
68
69
70 public Security(){}
71
72 #region 设置、获取Key、IV
73 /// <summary>
74 /// 获取产生的Key值,加密过程之前需调用该方法
75 /// </summary>
76 /// <returns></returns>
77 public string GetKey()
78 {
79 mCSP.GenerateKey();
80 return Convert.ToBase64String(mCSP.Key);
81 }
82
83 /// <summary>
84 /// 获取产生的随机变量值,加密过程之前需调用该方法
85 /// </summary>
86 /// <returns></returns>
87 public string GetIV()
88 {
89 mCSP.GenerateIV();
90 return Convert.ToBase64String(mCSP.IV);
91 }
92
93 /// <summary>
94 /// 设置Key的值,解密过程之前需要调用
95 /// </summary>
96 /// <param name="key"></param>
97 /// <returns></returns>
98 public string SetKey(string key)
99 {
100 mCSP.Key = Convert.FromBase64String(key);
101 return Convert.ToBase64String(mCSP.Key);
102 }
103 /// <summary>
104 /// 设置随机变量IV的值,解密过程之前需要调用
105 /// </summary>
106 /// <param name="IV"></param>
107 /// <returns></returns>
108 public string SetIV(string IV)
109 {
110 mCSP.IV = Convert.FromBase64String(IV);
111 return Convert.ToBase64String(mCSP.IV);
112 }
113 #endregion
114
115
116 }
117}
118
最后编译成dll,方便重用