JavaScript 和asp.net配合編碼字串

來源:互聯網
上載者:User

     .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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.