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,如需转载请自行联系原作者
目录
相关文章
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
587 8
|
JavaScript 前端开发
JS几种拼接字符串的方法
JS几种拼接字符串的方法
476 1
|
开发框架 JavaScript 前端开发
精选HTML、JavaScript、ASP代码片段集锦
这些代码片段代表了HTML, JavaScript和ASP的基本应用,可被集成到更复杂的项目中。它们注重实用性,并且易于理解,旨在帮助开发者快速开始项目构建或进行学习。尽管ASP不如其他服务器端技术(如Node.js, PHP, Ruby等)现代,但它在遗留系统中仍非常普遍,了解基础仍具有价值。
411 14
|
JavaScript 前端开发 数据处理
模板字符串和普通字符串在浏览器和 Node.js 中的性能表现是否一致?
综上所述,模板字符串和普通字符串在浏览器和 Node.js 中的性能表现既有相似之处,也有不同之处。在实际应用中,需要根据具体的场景和性能需求来选择使用哪种字符串处理方式,以达到最佳的性能和开发效率。
475 63
|
JavaScript 前端开发 开发者
JavaScript字符串的常用方法
在JavaScript中,字符串处理是一个非常常见的任务。JavaScript提供了丰富的字符串操作方法,使开发者能够高效地处理和操作字符串。本文将详细介绍JavaScript字符串的常用方法,并提供示例代码以便更好地理解和应用这些方法。
389 13
|
JavaScript 前端开发 索引
JavaScript学习第二章--字符串
本文介绍了JavaScript中的字符串处理,包括普通字符串和模板字符串的使用方法及常见字符串操作方法如`charAt`、`concat`、`endsWith`等,适合前端学习者参考。作者是一位热爱前端技术的大一学生,专注于分享实用的编程技巧。
254 2
|
JavaScript 前端开发
javascript创建字符串
javascript创建字符串
|
存储 JavaScript 前端开发
JavaScript 字符串(String) 对象
JavaScript 字符串(String) 对象
221 3
|
JavaScript 前端开发
如何在JavaScript中替换字符串:一篇详细指南
如何在JavaScript中替换字符串:一篇详细指南
|
JavaScript 前端开发 C++
JavaScript用indexOf()在字符串数组中查找子串时需要注意的一个地方
JavaScript用indexOf()在字符串数组中查找子串时需要注意的一个地方

热门文章

最新文章