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

/// &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), 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="Mod
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

}