Simple page encryption and decryption by JavaScript

Source: Internet
Author: User

I recently read several pages and found that many of them are processed.
However, there is a page with encryption crpit processing encryption.
The following describes the encryption and decryption processes.

I. Encryption

1. convert each character in the string to a number

The method is the string processing method charcodeat (INDEX)
The charcodeat () method returns the Unicode encoding of characters at the specified position, that is, the string is digitalized, And the return value is an integer between 0 and 65535 of the corresponding characters.
For example:
 

VaR STR = "Hello world! "Var r = Str. charcodeat (0); document. Write (r); // The output is: 104.

2. After modifying the number, use the fromcharcode () method to restore the character
The fromcharcode () method is a string static method that can be specified to convert a number Unicode code to a character.
For example

$ = String. fromcharcode (r); document. Write ($); // The output is: H

The key to encryption is string. fromchshortcode (R!

You can change the value of R to deviate the output characters, for example, r = R + 2.
 

$ = String. fromcharcode (R + 2); document. Write ($); // The output is: J

Therefore,

Hello world! Changed to jgnnq "yqtnf #
<HTML> changed to> jvon @

Ii. decryption


1. Change the character to an uncoide-encoded number first.

 

VaR STR = "> jvon @"; var r = Str. charcodeat (0); document. Write (r); // output: 62

2. Use the encrypted Inverse Algorithm
 

$ = String. fromcharcode (R-2); document. Write ($); // The output is: <

Iii. complete code

<SCRIPT type = "text/JavaScript"> // ============================ ========/// encryption method // ========================================== === function encode (content) {var $ = ""; for (VAR u = 0; U <content. length; U ++) {var r = content. charcodeat (U); $ + = string. fromcharcode (R + 2);} return $ ;}; // ================================================================/// decryption method/ /==================================== function decode (content) {var $ = ""; for (VAR u = 0; U <content. length; U ++) {var r = content. charcodeat (U); $ + = string. fromcharcode (R-2);} return $ ;}; // ==============================================================/// test // ====================================== var STR = "hello "; document. write (encode (STR) + "<br>"); document. write (decode (encode (STR); </SCRIPT>

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.