【博主】反骨仔 【出處】http://www.cnblogs.com/liqingwen/p/6155694.html ...
1 using System; 2 using System.IO; 3 using System.Security.Cryptography; 4 using System.Text; 5 6 namespace Wen.Helpers 7 { 8 /// <summary> 9 /// 安全助手 10 /// </summary> 11 public sealed class SecurityHelper 12 { 13 private static readonly byte [] IvBytes = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; 14 15 #region 通用加密演算法 16 17 /// <summary> 18 /// 哈希加密演算法 19 /// </summary> 20 /// <param name="hashAlgorithm"> 所有加密哈希演算法實現均必須從中派生的基類 </param> 21 /// <param name="input"> 待加密的字元串 </param> 22 /// <param name="encoding"> 字元編碼 </param> 23 /// <returns></returns> 24 private static string HashEncrypt( HashAlgorithm hashAlgorithm, string input, Encoding encoding) 25 { 26 var data = hashAlgorithm.ComputeHash(encoding.GetBytes(input)); 27 28 return BitConverter .ToString(data).Replace( "-", "" ); 29 } 30 31 /// <summary> 32 /// 驗證哈希值 33 /// </summary> 34 /// <param name="hashAlgorithm"> 所有加密哈希演算法實現均必須從中派生的基類 </param> 35 /// <param name="unhashedText"> 未加密的字元串 </param> 36 /// <param name="hashedText"> 經過加密的哈希值 </param> 37 /// <param name="encoding"> 字元編碼 </param> 38 /// <returns></returns> 39 private static bool VerifyHashValue( HashAlgorithm hashAlgorithm, string unhashedText, string hashedText, 40 Encoding encoding) 41 { 42 return string .Equals(HashEncrypt(hashAlgorithm, unhashedText, encoding), hashedText, 43 StringComparison .OrdinalIgnoreCase); 44 } 45 46 #endregion 通用加密演算法 47 48 #region 哈希加密演算法 49 50 #region MD5 演算法 51 52 /// <summary> 53 /// MD5 加密 54 /// </summary> 55 /// <param name="input"> 待加密的字元串 </param> 56 /// <param name="encoding"> 字元編碼 </param> 57 /// <returns></returns> 58 public static string MD5Encrypt( string input, Encoding encoding) 59 { 60 return HashEncrypt(MD5 .Create(), input, encoding); 61 } 62 63 /// <summary> 64 /// 驗證 MD5 值 65 /// </summary> 66 /// <param name="input"> 未加密的字元串 </param> 67 /// <param name="encoding"> 字元編碼 </param> 68 /// <returns></returns> 69 public static bool VerifyMD5Value( string input, Encoding encoding) 70 { 71 return VerifyHashValue(MD5 .Create(), input, MD5Encrypt(input, encoding), encoding); 72 } 73 74 #endregion MD5 演算法 75 76 #region SHA1 演算法 77 78 /// <summary> 79 /// SHA1 加密 80 /// </summary> 81 /// <param name="input"> 要加密的字元串 </param> 82 /// <param name="encoding"> 字元編碼 </param> 83 /// <returns></returns> 84 public static string SHA1Encrypt( string input, Encoding encoding) 85 { 86 return HashEncrypt(SHA1 .Create(), input, encoding); 87 } 88 89 /// <summary> 90 /// 驗證 SHA1 值 91 /// </summary> 92 /// <param name="input"> 未加密的字元串 </param> 93 /// <param name="encoding"> 字元編碼 </param> 94 /// <returns></returns> 95 public static bool VerifySHA1Value( string input, Encoding encoding) 96 { 97 return VerifyHashValue(SHA1 .Create(), input, SHA1Encrypt(input, encoding), encoding); 98 } 99 100 #endregion SHA1 演算法 101 102 #region SHA256 演算法 103 104 /// <summary> 105 /// SHA256 加密 106 /// </summary> 107 /// <param name="input"> 要加密的字元串 </param> 108 /// <param name="encoding"> 字元編碼 </param> 109 /// <returns></returns> 110 public static string SHA256Encrypt( string input, Encoding encoding) 111 { 112 return HashEncrypt(SHA256 .Create(), input, encoding); 113 } 114 115 /// <summary> 116 /// 驗證 SHA256 值 117 /// </summary> 118 /// <param name="input"> 未加密的字元串 </param> 119 /// <param name="encoding"> 字元編碼 </param> 120 /// <returns></returns> 121 public static bool VerifySHA256Value( string input, Encoding encoding) 122 { 123 return VerifyHashValue(SHA256 .Create(), input, SHA256Encrypt(input, encoding), encoding); 124 } 125 126 #endregion SHA256 演算法 127 128 #region SHA384 演算法 129 130 /// <summary> 131 /// SHA384 加密 132 /// </summary> 133 /// <param name="input"> 要加密的字元串 </param> 134 /// <param name="encoding"> 字元編碼 </param> 135 /// <returns></returns> 136 public static string SHA384Encrypt( string input, Encoding encoding) 137 { 138 return HashEncrypt(SHA384 .Create(), input, encoding); 139 } 140 141 /// <summary> 142 /// 驗證 SHA384 值 143 /// </summary> 144 /// <param name="input"> 未加密的字元串 </param> 145 /// <param name="encoding"> 字元編碼 </param> 146 /// <returns></returns> 147 public static bool VerifySHA384Value( string input, Encoding encoding) 148 { 149 return VerifyHashValue(SHA256 .Create(), input, SHA384Encrypt(input, encoding), encoding); 150 } 151 152 #endregion SHA384 演算法 153 154 #region SHA512 演算法 155 156 /// <summary> 157 /// SHA512 加密 158 /// </summary> 159 /// <param name="input"> 要加密的字元串 </param> 160 /// <param name="encoding"> 字元編碼 </param> 161 /// <returns></returns> 162 public static string SHA512Encrypt( string input, Encoding encoding) 163 { 164 return HashEncrypt(SHA512 .Create(), input, encoding); 165 } 166 167 /// <summary> 168 /// 驗證 SHA512 值 169 /// </summary> 170 /// <param name="input"> 未加密的字元串 </param> 171 /// <param name="encoding"> 字元編碼 </param> 172 /// <returns></returns> 173 public static bool VerifySHA512Value( string input, Encoding encoding) 174 { 175 return VerifyHashValue(SHA512 .Create(), input, SHA512Encrypt(input, encoding), encoding); 176 } 177 178 #endregion SHA512 演算法 179 180 #region HMAC-MD5 加密 181 182 /// <summary> 183 /// HMAC-MD5 加密 184 /// </summary> 185 /// <param name="input"> 要加密的字元串 </param> 186 /// <param name="key"> 密鑰 </param> 187 /// <param name="encoding"> 字元編碼 </param> 188 /// <returns></returns> 189 public static string HMACSMD5Encrypt( string input, string key, Encoding encoding) 190 { 191 return HashEncrypt(new HMACMD5 (encoding.GetBytes(key)), input, encoding); 192 } 193 194 #endregion HMAC-MD5 加密 195 196 #region HMAC-SHA1 加密 197 198 /// <summary> 199 /// HMAC-SHA1 加密 200 /// </summary> 201 /// <param name="input"> 要加密的字元串 </param> 202 /// <param name="key"> 密鑰 </param> 203 /// <param name="encoding"> 字元編碼 </param> 204 /// <returns></returns> 205 public static string HMACSHA1Encrypt( string input, string key, Encoding encoding) 206 { 207 return HashEncrypt(new HMACSHA1 (encoding.GetBytes(key)), input, encoding); 208 } 209 210 #endregion HMAC-SHA1 加密 211 212 #region HMAC-SHA256 加密 213 214 /// <summary> 215 /// HMAC-SHA256 加密 216 /// </summary> 217 /// <param name="input"> 要加密的字元串 </param> 218 /// <param name="key"> 密鑰 </param> 219 /// <param name="encoding"> 字元編碼 </param> 220 /// <returns></returns> 221 public static string HMACSHA256Encrypt( string input, string key, Encoding encoding) 222 { 223 return HashEncrypt(new HMACSHA256 (encoding.GetBytes(key)), input, encoding); 224 } 225 226 #endregion HMAC-SHA256 加密 227 228 #region HMAC-SHA384 加密 229 230 /// <summary> 231 /// HMAC-SHA384 加密 232 /// </summary> 233 /// <param name="input"> 要加密的字元串 </param> 234 /// <param name="key"> 密鑰 </param> 235 /// <param name="encoding"> 字元編碼 </param> 236 /// <returns></returns> 237 public static string HMACSHA384Encrypt( string input, string key, Encoding encoding) 238 { 239 return HashEncrypt(new HMACSHA384 (encoding.GetBytes(key)), input, encoding); 240 } 241 242 #endregion HMAC-SHA384 加密 243 244 #region HMAC-SHA512 加密 245 246 /// <summary> 247 /// HMAC-SHA512 加密 248 /// </summary> 249 /// <param name="input"> 要加密的字元串 </param> 250 /// <param name="key"> 密鑰 </param> 251 /// <param name="encoding"> 字元編碼 </param> 252 /// <returns></returns> 253 public static string HMACSHA512Encrypt( string input, string key, Encoding encoding) 254 { 255 return HashEncrypt(new HMACSHA512 (encoding.GetBytes(key)), input, encoding); 256 } 257 258 #endregion HMAC-SHA512 加密 259 260 #endregion 哈希加密演算法 261 262 #region 對稱加密演算法 263 264 #region Des 加解密 265 266 /// <summary> 267 /// DES 加密 268 /// </summary> 269 /// <param name="input"> 待加密的字元串 </param> 270 /// <param name="key"> 密鑰(8位) </param> 271 /// <returns></returns> 272 public static string DESEncrypt( string input, string key) 273 { 274 try 275 { 276 var keyBytes = Encoding .UTF8.GetBytes(key); 277 //var ivBytes = Encoding.UTF8.GetBytes(iv); 278 279 var des = DES .Create(); 280 des.Mode = CipherMode .ECB; //相容其他語言的 Des 加密演算法 281 des.Padding = PaddingMode .Zeros; //自動補 0 282 283 using (var ms = new MemoryStream ()) 284 { 285 var data = Encoding .UTF8.GetBytes(input); 286 287 using (var cs = new CryptoStream (ms, des.CreateEncryptor(keyBytes, IvBytes), CryptoStreamMode .Write) 288 ) 289 { 290 cs.Write(data, 0, data.Length); 291 cs.FlushFinalBlock(); 292 } 293 294 return Convert .ToBase64String(ms.ToArray()); 295 } 296 } 297 catch 298 { 299 return input; 300 } 301 } 302 303 /// <summary> 304 /// DES 解密 305 /// </summary> 306 /// <param name="input"> 待解密的字元串 </param> 307 /// <param name="key"> 密鑰(8位) </param> 308 /// <returns></returns> 309 public static string DESDecrypt( string input, string key) 310 { 311 try 312 { 313 var keyBytes = Encoding .UTF8.GetBytes(key); 314 //var ivBytes = Encoding.UTF8.GetBytes(iv); 315 316 var des = DES .Create(); 317 des.Mode = CipherMode .ECB; //相容其他語言的Des加密演算法 318 des.Padding = PaddingMode .Zeros; //自動補0 319 320 using (var ms = new MemoryStream ()) 321 { 322 var data = Convert .FromBase64String(input); 323 324 using (var cs = new CryptoStream (ms, des.CreateDecryptor(keyBytes, IvBytes), CryptoStreamMode .Write) 325 ) 326 { 327 cs.Write(data, 0, data.Length); 328 329 cs.FlushFinalBlock(); 330 } 331 332 return Encoding .UTF8.GetString(ms.ToArray()); 333 } 334 } 335 catch 336 { 337 return input; 338 } 339 } 340 341 #endregion Des 加解密 342 343 #endregion 對稱加密演算法 344 345 #region 非對稱加密演算法 346 347 /// <summary> 348 /// 生成 RSA 公鑰和私鑰 349 /// </summary> 350 /// <param name="publicKey"> 公鑰 </param> 351 /// <param name="privateKey"> 私鑰 </param> 352 public static void GenerateRSAKeys( out string publicKey, out string privateKey) 353 { 354 using (var rsa = new RSACryptoServiceProvider ()) 355 { 356 publicKey = rsa.ToXmlString( false ); 357 privateKey = rsa.ToXmlString( true ); 358 } 359 } 360 361 /// <summary> 362 /// RSA 加密 363 /// </summary> 364 /// <param name="publickey"> 公鑰 </param> 365 /// <param name="content"> 待加密的內容 </param> 366 /// <returns> 經過加密的字元串 </returns> 367 public static string RSAEncrypt( string publickey, string content) 368 { 369 var rsa = new RSACryptoServiceProvider(); 370 rsa.FromXmlString(publickey); 371 var cipherbytes = rsa.Encrypt(Encoding .UTF8.GetBytes(content), false); 372 373 return Convert .ToBase64String(cipherbytes); 374 } 375 376 /// <summary> 377 /// RSA 解密 378 /// </summary> 379 /// <param name="privatekey"> 私鑰 </param> 380 /// <param name="content"> 待解密的內容 </param> 381 /// <returns> 解密後的字元串 </returns> 382 public static string RSADecrypt( string privatekey, string content) 383 { 384 var rsa = new RSACryptoServiceProvider(); 385 rsa.FromXmlString(privatekey); 386 var cipherbytes = rsa.Decrypt(Convert .FromBase64String(content), false); 387 388 return Encoding .UTF8.GetString(cipherbytes); 389 } 390 391 #endregion 非對稱加密演算法 392 } 393 }
【博主】反骨仔
【出處】http://www.cnblogs.com/liqingwen/p/6155694.html