开发者社区> 余二五> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Python全栈 Web(JavaScript DOM树、DOM对象、BOM对象)
外部对象: BOM和DOM BOM: Browser Object Module 浏览器对象模型 将浏览器比喻成一个对象-window (网页初始化自动创建) 可以通过window对象操作浏览器 DOM: Document Object .
2086 0
+关注
20377
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载