关键词搜索

源码搜索 ×
×

JavaScript笔记-前端AES加密

发布2022-04-08浏览5990次

详情内容

这里用最传统的方式进行AES加密

提供两种方式:

  1. console.log("方式一");
  2. var userName = document.getElementById("userName").value;
  3. var password = document.getElementById("password").value;
  4. var offset = document.getElementById("offset").value;
  5. var salt = document.getElementById("salt").value;
  6. var encrypted = CryptoJS.AES.encrypt(password, salt, {
  7. mode: CryptoJS.mode.ECB,
  8. padding: CryptoJS.pad.Pkcs7
  9. });
  10. console.log("原始数据 : ", password);
  11. console.log("vi : ", offset);
  12. console.log("salt : ", salt);
  13. document.getElementById("password").value = encrypted;
  14. console.log("ase加密 : ", encrypted);
  15. console.log("解密");
  16. let decrypted = CryptoJS.AES.decrypt(encrypted, salt, {
  17. mode: CryptoJS.mode.ECB,
  18. padding: CryptoJS.pad.Pkcs7
  19. });
  20. console.log("ase 解码 : " , decrypted.toString(CryptoJS.enc.Utf8));
  21. console.log("方式二");
  22. salt = CryptoJS.enc.Utf8.parse(salt);
  23. password = CryptoJS.enc.Utf8.parse(password);
  24. encrypted = CryptoJS.AES.encrypt(password, salt, {
  25. mode: CryptoJS.mode.ECB,
  26. padding: CryptoJS.pad.Pkcs7
  27. });
  28. console.log("密文 : ", encrypted.toString());
  29. console.log("解密");
  30. decrypted = CryptoJS.AES.decrypt(encrypted.toString(), salt, {
  31. mode: CryptoJS.mode.ECB,
  32. padding: CryptoJS.pad.Pkcs7
  33. });
  34. console.log("明文 : ", CryptoJS.enc.Utf8.stringify(decrypted).toString());

运行截图如下:

 完整代码如下:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:th="https://www.thymeleaf.org"
  4. xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  5. <head>
  6. <!-- Standard Meta -->
  7. <meta charset="utf-8" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  10. <!-- Site Properties -->
  11. <title>Login Example - Semantic</title>
  12. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/reset.css">
  13. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/site.css">
  14. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/container.css">
  15. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/grid.css">
  16. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/header.css">
  17. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/image.css">
  18. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/menu.css">
  19. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/divider.css">
  20. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/segment.css">
  21. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/form.css">
  22. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/input.css">
  23. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/button.css">
  24. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/list.css">
  25. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/message.css">
  26. <link rel="stylesheet" type="text/css" href="https://semantic-ui.com/dist/components/icon.css">
  27. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  28. <!-- 引入 CDN Crypto.js 开始 AES加密 注意引入顺序 -->
  29. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/core.min.js"></script>
  30. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-base64.min.js"></script>
  31. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/md5.min.js"></script>
  32. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/evpkdf.min.js"></script>
  33. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/cipher-core.min.js"></script>
  34. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/aes.min.js"></script>
  35. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/pad-pkcs7.min.js"></script>
  36. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/mode-ecb.min.js"></script>
  37. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-utf8.min.js"></script>
  38. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-hex.min.js"></script>
  39. <!-- 引入 CDN Crypto.js 结束 -->
  40. <style type="text/css">
  41. body {
  42. background-color: #DADADA;
  43. }
  44. body > .grid {
  45. height: 100%;
  46. }
  47. .image {
  48. margin-top: -100px;
  49. }
  50. .column {
  51. max-width: 450px;
  52. }
  53. </style>
  54. <script language="JavaScript">
  55. function login(){
  56. console.log("方式一");
  57. var userName = document.getElementById("userName").value;
  58. var password = document.getElementById("password").value;
  59. var offset = document.getElementById("offset").value;
  60. var salt = document.getElementById("salt").value;
  61. var encrypted = CryptoJS.AES.encrypt(password, salt, {
  62. mode: CryptoJS.mode.ECB,
  63. padding: CryptoJS.pad.Pkcs7
  64. });
  65. console.log("原始数据 : ", password);
  66. console.log("vi : ", offset);
  67. console.log("salt : ", salt);
  68. document.getElementById("password").value = encrypted;
  69. console.log("ase加密 : ", encrypted);
  70. console.log("解密");
  71. let decrypted = CryptoJS.AES.decrypt(encrypted, salt, {
  72. mode: CryptoJS.mode.ECB,
  73. padding: CryptoJS.pad.Pkcs7
  74. });
  75. console.log("ase 解码 : " , decrypted.toString(CryptoJS.enc.Utf8));
  76. console.log("方式二");
  77. salt = CryptoJS.enc.Utf8.parse(salt);
  78. password = CryptoJS.enc.Utf8.parse(password);
  79. encrypted = CryptoJS.AES.encrypt(password, salt, {
  80. mode: CryptoJS.mode.ECB,
  81. padding: CryptoJS.pad.Pkcs7
  82. });
  83. console.log("密文 : ", encrypted.toString());
  84. console.log("解密");
  85. decrypted = CryptoJS.AES.decrypt(encrypted.toString(), salt, {
  86. mode: CryptoJS.mode.ECB,
  87. padding: CryptoJS.pad.Pkcs7
  88. });
  89. console.log("明文 : ", CryptoJS.enc.Utf8.stringify(decrypted).toString());
  90. }
  91. </script>
  92. </head>
  93. <body>
  94. <div class="ui middle aligned center aligned grid">
  95. <div class="column">
  96. <h2 class="ui teal image header">
  97. <div class="content">
  98. Log-in to your account
  99. </div>
  100. </h2>
  101. <form action="" method="post" class="ui large form" onsubmit="login()">
  102. <div class="ui stacked segment">
  103. <div class="field">
  104. <div class="ui left icon input">
  105. <i class="user icon"></i>
  106. <input type="text" id="userName" name="userName" placeholder="userName">
  107. </div>
  108. </div>
  109. <div class="field">
  110. <div class="ui left icon input">
  111. <i class="lock icon"></i>
  112. <input type="password" id="password" name="password" placeholder="password">
  113. </div>
  114. </div>
  115. <button class="ui fluid large teal button submit">Login</button>
  116. </div>
  117. <input name="salt" id="salt" type="hidden" value="abc123abc123abc1">
  118. <input name="offset" id="offset" type="hidden" value="abcabcabcabcabca">
  119. <div class="ui error message"></div>
  120. </form>
  121. </div>
  122. </div>
  123. </body>
  124. </html>

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载