时间:2021-05-28
public class TrialHelper { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为8位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string EncryptDES( string encryptString, string encryptKey = "" ) { try { if (string.IsNullOrEmpty(encryptKey) || encryptKey.Length < 8) { encryptKey = "winform01"; } byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES( string decryptString, string decryptKey = "" ) { try { if (string.IsNullOrEmpty(decryptKey) || decryptKey.Length < 8) { decryptKey = "winform01"; } byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } /// <summary> /// MD5数据加密 /// </summary> /// <param name="sDataIn">加密字段</param> /// <returns>加密后的字符串</returns> public static string GetMD5( string sDataIn ) { System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue, bytHash; bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn); bytHash = md5.ComputeHash(bytValue); md5.Clear(); string sTemp = ""; for (int i = 0; i < bytHash.Length; i++) { sTemp += bytHash[i].ToString("x").PadLeft(2, '0'); } return sTemp; } }
调用:
//获取登录信息 loginRecord.Name = tbName.Text.Trim(); loginRecord.MD5Pwd = TrialHelper.GetMD5(tbPwd.Password); //保存到数据库MD5加密方式 loginRecord.Pwd = TrialHelper.EncryptDES(tbPwd.Password);//记住密码des加密方式,保存到本地以上所述是小编给大家介绍的ASP.NET中DES加密与解密MD5加密帮助类,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了asp.net实现的MD5加密和DES加解密算法类。分享给大家供大家参考,具体如下:#regionMD5算法publicstringmd5(str
Asp.Net加密解密的方法如下: #regionDES加密解密//////DES加密//////待加密字串///32位Key值///加密后的字符串pub
1、md5加密,该加密算法是单向加密,即加密的数据不能再通过解密还原。相关类包含在java.security.MessageDigest包中。2、3-DES加密
当要进行MD5加密的字符串不含中文时,那么ASP.NET的加密结果和ASP是一致的:复制代码代码如下:Response.Write(FormsAuthentic
其实在ASP.Net编程中,不用调用md5.asp来加密数据。在DotNet中有自带的类: System.Web.Security.HashPasswor