DotNet中用到的加密算法总结

2008-02-22 09:42:24来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

1public class CryptUtil
2 {
3 public static string DecryptString(string input)
4 {
5 if (input.Equals(string.Empty))
6 {
7 return input;
8 }
9
10 byte[] byKey = {0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E};
11 byte[] IV = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
12 byte[] inputByteArray = new Byte[input.Length];
13 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
14 inputByteArray = Convert.FromBase64String(input);
15 MemoryStream ms = new MemoryStream();
16 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
17 cs.Write(inputByteArray, 0, inputByteArray.Length);
18 cs.FlushFinalBlock();
19 Encoding encoding = new UTF8Encoding();
20 return encoding.GetString(ms.ToArray());
21 }
22
23 public static string EncryptString(string input)
24 {
25 if (input.Equals(string.Empty))
26 {
27 return input;
28 }
29
30 byte[] byKey = {0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E};
31 byte[] IV = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
32 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
33 byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
34 MemoryStream ms = new MemoryStream();
35 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
36 cs.Write(inputByteArray, 0, inputByteArray.Length);
37 cs.FlushFinalBlock();
38 return Convert.ToBase64String(ms.ToArray());
39 }
40 /**//// <summary>
41 /// DES Base64 加密
42 /// </summary>
43 /// <param name="input">明文字符串</param>
44 /// <returns>已加密字符串</returns>
45 public static string DesBase64Encrypt(string input)
46 {
47 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
48 des.Mode = System.Security.Cryptography.CipherMode.ECB;
49 ICryptoTransform ct;
50 MemoryStream ms;
51 CryptoStream cs;
52 byte[] byt;
53 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};
54 byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};
55
56 ct = des.CreateEncryptor(Key, IV);
57
58 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
59
60 ms = new MemoryStream();
61 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
62 cs.Write(byt, 0, byt.Length);
63 cs.FlushFinalBlock();
64
65 cs.Close();
66
67 byte[] answer = ms.ToArray();
68 for(int j=0;j<answer.Length;j )
69 {
70 Console.Write(answer[j].ToString() " ");
71 }
72 Console.WriteLine();
73 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
74 }
75
76 /**//// <summary>
77 /// DES Base64 解密
78 /// </summary>
79 /// <param name="input">密文字符串</param>
80 /// <returns>解密字符串</returns>
81 public static string DesBase64Decrypt(string input)
82 {
83 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
84 des.Mode = System.Security.Cryptography.CipherMode.ECB;
85 ICryptoTransform ct;
86 MemoryStream ms;
87 CryptoStream cs;
88 byte[] byt;
89 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};
90 byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};
91
92 ct = des.CreateDecryptor(Key, IV);
93 byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组
94
95 ms = new MemoryStream();
96 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
97 cs.Write(byt, 0, byt.Length);
98 cs.FlushFinalBlock();
99
100 cs.Close();
101
102 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
103 }
104
105
106
107 /**//// <summary>
108 /// DES Base64 加密
109 /// </summary>
110 /// <param name="input">明文字符串</param>
111 /// <returns>已加密字符串</returns>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Session丢失原因与解决方案小结

下一篇:MySQL与ASP.NET配合更强大