JavaScript encrypts the parameters in the URL to decrypt the code _javascript tips

Source: Internet
Author: User
Tags decrypt pow

Today, while doing an old project, I encountered a requirement to encrypt the parameters in the URL in JavaScript, and found this useful code from the Internet:

Copy Code code 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");
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.charA T (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)) + parseint (prand.substring (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 could not to be extracted to 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");
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.charA T (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)) + parseint (prand.substring (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 (parseint (str.substring (I, i+2),) ^ Math.floor ((prand/modu) * 255));
Enc_str + + string.fromcharcode (ENC_CHR);
Prand = (mult * prand + incr)% Modu;
}
return unescape (ENC_STR);
}
End-->
</script>

Later encountered encryption and decryption problem, directly to write the above code a JS file, it is done. It's convenient ....

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.