JavaScript 和asp.net配合编码字符串
2007-01-31 10:27
260 查看
.net 的System.Text.ASCIIEncoding 和System.BitConvertor类配合在服务端加密字符串,客户端使用Javascript解密字符串。代码如下:
<script language="javascript">
/*
==========================================================
This function helps protect the email address from the evil spam-bots
that scan web pages for useful data such as (email addresses).
Instead of using the data directly, the encoded value is stored in the
html and decoded when required.
==========================================================
*/
function decode(ServerEncoded)
{
// The ServerEncoded parameter is a string that contains the encoded data.
// Each character in the ServerEncoded parameter has been converted into
// a two digit number (hex / base16). This function converts the
// series of numbers back into the normal form and returns the
// decoded string to the client
// holds the decoded string
var res = "";
// go through and decode the full input server encoded string
for (i=0; i < ServerEncoded.length;)
{
// holds each letter (2 digits)
var letter = "";
letter = ServerEncoded.charAt(i) + ServerEncoded.charAt(i+1)
// build the real decoded value
res += String.fromCharCode(parseInt(letter,16));
i += 2;
}
//return the new decoded string to allow it to be rendered to screen
return res;
}
/*
==========================================================
This function gets a reference to the server encrypted string and
then decrypts this using the decode() function and sets the
txtDecrypted value to the value return by the decode() function
==========================================================
*/
function GetEmailAndDecode() {
//get the table <A class=iAs style="FONT-WEIGHT: normal; FONT-SIZE: 100%; PADDING-BOTTOM: 1px; COLOR: darkgreen; BORDER-BOTTOM: darkgreen 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="#" target=_blank itxtdid="3146774">element</A>
var txtSvrEncr = document.getElementById('txtServerEncrypted');
var txtJSDecr = document.getElementById('txtDecrypted');
txtJSDecr.value = decode(txtSvrEncr.value);
var txtAllTog = document.getElementById('txtAllTogether');
txtAllTog.value = decode(txtAllTog.value);
}
</script>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Email Encryption
//if javascript is enabled do the encoding
if (Request.Browser.JavaScript)
{
//do the encryption using the raw email
txtServerEncrypted.Text = System.BitConverter.ToString(
System.Text.ASCIIEncoding.ASCII.GetBytes(
(txtRawEmail.Text))).Replace("-", "");
//do the encryption using the raw email
txtAllTogether.Text = System.BitConverter.ToString(
System.Text.ASCIIEncoding.ASCII.GetBytes(
(txtRawEmail.Text))).Replace("-", "");
}
else
{
//couldnt find javascript so just use normal email
txtServerEncrypted.Text = txtRawEmail.Text;
txtAllTogether.Text = txtRawEmail.Text;
}
#endregion
}
}
具体参见:Simple web based obfuscation
<script language="javascript">
/*
==========================================================
This function helps protect the email address from the evil spam-bots
that scan web pages for useful data such as (email addresses).
Instead of using the data directly, the encoded value is stored in the
html and decoded when required.
==========================================================
*/
function decode(ServerEncoded)
{
// The ServerEncoded parameter is a string that contains the encoded data.
// Each character in the ServerEncoded parameter has been converted into
// a two digit number (hex / base16). This function converts the
// series of numbers back into the normal form and returns the
// decoded string to the client
// holds the decoded string
var res = "";
// go through and decode the full input server encoded string
for (i=0; i < ServerEncoded.length;)
{
// holds each letter (2 digits)
var letter = "";
letter = ServerEncoded.charAt(i) + ServerEncoded.charAt(i+1)
// build the real decoded value
res += String.fromCharCode(parseInt(letter,16));
i += 2;
}
//return the new decoded string to allow it to be rendered to screen
return res;
}
/*
==========================================================
This function gets a reference to the server encrypted string and
then decrypts this using the decode() function and sets the
txtDecrypted value to the value return by the decode() function
==========================================================
*/
function GetEmailAndDecode() {
//get the table <A class=iAs style="FONT-WEIGHT: normal; FONT-SIZE: 100%; PADDING-BOTTOM: 1px; COLOR: darkgreen; BORDER-BOTTOM: darkgreen 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="#" target=_blank itxtdid="3146774">element</A>
var txtSvrEncr = document.getElementById('txtServerEncrypted');
var txtJSDecr = document.getElementById('txtDecrypted');
txtJSDecr.value = decode(txtSvrEncr.value);
var txtAllTog = document.getElementById('txtAllTogether');
txtAllTog.value = decode(txtAllTog.value);
}
</script>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Email Encryption
//if javascript is enabled do the encoding
if (Request.Browser.JavaScript)
{
//do the encryption using the raw email
txtServerEncrypted.Text = System.BitConverter.ToString(
System.Text.ASCIIEncoding.ASCII.GetBytes(
(txtRawEmail.Text))).Replace("-", "");
//do the encryption using the raw email
txtAllTogether.Text = System.BitConverter.ToString(
System.Text.ASCIIEncoding.ASCII.GetBytes(
(txtRawEmail.Text))).Replace("-", "");
}
else
{
//couldnt find javascript so just use normal email
txtServerEncrypted.Text = txtRawEmail.Text;
txtAllTogether.Text = txtRawEmail.Text;
}
#endregion
}
}
具体参见:Simple web based obfuscation
相关文章推荐
- JavaScript 和asp.net配合编码字符串
- JavaScript 和asp.net配合编码字符串
- 在asp.net mvc中使用百度UEditor初始化内容遇到自数据库中读取的html字符串在UEditor中始终是html编码的问题
- ASP.NET MVC Razor 输出没有编码的HTML字符串
- javascript中文编码/解析与C#ASP.NET中文编码/解析 C#: Server.UrlEncode() HttpUtility.UrlEncode() javascript: escape() encodeURI() encodeURICom
- asp.net 字符串编码
- asp.net与js中字符串的HTML编码与解码
- [C#/asp.net]字符串编码、解码以及编码转换
- ASP.NET MVC Razor 输出没有编码的HTML字符串
- 将 JavaScript 与 ASP.NET 2.0 配合使用
- 将 JavaScript 与 ASP.NET 2.0 配合使用
- ASP.NET MVC Razor 输出没有编码的HTML字符串
- asp.net使用unescape读取js escape编码过的字符串
- 将 JavaScript 与 ASP.NET 2.0 配合使用 (ZT)
- 将 JavaScript 与 ASP.NET 2.0 配合使用
- 将 JavaScript 与 ASP.NET 2.0 配合使用
- asp.net 字符串、二进制、编码数组转换函数
- asp.net mvc 返回字符串的动作编码tip
- asp.net 字符串、二进制、编码数组转换函数
- asp.net 转换GB2312 的字符串为UTF8编码