107 /**////
108 /// DES + Base64 加密
109 ///
110 /// 明文字符串
111 ///
112 public static string DesBase64EncryptForID5(string input)
113 {
114 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
115 des.Mode = System.Security.Cryptography.CipherMode.CBC;
116 ICryptoTransform ct;
117 MemoryStream ms;
118 CryptoStream cs;
119 byte[] byt;
120 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};
121 byte[] IV = new byte[8]{56,50,55,56,56,55,49,49};
122
123 ct = des.CreateEncryptor(Key, IV);
124
125 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
126
127 ms = new MemoryStream();
128 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
129 cs.Write(byt, 0, byt.Length);
130 cs.FlushFinalBlock();
131
132 cs.Close();
133
134 byte[] answer = ms.ToArray();
135 for(int j=0;j 136 { 137 Console.Write(answer[j].ToString()+ " "); 138 } 139 Console.WriteLine(); 140 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串 141 } 142 143 144 /**//// 145 /// DES + Base64 解密 146 /// 147 /// 密文字符串 148 /// 149 public static string DesBase64DecryptForID5(string input) 150 { 151 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create(); 152 des.Mode = System.Security.Cryptography.CipherMode.CBC; 153 ICryptoTransform ct; 154 MemoryStream ms; 155 CryptoStream cs; 156 byte[] byt; 157 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49}; 158 byte[] IV = new byte[8]{56,50,55,56,56,55,49,49}; 159 160 ct = des.CreateDecryptor(Key, IV); 161 byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组 162 163 ms = new MemoryStream(); 164 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 165 cs.Write(byt, 0, byt.Length); 166 cs.FlushFinalBlock(); 167 168 cs.Close(); 169 170 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串 171 } 172 173 174 /**//// 175 /// 3DES 加密 Byte[] to HEX string 176 /// 177 /// 明文字符串 178 /// 179 public static string ThreeDesEncryptHEX(string input) 180 { 181 string result = ""; 182 System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create(); 183 des.Mode = System.Security.Cryptography.CipherMode.CBC; 184 des.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 185 ICryptoTransform ct; 186 MemoryStream ms; 187 CryptoStream cs; 188 byte[] byt; 189 byte[] Key = new byte[24]{ 190 1,2,3,4,5,6, 191 1,2,3,4,5,6, 192 1,2,3,4,5,6, 193 1,2,3,4,5,6 194 }; 195 byte[] IV = new byte[8]{1,2,3,4,5,6,1,2}; 196 197 ct = des.CreateEncryptor(Key, IV); 198 199 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组 200 201 ms = new MemoryStream(); 202 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 203 cs.Write(byt, 0, byt.Length); 204 cs.FlushFinalBlock(); 205 206 cs.Close(); 207 208 byte[] answer = ms.ToArray(); 209 for(int j=0;j 210 { 211 result += answer[j].ToString("x").PadLeft(2,'0'); 212 } 213 return result; 214 } 215 216 /**//// 217 /// 3DES + HEX to byte[] 解密 218 /// 219 /// 密文字符串 220 /// 221 public static string ThreeDesDecryptHEX(string input) 222 { 223 System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create(); 224 des.Mode = System.Security.Cryptography.CipherMode.CBC; 225 des.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 226 ICryptoTransform ct; 227 MemoryStream ms; 228 CryptoStream cs; 229 byte[] Key = new byte[24]{ 230 1,2,3,4,5,6, 231 1,2,3,4,5,6, 232 1,2,3,4,5,6, 233 1,2,3,4,5,6 234 }; 235 byte[] IV = new byte[8]{1,2,3,4,5,6,1,2}; 236 237 ct = des.CreateDecryptor(Key, IV); 238 //byt = Convert.FromBase64String(input); // 将 密文 以 HEX to byte[]编码转换成 byte 数组 239 if(input.Length<=1) 240 { 241 throw new Exception("encrypted HEX string is too short!"); 242 } 243 byte[] byt = new byte[input.Length/2]; 244 for(int i=0;i 245 { 246 //Console.WriteLine(input.Substring(i*2,2)); 247 byt[i] = Convert.ToByte(input.Substring(i*2,2),16); 248 } 249 250 ms = new MemoryStream(); 251 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 252 cs.Write(byt, 0, byt.Length); 253 cs.FlushFinalBlock(); 254 255 cs.Close(); 256 257 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串 258 } 259 /**//// 260 /// Base64解码 261 /// 262 /// 263 /// 264 public static string DecodingFromBase64(string base64Str) 265 { 266 Byte[] bytes = Convert.FromBase64String(base64Str); 267 return System.Text.Encoding.UTF8.GetString(bytes); 268 } 269 /**//// 270 /// Base64编码 271 /// 272 /// 273 /// 274 public static string EncodingToBase64(string str) 275 { 276 return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); 277 } 278 /**//// 279 /// 根据指定的编码方式Base64解码 280 /// 281 /// 282 /// 283 /// 284 public static string DecodingFromBase64(string base64Str,System.Text.Encoding strEncoding) 285 { 286 Byte[] bytes = Convert.FromBase64String(base64Str); 287 return strEncoding.GetString(bytes); 288 } 289 /**//// 290 /// 根据指定的编码方式Base64编码 291 /// 292 /// 293 /// 294 /// 295 public static string EncodingToBase64(string str,System.Text.Encoding strEncoding) 296 { 297 return Convert.ToBase64String(strEncoding.GetBytes(str)); 298 } 299 } 两个常用的方法 1 /**//// 2 /// 通过字节数组形式的密钥获取字符串形式的密钥 3 /// 4 void GetStringByByteArray() 5 { 6 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49}; 7 Response.Write(System.Text.Encoding.Default.GetString(Key)); 8 Response.End(); 9 } 10 11 /**//// 12 /// 通过字符串形式的密钥获取字节数组形式的密钥 13 /// 14 void GetByteArrayByString() 15 { 16 string key = "82788711"; 17 Response.Write(System.Text.Encoding.Default.GetBytes(key)); 18 Response.End(); 19 20 } 有这里没包括的,欢迎回复,大家一起总结一下~~ http://www.cnblogs.com/goody9807/archive/2007/01/23/627785.HTML

