一个asp.net ajax例子,使用jquery,实现md5加密。在.NET 4.0,Visual Studio 2010上成功运行。
效果体验:http://tool.keleyi.com/t/md5.htm
前端代码(md5.htm):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>md5加密 使用asp.net jquery ajax-柯乐义</title> <script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.10.2.min.js"></script> <!--请连接网络--> </head> <body> <div> <h3>MD5加密</h3> 请输入要加密的字符串:<input type="text" id="input_keleyi_com" style="250px;"/> <br /> <span id="Md5Type"> <input id="Md5Type_1" type="radio" name="md5type_keleyi_com" value="1" checked="checked" /><label for="Md5Type_1">32位[大]</label> <input id="Md5Type_2" type="radio" name="md5type_keleyi_com" value="2" /><label for="Md5Type_2">32位[小]</label> <input id="Md5Type_3" type="radio" name="md5type_keleyi_com" value="3" /><label for="Md5Type_3">16位[大]</label> <input id="Md5Type_4" type="radio" name="md5type_keleyi_com" value="4" /><label for="Md5Type_4">16位[小]</label> </span><input type="button" value="加 密" onclick="javascript:Md5Ajax();" /> <br /> 结果:<input id="MD5Result_keleyi_com" type="text" style="450px" /> </div> <script type="text/javascript"> function Md5Ajax() { var k_input = $("#input_ke" + "leyi_com").val(); var k_format = $('input[name="md5type_keleyi_com"]:checked').val(); $.ajax({ type: "Post", url: "KeleyiMd5.aspx/GetMd5", data: "{'input':'" + k_input + "','format':'" + k_format + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if ((String)(data.d) != "-1") { $("#MD5Result_k"+"eleyi_com").val(data.d); } else alert('加密失败'); }, error: function (err) { alert('柯乐义提醒您:出错了' + err); } }); } </script> </body> </html>
以上这个文件使用了jquery的ajax方法,详细请参考:http://keleyi.com/a/bjac/otjlqc2l.htm以及http://keleyi.com/a/bjac/c0me6l9l.htm
更多参考:http://keleyi.com/a/bjac/qkgckany.htm (JavaScript AJAX实例)
KeleyiMd5.aspx文件代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="KeleyiMd5.aspx.cs" Inherits="Keleyi.Com.KeleyiAjax.KeleyiMd5" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>asp.net jquery ajax示例之md5加密-柯乐义</title> </head> <body> <div> <a href="http://tool.keleyi.com/t/md5.htm">查看效果</a> <br /> <img src="http://keleyi.com/image/a/tvh00n12.jpg" alt="康逸琨" /> </div> </body> </html>
本示例是asp.net的ajax示例,了解asp.net mvc的ajax实现请参考:http://keleyi.com/a/bjac/781ba7719ce323f.htm
后台代码(KeleyiMd5.aspx.cs):
using System; using System.Web.Services; namespace Keleyi.Com.KeleyiAjax { public partial class KeleyiMd5 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="input"></param> /// <param name="format">1表示32位大写,2表示32位小写,3表示16位大写,4表示16位小写</param> /// <returns></returns> [WebMethod] public static string GetMd5(string input, int format) { switch (format) { case 1: return GetMd5Upper32(input); case 2: return GetMd5Upper32(input).ToLower(); case 3: return GetMd5Upper32(input).Substring(8, 16); case 4: return GetMd5Upper32(input).Substring(8, 16).ToLower(); default: return GetMd5Upper32(input); } } static string GetMd5Upper32(string input) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "MD5"); } } }