Encode strings with JavaScript and Asp.net

Source: Internet
Author: User

The. NET system. Text. asciiencoding and system. bitconvertor classes work together to encrypt strings on the server. The client uses JavaScript to decrypt strings. The Code is as follows:

<Script language = "JavaScript">

/*

========================================================== ============================

This function helps protect the email address from the edevil 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

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

// A two digit number (hex/base16). This function converts

// Series of numbers back into the normal form and returns

// 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

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

}

}

 

For details, see simple web based obfuscation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.