[C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
2022/7/1 14:23:19
本文主要是介绍[C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 #region 2 3 using System; 4 using System.IO; 5 using System.Security.Cryptography; 6 using System.Text; 7 8 #endregion 9 10 namespace Wen.Helpers.Common 11 { 12 /// <summary> 13 /// 安全助手 14 /// </summary> 15 public sealed class SecurityHelper 16 { 17 private static readonly byte[] IvBytes = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; 18 19 #region 通用加密算法 20 21 /// <summary> 22 /// 哈希加密算法 23 /// </summary> 24 /// <param name="hashAlgorithm"> 所有加密哈希算法实现均必须从中派生的基类 </param> 25 /// <param name="input"> 待加密的字符串 </param> 26 /// <param name="encoding"> 字符编码 </param> 27 /// <returns></returns> 28 private static string HashEncrypt(HashAlgorithm hashAlgorithm, string input, Encoding encoding) 29 { 30 var data = hashAlgorithm.ComputeHash(encoding.GetBytes(input)); 31 32 return BitConverter.ToString(data).Replace("-", ""); 33 } 34 35 /// <summary> 36 /// 验证哈希值 37 /// </summary> 38 /// <param name="hashAlgorithm"> 所有加密哈希算法实现均必须从中派生的基类 </param> 39 /// <param name="unhashedText"> 未加密的字符串 </param> 40 /// <param name="hashedText"> 经过加密的哈希值 </param> 41 /// <param name="encoding"> 字符编码 </param> 42 /// <returns></returns> 43 private static bool VerifyHashValue(HashAlgorithm hashAlgorithm, string unhashedText, string hashedText, 44 Encoding encoding) 45 { 46 return string.Equals(HashEncrypt(hashAlgorithm, unhashedText, encoding), hashedText, 47 StringComparison.OrdinalIgnoreCase); 48 } 49 50 #endregion 通用加密算法 51 52 #region 哈希加密算法 53 54 #region MD5 算法 55 56 /// <summary> 57 /// MD5 加密 58 /// </summary> 59 /// <param name="input"> 待加密的字符串 </param> 60 /// <param name="encoding"> 字符编码 </param> 61 /// <returns></returns> 62 public static string Md5Encrypt(string input, Encoding encoding) 63 { 64 return HashEncrypt(MD5.Create(), input, encoding); 65 } 66 67 /// <summary> 68 /// 验证 MD5 值 69 /// </summary> 70 /// <param name="input"> 未加密的字符串 </param> 71 /// <param name="encoding"> 字符编码 </param> 72 /// <returns></returns> 73 public static bool VerifyMd5Value(string input, Encoding encoding) 74 { 75 return VerifyHashValue(MD5.Create(), input, Md5Encrypt(input, encoding), encoding); 76 } 77 78 #endregion MD5 算法 79 80 #region SHA1 算法 81 82 /// <summary> 83 /// SHA1 加密 84 /// </summary> 85 /// <param name="input"> 要加密的字符串 </param> 86 /// <param name="encoding"> 字符编码 </param> 87 /// <returns></returns> 88 public static string Sha1Encrypt(string input, Encoding encoding) 89 { 90 return HashEncrypt(SHA1.Create(), input, encoding); 91 } 92 93 /// <summary> 94 /// 验证 SHA1 值 95 /// </summary> 96 /// <param name="input"> 未加密的字符串 </param> 97 /// <param name="encoding"> 字符编码 </param> 98 /// <returns></returns> 99 public static bool VerifySha1Value(string input, Encoding encoding) 100 { 101 return VerifyHashValue(SHA1.Create(), input, Sha1Encrypt(input, encoding), encoding); 102 } 103 104 #endregion SHA1 算法 105 106 #region SHA256 算法 107 108 /// <summary> 109 /// SHA256 加密 110 /// </summary> 111 /// <param name="input"> 要加密的字符串 </param> 112 /// <param name="encoding"> 字符编码 </param> 113 /// <returns></returns> 114 public static string Sha256Encrypt(string input, Encoding encoding) 115 { 116 return HashEncrypt(SHA256.Create(), input, encoding); 117 } 118 119 /// <summary> 120 /// 验证 SHA256 值 121 /// </summary> 122 /// <param name="input"> 未加密的字符串 </param> 123 /// <param name="encoding"> 字符编码 </param> 124 /// <returns></returns> 125 public static bool VerifySha256Value(string input, Encoding encoding) 126 { 127 return VerifyHashValue(SHA256.Create(), input, Sha256Encrypt(input, encoding), encoding); 128 } 129 130 #endregion SHA256 算法 131 132 #region SHA384 算法 133 134 /// <summary> 135 /// SHA384 加密 136 /// </summary> 137 /// <param name="input"> 要加密的字符串 </param> 138 /// <param name="encoding"> 字符编码 </param> 139 /// <returns></returns> 140 public static string Sha384Encrypt(string input, Encoding encoding) 141 { 142 return HashEncrypt(SHA384.Create(), input, encoding); 143 } 144 145 /// <summary> 146 /// 验证 SHA384 值 147 /// </summary> 148 /// <param name="input"> 未加密的字符串 </param> 149 /// <param name="encoding"> 字符编码 </param> 150 /// <returns></returns> 151 public static bool VerifySha384Value(string input, Encoding encoding) 152 { 153 return VerifyHashValue(SHA256.Create(), input, Sha384Encrypt(input, encoding), encoding); 154 } 155 156 #endregion SHA384 算法 157 158 #region SHA512 算法 159 160 /// <summary> 161 /// SHA512 加密 162 /// </summary> 163 /// <param name="input"> 要加密的字符串 </param> 164 /// <param name="encoding"> 字符编码 </param> 165 /// <returns></returns> 166 public static string Sha512Encrypt(string input, Encoding encoding) 167 { 168 return HashEncrypt(SHA512.Create(), input, encoding); 169 } 170 171 /// <summary> 172 /// 验证 SHA512 值 173 /// </summary> 174 /// <param name="input"> 未加密的字符串 </param> 175 /// <param name="encoding"> 字符编码 </param> 176 /// <returns></returns> 177 public static bool VerifySha512Value(string input, Encoding encoding) 178 { 179 return VerifyHashValue(SHA512.Create(), input, Sha512Encrypt(input, encoding), encoding); 180 } 181 182 #endregion SHA512 算法 183 184 #region HMAC-MD5 加密 185 186 /// <summary> 187 /// HMAC-MD5 加密 188 /// </summary> 189 /// <param name="input"> 要加密的字符串 </param> 190 /// <param name="key"> 密钥 </param> 191 /// <param name="encoding"> 字符编码 </param> 192 /// <returns></returns> 193 public static string HmacMd5Encrypt(string input, string key, Encoding encoding) 194 { 195 return HashEncrypt(new HMACMD5(encoding.GetBytes(key)), input, encoding); 196 } 197 198 #endregion HMAC-MD5 加密 199 200 #region HMAC-SHA1 加密 201 202 /// <summary> 203 /// HMAC-SHA1 加密 204 /// </summary> 205 /// <param name="input"> 要加密的字符串 </param> 206 /// <param name="key"> 密钥 </param> 207 /// <param name="encoding"> 字符编码 </param> 208 /// <returns></returns> 209 public static string HmacSha1Encrypt(string input, string key, Encoding encoding) 210 { 211 return HashEncrypt(new HMACSHA1(encoding.GetBytes(key)), input, encoding); 212 } 213 214 #endregion HMAC-SHA1 加密 215 216 #region HMAC-SHA256 加密 217 218 /// <summary> 219 /// HMAC-SHA256 加密 220 /// </summary> 221 /// <param name="input"> 要加密的字符串 </param> 222 /// <param name="key"> 密钥 </param> 223 /// <param name="encoding"> 字符编码 </param> 224 /// <returns></returns> 225 public static string HmacSha256Encrypt(string input, string key, Encoding encoding) 226 { 227 return HashEncrypt(new HMACSHA256(encoding.GetBytes(key)), input, encoding); 228 } 229 230 #endregion HMAC-SHA256 加密 231 232 #region HMAC-SHA384 加密 233 234 /// <summary> 235 /// HMAC-SHA384 加密 236 /// </summary> 237 /// <param name="input"> 要加密的字符串 </param> 238 /// <param name="key"> 密钥 </param> 239 /// <param name="encoding"> 字符编码 </param> 240 /// <returns></returns> 241 public static string HmacSha384Encrypt(string input, string key, Encoding encoding) 242 { 243 return HashEncrypt(new HMACSHA384(encoding.GetBytes(key)), input, encoding); 244 } 245 246 #endregion HMAC-SHA384 加密 247 248 #region HMAC-SHA512 加密 249 250 /// <summary> 251 /// HMAC-SHA512 加密 252 /// </summary> 253 /// <param name="input"> 要加密的字符串 </param> 254 /// <param name="key"> 密钥 </param> 255 /// <param name="encoding"> 字符编码 </param> 256 /// <returns></returns> 257 public static string HmacSha512Encrypt(string input, string key, Encoding encoding) 258 { 259 return HashEncrypt(new HMACSHA512(encoding.GetBytes(key)), input, encoding); 260 } 261 262 #endregion HMAC-SHA512 加密 263 264 #endregion 哈希加密算法 265 266 #region 对称加密算法 267 268 #region Des 加解密 269 270 /// <summary> 271 /// DES 加密 272 /// </summary> 273 /// <param name="input"> 待加密的字符串 </param> 274 /// <param name="key"> 密钥(8位) </param> 275 /// <returns></returns> 276 public static string DesEncrypt(string input, string key) 277 { 278 try 279 { 280 var keyBytes = Encoding.UTF8.GetBytes(key); 281 //var ivBytes = Encoding.UTF8.GetBytes(iv); 282 283 var des = DES.Create(); 284 des.Mode = CipherMode.ECB; //兼容其他语言的 Des 加密算法 285 des.Padding = PaddingMode.Zeros; //自动补 0 286 287 using (var ms = new MemoryStream()) 288 { 289 var data = Encoding.UTF8.GetBytes(input); 290 291 using (var cs = new CryptoStream(ms, des.CreateEncryptor(keyBytes, IvBytes), CryptoStreamMode.Write) 292 ) 293 { 294 cs.Write(data, 0, data.Length); 295 cs.FlushFinalBlock(); 296 } 297 298 return Convert.ToBase64String(ms.ToArray()); 299 } 300 } 301 catch 302 { 303 return input; 304 } 305 } 306 307 /// <summary> 308 /// DES 解密 309 /// </summary> 310 /// <param name="input"> 待解密的字符串 </param> 311 /// <param name="key"> 密钥(8位) </param> 312 /// <returns></returns> 313 public static string DesDecrypt(string input, string key) 314 { 315 try 316 { 317 var keyBytes = Encoding.UTF8.GetBytes(key); 318 //var ivBytes = Encoding.UTF8.GetBytes(iv); 319 320 var des = DES.Create(); 321 des.Mode = CipherMode.ECB; //兼容其他语言的Des加密算法 322 des.Padding = PaddingMode.Zeros; //自动补0 323 324 using (var ms = new MemoryStream()) 325 { 326 var data = Convert.FromBase64String(input); 327 328 using (var cs = new CryptoStream(ms, des.CreateDecryptor(keyBytes, IvBytes), CryptoStreamMode.Write) 329 ) 330 { 331 cs.Write(data, 0, data.Length); 332 333 cs.FlushFinalBlock(); 334 } 335 336 return Encoding.UTF8.GetString(ms.ToArray()); 337 } 338 } 339 catch 340 { 341 return input; 342 } 343 } 344 345 #endregion Des 加解密 346 347 #endregion 对称加密算法 348 349 #region 非对称加密算法 350 351 /// <summary> 352 /// 生成 RSA 公钥和私钥 353 /// </summary> 354 /// <param name="publicKey"> 公钥 </param> 355 /// <param name="privateKey"> 私钥 </param> 356 public static void GenerateRsaKeys(out string publicKey, out string privateKey) 357 { 358 using (var rsa = new RSACryptoServiceProvider()) 359 { 360 publicKey = rsa.ToXmlString(false); 361 privateKey = rsa.ToXmlString(true); 362 } 363 } 364 365 /// <summary> 366 /// RSA 加密 367 /// </summary> 368 /// <param name="publickey"> 公钥 </param> 369 /// <param name="content"> 待加密的内容 </param> 370 /// <returns> 经过加密的字符串 </returns> 371 public static string RsaEncrypt(string publickey, string content) 372 { 373 var rsa = new RSACryptoServiceProvider(); 374 rsa.FromXmlString(publickey); 375 var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); 376 377 return Convert.ToBase64String(cipherbytes); 378 } 379 380 /// <summary> 381 /// RSA 解密 382 /// </summary> 383 /// <param name="privatekey"> 私钥 </param> 384 /// <param name="content"> 待解密的内容 </param> 385 /// <returns> 解密后的字符串 </returns> 386 public static string RsaDecrypt(string privatekey, string content) 387 { 388 var rsa = new RSACryptoServiceProvider(); 389 rsa.FromXmlString(privatekey); 390 var cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); 391 392 return Encoding.UTF8.GetString(cipherbytes); 393 } 394 395 #endregion 非对称加密算法 396 } 397 }
这篇关于[C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#