Javascript encrypts and decrypts the parameter code in the url.

Source: Internet
Author: User

Javascript encrypts and decrypts the parameter code in the url.

Today, when I was working on an old project, I encountered a need to encrypt and decrypt the parameters in the url in javascript. I found this useful code from the Internet:

Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! -- Begin
Function Encrypt (str, pwd ){
If (str = "") return "";
Str = escape (str );
If (! Pwd | pwd = "") {var pwd = "1234 ";}
Pwd = escape (pwd );
If (pwd = null | pwd. length <= 0 ){
Alert ("Please enter a password with which to encrypt the message .");
Return null;
}
Var prand = "";
For (var I = 0; I <pwd. length; I ++ ){
Prand + = pwd. charCodeAt (I). toString ();
}
Var sPos = Math. floor (prand. length/5 );
Var mult = parseInt (prand. charAt (sPos) + prand. charAt (sPos * 2) + prand. charAt (sPos * 3) + prand. charAt (sPos * 4) + prand. charAt (sPos * 5 ));
Var incr = Math. ceil (pwd. length/2 );
Var modu = Math. pow (2, 31)-1;
If (mult <2 ){
Alert ("Algorithm cannot find a suitable hash. Please choose a different password./nPossible considerations are to choose a more complex or longer password .");
Return null;
}
Var salt = Math. round (Math. random () * 1000000000) % 100000000;
Prand + = salt;
While (prand. length> 10 ){
Prand = (parseInt (prand. substring (0, 10) + parseInt (prand. substring (10, prand. length). toString ();
}
Prand = (mult * prand + incr) % modu;
Var enc_chr = "";
Var enc_str = "";
For (var I = 0; I <str. length; I ++ ){
Enc_chr = parseInt (str. charCodeAt (I) ^ Math. floor (prand/modu) * 255 ));
If (enc_chr <16 ){
Enc_str + = "0" + enc_chr.toString (16 );
} Else
Enc_str + = enc_chr.toString (16 );
Prand = (mult * prand + incr) % modu;
}
Salt = salt. toString (16 );
While (salt. length <8) salt = "0" + salt;
Enc_str + = salt;
Return enc_str;
}
Function Decrypt (str, pwd ){
If (str = "") return "";
If (! Pwd | pwd = "") {var pwd = "1234 ";}
Pwd = escape (pwd );
If (str = null | str. length <8 ){
Alert ("A salt value cocould not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted .");
Return;
}
If (pwd = null | pwd. length <= 0 ){
Alert ("Please enter a password with which to decrypt the message .");
Return;
}
Var prand = "";
For (var I = 0; I <pwd. length; I ++ ){
Prand + = pwd. charCodeAt (I). toString ();
}
Var sPos = Math. floor (prand. length/5 );
Var mult = parseInt (prand. charAt (sPos) + prand. charAt (sPos * 2) + prand. charAt (sPos * 3) + prand. charAt (sPos * 4) + prand. charAt (sPos * 5 ));
Var incr = Math. round (pwd. length/2 );
Var modu = Math. pow (2, 31)-1;
Var salt = parseInt (str. substring (str. length-8, str. length), 16 );
Str = str. substring (0, str. length-8 );
Prand + = salt;
While (prand. length> 10 ){
Prand = (parseInt (prand. substring (0, 10) + parseInt (prand. substring (10, prand. length). toString ();
}
Prand = (mult * prand + incr) % modu;
Var enc_chr = "";
Var enc_str = "";
For (var I = 0; I <str. length; I + = 2 ){
Enc_chr = parseInt (str. substring (I, I + 2), 16) ^ Math. floor (prand/modu) * 255 ));
Enc_str + = String. fromCharCode (enc_chr );
Prand = (mult * prand + incr) % modu;
}
Return unescape (enc_str );
}
// End -->
</Script>

In the future, if you encounter encryption and decryption problems, simply write the above code into a js file. It's easy ....

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.