View Code
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Security.Cryptography;
11 using System.IO;
12
13 /// <summary>
14 /// 密码控制
15 /// </summary>
16 public class Class_Password
17 {
18 const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
19
20 const string IV_64 = "VavicApp";
21
22 public Class_Password()
23 {
24 //
25 // TODO: 在此处添加构造函数逻辑
26 //
27 }
28
29 #region EnCode 加密
30 /// <summary>
31 /// EnCode 加密
32 /// </summary>
33 /// <param name="str">要加密的字符串</param>
34 /// <returns></returns>
35
36 public string EnCode(string data)
37 {
38 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
39 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
40
41 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
42 int i = cryptoProvider.KeySize;
43 MemoryStream ms = new MemoryStream();
44 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,
45
46 byIV), CryptoStreamMode.Write);
47
48 StreamWriter sw = new StreamWriter(cst);
49 sw.Write(data);
50 sw.Flush();
51 cst.FlushFinalBlock();
52 sw.Flush();
53 return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
54 }
55 #endregion
56
57 #region DeCode 解密
58 /// <summary>
59 /// DeCode 解密
60 /// </summary>
61 /// <param name="str">要解密的字符串</param>
62 /// <returns></returns>
63 public string DeCode(string data)
64 {
65 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
66 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
67
68 byte[] byEnc;
69 try
70 {
71 byEnc = Convert.FromBase64String(data);
72 }
73 catch
74 {
75 return null;
76 }
77
78 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
79 MemoryStream ms = new MemoryStream(byEnc);
80 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,
81
82 byIV), CryptoStreamMode.Read);
83 StreamReader sr = new StreamReader(cst);
84 return sr.ReadToEnd();
85 }
86 #endregion
87
88 }