入口参数有三个:key、data、mode。 key为加密解密使用的密钥,data为加密解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。
1 /// <summary>
2 /// DES对称加密
3 /// </summary>
4 public sealed class DES {
5 /// <summary>
6 /// Des解密方法
7 /// </summary>
8 /// <param name="val"></param>
9 /// <param name="key"></param>
10 /// <param name="IV"></param>
11 /// <returns></returns>
12 public static string Decrypt(string val, string key, string IV) {
13 try {
14 byte[] buffer1 = Convert.FromBase64String(key);
15 byte[] buffer2 = Convert.FromBase64String(IV);
16 DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
17 provider1.Mode = CipherMode.ECB;
18 provider1.Key = buffer1;
19 provider1.IV = buffer2;
20 ICryptoTransform transform1 = provider1.CreateDecryptor(provider1.Key, provider1.IV);
21 byte[] buffer3 = Convert.FromBase64String(val);
22 MemoryStream stream1 = new MemoryStream();
23 CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
24 stream2.Write(buffer3, 0, buffer3.Length);
25 stream2.FlushFinalBlock();
26 stream2.Close();
27 return Encoding.Default.GetString(stream1.ToArray());
28 }
29 catch// (System.Exception ex)
30 {
31 return "";
32 }
33 }
34
35 /// <summary>
36 /// Des加密方法
37 /// </summary>
38 /// <param name="val"></param>
39 /// <param name="key"></param>
40 /// <param name="IV"></param>
41 /// <returns></returns>
42 public static string Encrypt(string val, string key, string IV) {
43 try {
44 byte[] buffer1 = Convert.FromBase64String(key);
45 byte[] buffer2 = Convert.FromBase64String(IV);
46
47 DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
48 provider1.Mode = CipherMode.ECB;
49 provider1.Key = buffer1;
50 provider1.IV = buffer2;
51 ICryptoTransform transform1 = provider1.CreateEncryptor(provider1.Key, provider1.IV);
52 byte[] buffer3 = Encoding.Default.GetBytes(val);
53 MemoryStream stream1 = new MemoryStream();
54 CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
55 stream2.Write(buffer3, 0, buffer3.Length);
56 stream2.FlushFinalBlock();
57 stream2.Close();
58 return Convert.ToBase64String(stream1.ToArray());
59 }
60 catch// (Exception ex)
61 {
62 return "";
63 }
64 }
65 }
66
2 /// DES对称加密
3 /// </summary>
4 public sealed class DES {
5 /// <summary>
6 /// Des解密方法
7 /// </summary>
8 /// <param name="val"></param>
9 /// <param name="key"></param>
10 /// <param name="IV"></param>
11 /// <returns></returns>
12 public static string Decrypt(string val, string key, string IV) {
13 try {
14 byte[] buffer1 = Convert.FromBase64String(key);
15 byte[] buffer2 = Convert.FromBase64String(IV);
16 DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
17 provider1.Mode = CipherMode.ECB;
18 provider1.Key = buffer1;
19 provider1.IV = buffer2;
20 ICryptoTransform transform1 = provider1.CreateDecryptor(provider1.Key, provider1.IV);
21 byte[] buffer3 = Convert.FromBase64String(val);
22 MemoryStream stream1 = new MemoryStream();
23 CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
24 stream2.Write(buffer3, 0, buffer3.Length);
25 stream2.FlushFinalBlock();
26 stream2.Close();
27 return Encoding.Default.GetString(stream1.ToArray());
28 }
29 catch// (System.Exception ex)
30 {
31 return "";
32 }
33 }
34
35 /// <summary>
36 /// Des加密方法
37 /// </summary>
38 /// <param name="val"></param>
39 /// <param name="key"></param>
40 /// <param name="IV"></param>
41 /// <returns></returns>
42 public static string Encrypt(string val, string key, string IV) {
43 try {
44 byte[] buffer1 = Convert.FromBase64String(key);
45 byte[] buffer2 = Convert.FromBase64String(IV);
46
47 DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
48 provider1.Mode = CipherMode.ECB;
49 provider1.Key = buffer1;
50 provider1.IV = buffer2;
51 ICryptoTransform transform1 = provider1.CreateEncryptor(provider1.Key, provider1.IV);
52 byte[] buffer3 = Encoding.Default.GetBytes(val);
53 MemoryStream stream1 = new MemoryStream();
54 CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
55 stream2.Write(buffer3, 0, buffer3.Length);
56 stream2.FlushFinalBlock();
57 stream2.Close();
58 return Convert.ToBase64String(stream1.ToArray());
59 }
60 catch// (Exception ex)
61 {
62 return "";
63 }
64 }
65 }
66