C# DES加密解密
前台layui布局:
<div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label">未加密密码</label> <div class="layui-input-block"> <input type="text" name="UnEncryptPwd" autocomplete="off" placeholder="请输入密码" class="layui-input" /> </div> </div> <div class="layui-inline"> <button type="button" id="EncryptPwd" class="layui-form-label layui-btn layui-btn-primary">加密</button> <div class="layui-input-block"> <input type="text" name="DecryptPwd" placeholder="" autocomplete="off" class="layui-input" /> </div> </div> </div> <div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label">已加密密码</label> <div class="layui-input-block"> <input type="text" name="EncryptPwd" autocomplete="off" placeholder="请输入密码" class="layui-input" /> </div> </div> <div class="layui-inline"> <button type="button" id="DecryptPwd" class="layui-form-label layui-btn layui-btn-primary">解密</button> <div class="layui-input-block"> <input type="text" name="txtDecryptPwd" placeholder="" autocomplete="off" class="layui-input" /> </div> </div> </div>
ajax发起请求:
<script type="text/javascript"> $(function () { //加密 $("#EncryptPwd").click(function () { var $unEncryptPwd = $("input[name=UnEncryptPwd]");//密码输入框 $.post("/Ashx/Login.ashx", { action: "EncryptPwd", unEncryptPwd: $unEncryptPwd.val()//未加密密码 }, function (data) { data = JSON.parse(data); $("input[name=DecryptPwd]").val(data.msg); }); }); //解密 $("#DecryptPwd").click(function () { var $EncryptPwd = $("input[name=EncryptPwd]");//密码输入框 $.post("/Ashx/Login.ashx", { action: "DecryptPwd", EncryptPwd: $EncryptPwd.val()//未加密密码 }, function (data) { data = JSON.parse(data); $("input[name=txtDecryptPwd]").val(data.msg); }); }); }); </script>
一般处理程序接受action并通过反射调用对应公共方法
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string action = context.Request.Params["action"]; MethodInfo mi = this.GetType().GetMethod(action); mi.Invoke(this, null); } public void EncryptPwd() { var context = HttpContext.Current; string unEncryptPwd = context.Request.Params["unEncryptPwd"]; string result = "{\"code\":1,\"msg\":\""+ DESEncrypt(unEncryptPwd) + "\"}"; context.Response.Write(result); } public void DecryptPwd() { var context = HttpContext.Current; string EncryptPwd = context.Request.Params["EncryptPwd"]; string result = "{\"code\":1,\"msg\":\"" + DESDecrypt(EncryptPwd) + "\"}"; context.Response.Write(result); } /// <summary> /// DES数据加密 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> private string DESEncrypt(string Text, string sKey = "pwdKey") { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); } /// <summary> /// DES数据解密 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> private string DESDecrypt(string pToDecrypt, string sKey = "pwdKey") { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }