Mayx's Home Page https://mabbs.github.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0 KiB

  1. <html>
  2. <head>
  3. <title>AES加解密</title>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  5. <script src="/assets/js/aes.js"></script>
  6. <script>
  7. function getByteLen(val) {
  8. var len = 0;
  9. for (var i = 0; i < val.length; i++) {
  10. if (val[i].match(/[^\x00-\xff]/ig) != null) len += 3;
  11. else len += 1;
  12. }
  13. return len;
  14. }
  15. function onbtnEncrypto() {
  16. var plaintText = document.getElementById("input").value;
  17. var keyword = document.getElementById("inputkey").value;
  18. if (plaintText.replace(/(^\s*)|(\s*$)/g, "") == '') {
  19. alert("输入要加密的内容!");
  20. return;
  21. }
  22. if (keyword.replace(/(^\s*)|(\s*$)/g, "") == '') {
  23. alert("输入要加密的key!");
  24. return;
  25. }
  26. while (getByteLen(keyword) % 8 != 0) {
  27. keyword = keyword + "\0";
  28. }
  29. var key = CryptoJS.enc.Utf8.parse(keyword);
  30. var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
  31. mode: CryptoJS.mode.ECB,
  32. padding: CryptoJS.pad.Pkcs7
  33. });
  34. encryptedData = encryptedData.ciphertext.toString();
  35. document.getElementById("output").value = encryptedData;
  36. }
  37. function onbtnDecrypto() {
  38. var encryptedData = document.getElementById("input").value;
  39. var keyword = document.getElementById("inputkey").value;
  40. if (encryptedData.replace(/(^\s*)|(\s*$)/g, "") == '') {
  41. alert("输入要加密的内容!");
  42. return;
  43. }
  44. if (keyword.replace(/(^\s*)|(\s*$)/g, "") == '') {
  45. alert("输入要加密的key!");
  46. return;
  47. }
  48. while (getByteLen(keyword) % 8 != 0) {
  49. keyword = keyword + "\0";
  50. }
  51. var key = CryptoJS.enc.Utf8.parse(keyword);
  52. var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
  53. var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  54. var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
  55. mode: CryptoJS.mode.ECB,
  56. padding: CryptoJS.pad.Pkcs7
  57. });
  58. if (decryptedData.sigBytes < 0) {
  59. document.getElementById("output").value = "解密失败!密文或者key错误!";
  60. return;
  61. }
  62. try {
  63. decryptedData.toString(CryptoJS.enc.Utf8)
  64. } catch(e) {
  65. document.getElementById("output").value = "解密失败!密文或者key错误!";
  66. return;
  67. }
  68. var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
  69. document.getElementById("output").value = decryptedStr;
  70. }
  71. </script>
  72. </head>
  73. <body>
  74. <div>
  75. <br />
  76. <textarea id="input" name="input" style="width: 80%" rows="8">这里是要加密的内容!</textarea>
  77. <br />key:
  78. <input id="inputkey" type="text" />
  79. <br />
  80. <p> <button id="en" class="btn" onclick="onbtnEncrypto()" style="width:100px">加密</button> <button id="de" class="btn" onclick="onbtnDecrypto()" style="width:100px">解密</button> </p>
  81. <textarea id="output" name="output" style="width: 80%" rows="10"></textarea>
  82. </div>
  83. </body>
  84. </html>

Powered by TurnKey Linux.