Today, when I was doing an old project, there was a need to decrypt the parameters in the URL in JavaScript and find this useful code from the Internet:
[JavaScript]View Plaincopy
- <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.C Harat (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 is to choose a more complex or longer password. ");
- return null;
- }
- var salt = Math.Round (Math.random () * 1000000000)% 100000000;
- Prand + = salt;
- While (Prand.length >) {
- Prand = (parseint (prand.substring (0, ten)) + 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 <) {
- 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 being 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.C Harat (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 >) {
- Prand = (parseint (prand.substring (0, ten)) + 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-To
- </script>
After encountering the encryption and decryption problem, directly write the above code into a JS file, it is done. It's convenient ....
A useful JavaScript encryption decryption