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,如需转载请自行联系原作者
目录
相关文章
|
10月前
|
JavaScript
开发中的编码和解码(js的问题)
开发中的编码和解码(js的问题)
48 0
|
5月前
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
149 8
|
7月前
|
API
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
|
7月前
|
开发框架 JavaScript 前端开发
揭秘:如何让你的asp.net页面变身交互魔术师——先施展JavaScript咒语,再引发服务器端魔法!
【8月更文挑战第16天】在ASP.NET开发中,处理客户端与服务器交互时,常需先执行客户端验证再提交数据。传统上使用ASP.NET Button控件直接触发服务器事件,但难以插入客户端逻辑。本文对比此法与改进方案:利用HTML按钮及JavaScript手动控制表单提交。后者通过`onclick`事件调用JavaScript函数`SubmitForm()`来检查输入并决定是否提交,增强了灵活性和用户体验,同时确保了服务器端逻辑的执行。
80 5
|
7月前
|
JavaScript 前端开发
JS - 如何上传 Base64 编码的图片
本文展示了如何将Base64编码的图片在前端转换为Blob对象,并使用`FormData`上传到服务器端的方法。
258 0
|
8月前
|
JavaScript 前端开发 网络架构
JavaScript编码之路【对象的增强、ES6新特性之函数的默认值设置 、rest参数 (剩余参数)、拓展运算符、对象与数组的解构赋值】
JavaScript编码之路【对象的增强、ES6新特性之函数的默认值设置 、rest参数 (剩余参数)、拓展运算符、对象与数组的解构赋值】
77 1
|
8月前
|
存储 JavaScript 前端开发
JavaScript编码之路【ES6新特性之 Symbol 、Set 、Map、迭代器、生成器】(二)
JavaScript编码之路【ES6新特性之 Symbol 、Set 、Map、迭代器、生成器】(二)
81 1
|
8月前
|
JavaScript 前端开发 索引
JavaScript编码之路 【JavaScript之操作数组、字符串方法汇总】(三)
JavaScript编码之路 【JavaScript之操作数组、字符串方法汇总】(三)
68 1
|
9月前
|
JavaScript 前端开发 安全
安全开发-JS应用&原生开发&JQuery库&Ajax技术&加密编码库&断点调试&逆向分析&元素属性操作
安全开发-JS应用&原生开发&JQuery库&Ajax技术&加密编码库&断点调试&逆向分析&元素属性操作
|
8月前
|
存储 JavaScript 前端开发
JavaScript编码之路【ES6新特性之 Symbol 、Set 、Map、迭代器、生成器】(一)
JavaScript编码之路【ES6新特性之 Symbol 、Set 、Map、迭代器、生成器】(一)
57 0

热门文章

最新文章