JavaScript 和asp.net配合编码字符串

简介:
 .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
    }
}
 





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/74411,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
|
1月前
|
JavaScript 前端开发
JS几种拼接字符串的方法
JS几种拼接字符串的方法
48 1
|
3月前
|
存储 JavaScript 前端开发
JS上传文件(base64字符串和二进制文件流)
这篇文章介绍了两种JavaScript文件上传的方法:使用FileReader对象将文件读取为base64字符串上传,以及使用FormData对象以二进制文件流的形式上传文件,包括如何处理文件选择、读取和上传的详细代码示例。
437 2
JS上传文件(base64字符串和二进制文件流)
|
2月前
|
JavaScript
js 解析 byte数组 成字符串
js 解析 byte数组 成字符串
68 5
|
3月前
|
API
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
|
1月前
|
存储 JavaScript 前端开发
JavaScript 字符串(String) 对象
JavaScript 字符串(String) 对象
43 3
|
2月前
|
JavaScript 前端开发
javascript创建字符串
javascript创建字符串
|
2月前
|
JavaScript 前端开发
如何在JavaScript中替换字符串:一篇详细指南
如何在JavaScript中替换字符串:一篇详细指南
|
2月前
|
存储 JavaScript
js切割截取字符串方法
js切割截取字符串方法
46 2
|
2月前
|
Web App开发 JavaScript 前端开发
JavaScript 模板字符串
JavaScript 模板字符串
28 3