1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.Web.Security; namespace HashAndEncrypt { /// <summary> /// 多重混合哈希工具类 /// </summary> public sealed class HashHelper { private static readonly String hashKey = "qwer#&^Buaa06"; /// <summary> /// 对敏感数据进行多重混合哈希 /// </summary> /// <param name="source">待处理明文</param> /// <returns>Hasn后的数据</returns> public static String Hash(String source) { String hashCode = FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5") + FormsAuthentication.HashPasswordForStoringInConfigFile(hashKey, "MD5"); return FormsAuthentication.HashPasswordForStoringInConfigFile(hashCode, "SHA1"); } } } |
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 | using System; using System.Security.Cryptography; using System.Text; using System.Web.Security; namespace HashAndEncrypt { /// <summary> /// 工具类,封装了加解密相关操作 /// </summary> public sealed class EncryptHelper { private static readonly Byte[] DesKey = {5, 7, 8, 9, 0, 2, 1, 6}; private static readonly Byte[] DesVi = { 6, 9, 8, 5, 1, 6, 2, 8 }; /// <summary> /// 使用DES算法加密数据 /// </summary> /// <param name="data">待加密数据</param> /// <returns>密文</returns> public static String Encrypt(String data) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Encoding utf = new UTF8Encoding(); ICryptoTransform encryptor = des.CreateEncryptor(DesKey, DesVi); byte[] bData = utf.GetBytes(data); byte[] bEnc = encryptor.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bEnc); } /// <summary> /// 使用DES算法解密数据 /// </summary> /// <param name="data">待解密数据</param> /// <returns>明文</returns> public static String Decrypt(String data) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Encoding utf = new UTF8Encoding(); ICryptoTransform decryptor = des.CreateDecryptor(DesKey, DesVi); byte[] bEnc = Convert.FromBase64String(data); byte[] bDec = decryptor.TransformFinalBlock(bEnc, 0, bEnc.Length); return utf.GetString(bDec); } } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |