.net DEC加密 与解密
1
using System;
2
using System.Security.Cryptography;
3
using System.IO;
4
using System.Text;
5
namespace Company.BLL
6

{
7
/**//// <summary>
8
/// 此类用来加密与解密存在于URL中的参数
9
/// Create by sp
10
/// </summary>
11
public class IaskUrlEncode
12
{
13
public IaskUrlEncode()
14
{
15
//
16
// TODO: 在此处添加构造函数逻辑
17
//
18
}
19
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key
20
string _PassWordKey = "hgfedcba";//PassWord加密Key
21
22
公共方法#region 公共方法
23
/**//// <summary>
24
/// 加密URL传输的字符串
25
/// </summary>
26
/// <param name="QueryString"></param>
27
/// <returns></returns>
28
public string EncryptQueryString(string QueryString)
29
{
30
return Encrypt(QueryString, _QueryStringKey);
31
}
32
33
34
/**//// <summary>
35
/// 解密URL传输的字符串
36
/// </summary>
37
/// <param name="QueryString"></param>
38
/// <returns></returns>
39
public string DecryptQueryString(string QueryString)
40
{
41
return Decrypt(QueryString, _QueryStringKey);
42
}
43
44
45
/**//// <summary>
46
/// 加密帐号口令
47
/// </summary>
48
/// <param name="PassWord"></param>
49
/// <returns></returns>
50
private string EncryptPassWord(string PassWord)
51
{
52
return Encrypt(PassWord, _PassWordKey);
53
}
54
55
56
/**//// <summary>
57
/// 解密帐号口令
58
/// </summary>
59
/// <param name="PassWord"></param>
60
/// <returns></returns>
61
private string DecryptPassWord(string PassWord)
62
{
63
return Decrypt(PassWord, _PassWordKey);
64
}
65
#endregion
66
67
加密过程#region 加密过程
68
/**//// <summary>
69
/// DEC 加密过程
70
/// </summary>
71
/// <param name="pToEncrypt"></param>
72
/// <param name="sKey"></param>
73
/// <returns></returns>
74
private string Encrypt(string pToEncrypt, string sKey)
75
{
76
DESCryptoServiceProvider des = new DESCryptoServiceProvider();//把字符串放到byte数组中
77
78
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
79
80
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);//建立加密对象的密钥和偏移量
81
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
82
MemoryStream ms = new MemoryStream();//使得输入密码必须输入英文文本
83
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
84
85
cs.Write(inputByteArray, 0, inputByteArray.Length);
86
cs.FlushFinalBlock();
87
88
StringBuilder ret = new StringBuilder();
89
foreach (byte b in ms.ToArray())
90
{
91
ret.AppendFormat("{0:X2}", b);
92
}
93
ret.ToString();
94
return ret.ToString();
95
}
96
#endregion
97
98
解密过程#region 解密过程
99
/**//// <summary>
100
/// DEC 解密过程
101
/// </summary>
102
/// <param name="pToDecrypt"></param>
103
/// <param name="sKey"></param>
104
/// <returns></returns>
105
private string Decrypt(string pToDecrypt, string sKey)
106
{
107
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
108
109
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
110
for (int x = 0; x < pToDecrypt.Length / 2; x++)
111
{
112
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
113
inputByteArray[x] = (byte)i;
114
}
115
116
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);//建立加密对象的密钥和偏移量,此值重要,不能修改
117
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
118
MemoryStream ms = new MemoryStream();
119
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
120
121
cs.Write(inputByteArray, 0, inputByteArray.Length);
122
cs.FlushFinalBlock();
123
124
StringBuilder ret = new StringBuilder();//建立StringBuilder对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
125
126
return System.Text.Encoding.Default.GetString(ms.ToArray());
127
}
128
#endregion
129
130
匹配过程#region 匹配过程
131
/**//// <summary>
132
/// 检查己加密的字符串是否与原文相同
133
/// </summary>
134
/// <param name="EnString"></param>
135
/// <param name="FoString"></param>
136
/// <param name="Mode"></param>
137
/// <returns></returns>
138
private bool ValidateString(string EnString, string FoString, int Mode)
139
{
140
switch (Mode)
141
{
142
default:
143
case 1:
144
if (Decrypt(EnString, _QueryStringKey) == FoString.ToString())
145
{
146
return true;
147
}
148
else
149
{
150
return false;
151
}
152
case 2:
153
if (Decrypt(EnString, _PassWordKey) == FoString.ToString())
154
{
155
return true;
156
}
157
else
158
{
159
return false;
160
}
161
}
162
}
163
#endregion
164
}
165
}
using System;2
using System.Security.Cryptography;3
using System.IO;4
using System.Text;5
namespace Company.BLL6


{7

/**//// <summary>8
/// 此类用来加密与解密存在于URL中的参数9
/// Create by sp10
/// </summary>11
public class IaskUrlEncode12

{13
public IaskUrlEncode()14

{15
//16
// TODO: 在此处添加构造函数逻辑17
//18
}19
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key20
string _PassWordKey = "hgfedcba";//PassWord加密Key21

22

公共方法#region 公共方法23

/**//// <summary>24
/// 加密URL传输的字符串25
/// </summary>26
/// <param name="QueryString"></param>27
/// <returns></returns>28
public string EncryptQueryString(string QueryString)29

{30
return Encrypt(QueryString, _QueryStringKey);31
}32

33

34

/**//// <summary>35
/// 解密URL传输的字符串36
/// </summary>37
/// <param name="QueryString"></param>38
/// <returns></returns>39
public string DecryptQueryString(string QueryString)40

{41
return Decrypt(QueryString, _QueryStringKey);42
}43

44

45

/**//// <summary>46
/// 加密帐号口令47
/// </summary>48
/// <param name="PassWord"></param>49
/// <returns></returns>50
private string EncryptPassWord(string PassWord)51

{52
return Encrypt(PassWord, _PassWordKey);53
}54

55

56

/**//// <summary>57
/// 解密帐号口令58
/// </summary>59
/// <param name="PassWord"></param>60
/// <returns></returns>61
private string DecryptPassWord(string PassWord)62

{63
return Decrypt(PassWord, _PassWordKey);64
}65
#endregion66

67

加密过程#region 加密过程68

/**//// <summary>69
/// DEC 加密过程70
/// </summary>71
/// <param name="pToEncrypt"></param>72
/// <param name="sKey"></param>73
/// <returns></returns>74
private string Encrypt(string pToEncrypt, string sKey)75

{76
DESCryptoServiceProvider des = new DESCryptoServiceProvider();//把字符串放到byte数组中77

78
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);79

80
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);//建立加密对象的密钥和偏移量81
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);//原文使用ASCIIEncoding.ASCII方法的GetBytes方法82
MemoryStream ms = new MemoryStream();//使得输入密码必须输入英文文本83
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);84

85
cs.Write(inputByteArray, 0, inputByteArray.Length);86
cs.FlushFinalBlock();87

88
StringBuilder ret = new StringBuilder();89
foreach (byte b in ms.ToArray())90

{91
ret.AppendFormat("{0:X2}", b);92
}93
ret.ToString();94
return ret.ToString();95
}96
#endregion97

98

解密过程#region 解密过程99

/**//// <summary>100
/// DEC 解密过程101
/// </summary>102
/// <param name="pToDecrypt"></param>103
/// <param name="sKey"></param>104
/// <returns></returns>105
private string Decrypt(string pToDecrypt, string sKey)106

{107
DESCryptoServiceProvider des = new DESCryptoServiceProvider();108

109
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];110
for (int x = 0; x < pToDecrypt.Length / 2; x++)111

{112
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));113
inputByteArray[x] = (byte)i;114
}115

116
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);//建立加密对象的密钥和偏移量,此值重要,不能修改117
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);118
MemoryStream ms = new MemoryStream();119
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);120

121
cs.Write(inputByteArray, 0, inputByteArray.Length);122
cs.FlushFinalBlock();123

124
StringBuilder ret = new StringBuilder();//建立StringBuilder对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象125

126
return System.Text.Encoding.Default.GetString(ms.ToArray());127
}128
#endregion129

130

匹配过程#region 匹配过程131

/**//// <summary>132
/// 检查己加密的字符串是否与原文相同133
/// </summary>134
/// <param name="EnString"></param>135
/// <param name="FoString"></param>136
/// <param name="Mode"></param>137
/// <returns></returns>138
private bool ValidateString(string EnString, string FoString, int Mode)139

{140
switch (Mode)141

{142
default:143
case 1:144
if (Decrypt(EnString, _QueryStringKey) == FoString.ToString())145

{146
return true;147
}148
else149

{150
return false;151
}152
case 2:153
if (Decrypt(EnString, _PassWordKey) == FoString.ToString())154

{155
return true;156
}157
else158

{159
return false;160
}161
}162
}163
#endregion164
}165
}
浙公网安备 33010602011771号