A useful JavaScript encryption decryption

Source: Internet
Author: User
Tags decrypt pow

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
  1. <script language="JavaScript" >
  2. <!--Begin
  3. function Encrypt (str, pwd) {
  4. if (str=="")return "";
  5. str = Escape (str);
  6. if (!pwd | | pwd=="") { var pwd="1234";}
  7. PWD = Escape (pwd);
  8. if (pwd = = Null | | pwd.length <= 0) {
  9. Alert ("Please enter a password with which to encrypt the message.");
  10. return null;
  11. }
  12. var prand = "";
  13. For (var i=0; i<pwd.length; i++) {
  14. Prand + = Pwd.charcodeat (I). toString ();
  15. }
  16. var spos = Math.floor (PRAND.LENGTH/5);
  17. var mult = parseint (Prand.charat (Spos) + Prand.charat (spos*2) + Prand.charat (spos*3) + Prand.charat (spos*4) + PRAND.C    Harat (spos*5));
  18. var incr = Math.ceil (PWD.LENGTH/2);
  19. var modu = Math.pow (2, 31)-1;
  20. if (Mult < 2) {
  21. Alert ("algorithm cannot find a suitable hash. Please choose a different password. /npossible considerations is to choose a more complex or longer password. ");
  22. return null;
  23. }
  24. var salt = Math.Round (Math.random () * 1000000000)% 100000000;
  25. Prand + = salt;
  26. While (Prand.length >) {
  27. Prand = (parseint (prand.substring (0, ten)) + parseint (prand.substring (Prand.length)). ToString ();
  28. }
  29. Prand = (mult * prand + incr)% Modu;
  30. var enc_chr = "";
  31. var enc_str = "";
  32. For (var i=0; i<str.length; i++) {
  33. ENC_CHR = parseint (str.charcodeat (I) ^ Math.floor ((prand/modu) * 255));
  34. if (Enc_chr <) {
  35. Enc_str + = "0" + enc_chr.tostring (16);
  36. }Else
  37. Enc_str + = enc_chr.tostring (16);
  38. Prand = (mult * prand + incr)% Modu;
  39. }
  40. Salt = salt.tostring (16);
  41. while (Salt.length < 8) Salt = "0" + salt;
  42. Enc_str + = salt;
  43. return enc_str;
  44. }
  45. function Decrypt (str, pwd) {
  46. if (str=="")return "";
  47. if (!pwd | | pwd=="") { var pwd="1234";}
  48. PWD = Escape (pwd);
  49. if (str = = Null | | Str.length < 8) {
  50. Alert ("A salt value could not being extracted from the encrypted message because it's length is too short. The message cannot be decrypted. ");
  51. return;
  52. }
  53. if (pwd = = Null | | pwd.length <= 0) {
  54. Alert ("Please enter a password with which to decrypt the message.");
  55. return;
  56. }
  57. var prand = "";
  58. For (var i=0; i<pwd.length; i++) {
  59. Prand + = Pwd.charcodeat (I). toString ();
  60. }
  61. var spos = Math.floor (PRAND.LENGTH/5);
  62. var mult = parseint (Prand.charat (Spos) + Prand.charat (spos*2) + Prand.charat (spos*3) + Prand.charat (spos*4) + PRAND.C    Harat (spos*5));
  63. var incr = Math.Round (PWD.LENGTH/2);
  64. var modu = Math.pow (2, 31)-1;
  65. var salt = parseint (str.substring (str.length-8, str.length), 16);
  66. str = str.substring (0, str.length-8);
  67. Prand + = salt;
  68. While (Prand.length >) {
  69. Prand = (parseint (prand.substring (0, ten)) + parseint (prand.substring (Prand.length)). ToString ();
  70. }
  71. Prand = (mult * prand + incr)% Modu;
  72. var enc_chr = "";
  73. var enc_str = "";
  74. For (var i=0; i<str.length; i+=2) {
  75. ENC_CHR = parseint (parseint (str.substring (I, i+2), +) ^ Math.floor ((prand/modu) * 255));
  76. Enc_str + = String.fromCharCode (ENC_CHR);
  77. Prand = (mult * prand + incr)% Modu;
  78. }
  79. return unescape (ENC_STR);
  80. }
  81. End-To
  82. </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

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.