Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET技巧

asp.net 字符串加密解密技术

来源:中文源码网    浏览:136 次    日期:2024-05-01 18:26:58
【下载文档:  asp.net 字符串加密解密技术.txt 】


asp.net 字符串加密解密技术
复制代码 代码如下:using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Security.Cryptography; using System.IO; namespace www { public partial class jiami : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Bind(); } private void Bind() { //加密 this.Title = DesEncrypt("pwd", "abcd1234"); this.Title += DesDecrypt(this.Title, "abcd1234"); Response.Write(DesDecrypt("2ikCw0TqKGo=", "abcd1234")); } /// /// 加密字符串 /// 注意:密钥必须为8位 /// /// 字符串 /// 密钥 /// 返回加密后的字符串 public string DesEncrypt(string inputString, string encryptKey) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (System.Exception error) { //return error.Message; return null; } } /// /// 解密字符串 /// /// 加了密的字符串 /// 密钥 /// 返回解密后的字符串 public string DesDecrypt(string inputString, string decryptKey) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new Byte[inputString.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(inputString); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch (System.Exception error) { //return error.Message; return null; } } } }

相关内容