ajax實現md5加密

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   使用   sp   java   for   

一個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="width: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="width: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");
}
}
}

 

ajax實現md5加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.