1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Web.Security; namespace HuaTong.General.Utility { /// <summary> /// 加密工具类 /// </summary> public class EncryptHelper { //默认密钥 private static string AESKey = "[45/*YUIdse..e;]"; private static string DESKey = "[&HdN72]"; /// <summary> /// AES加密 /// </summary> public static string AESEncrypt(string value, string _aeskey = null) { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = Encoding.UTF8.GetBytes(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> public static string AESDecrypt(string value, string _aeskey = null) { try { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = Convert.FromBase64String(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } /// <summary> /// DES加密 /// </summary> public static string DESEncrypt(string value, string _deskey = null) { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = Encoding.UTF8.GetBytes(value); DESCryptoServiceProvider rDel = new DESCryptoServiceProvider(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// DES解密 /// </summary> public static string DESDecrypt(string value, string _deskey = null) { try { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = Convert.FromBase64String(value); DESCryptoServiceProvider rDel = new DESCryptoServiceProvider(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } public static string MD5(string value) { byte[] result = Encoding.UTF8.GetBytes(value); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } public static string HMACMD5(string value, string hmacKey) { HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKey)); byte[] result = System.Text.Encoding.UTF8.GetBytes(value); byte[] output = hmacsha1.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } /// <summary> /// base64编码 /// </summary> /// <returns></returns> public static string Base64Encode(string value) { string result = Convert.ToBase64String(Encoding.Default.GetBytes(value)); return result; } /// <summary> /// base64解码 /// </summary> /// <returns></returns> public static string Base64Decode(string value) { string result = Encoding.Default.GetString(Convert.FromBase64String(value)); return result; } } } |