一个asp.net ajax例子,使用jquery,实现md5加密。.NET 4.0,Visual Studio 2010以上。
效果体验:http://tool.keleyi.com/t/md5.htm
1. [图片] asp.net ajax 效果截图.png
2. [代码]前端代码HTML/Javascript/jQuery
<!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>
3. [代码][HTML]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>
</div>
</body>
</html>
4. [代码]aspx.cs后台C#代码
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");
}
}
}