关键词搜索

源码搜索 ×
×

Web前后端笔记-vue cli及java进行AES加解密

发布2020-05-19浏览986次

详情内容

这里我直接贴代码了。

vue cli中,要先导入

npm install crypto-js

vue代码如下:

  1. import axios from 'axios'
  2. import cryptoJS from "crypto-js/crypto-js";
  3. axios.defaults.timeout = 35 * 1000;
  4. /**
  5. * Get方法
  6. * @param {*} url
  7. * @param {*} params
  8. */
  9. export function fetch(url, params = {}) {
  10. return new Promise((resolve, reject) => {
  11. params['timestamp'] = new Date().getTime();
  12. let oriInfo = "";
  13. for (let key in params) {
  14. oriInfo += params[key] + "$";
  15. }
  16. let paraString = oriInfo.substr(0, oriInfo.length - 1);
  17. params['signature'] = this.$md5(paraString);
  18. let key = params['signature'].substring(0, 16);
  19. let shieldStr = encrypt(params['signature'], key)
  20. console.log("shield:" + shieldStr);
  21. console.log("origin:" + decrypt(shieldStr, key))
  22. params['shield'] = shieldStr;
  23. axios.get(url, {
  24. params: params
  25. }).then(response => {
  26. resolve(response);
  27. }).catch(err => {
  28. reject(err)
  29. })
  30. })
  31. }
  32. export function post(url, data = {}) {
  33. return new Promise((resolve, reject) => {
  34. axios.post(url, data)
  35. .then(response => {
  36. resolve(response);
  37. },
  38. err => {
  39. reject(err);
  40. })
  41. })
  42. }
  43. //aes加密
  44. export function encrypt(word, keyStr) {
  45. keyStr = keyStr ? keyStr : 'QWERTASDFGZXCVBN';
  46. let key = cryptoJS.enc.Utf8.parse(keyStr);
  47. let srcs = cryptoJS.enc.Utf8.parse(word);
  48. let encrypted = cryptoJS.AES.encrypt(srcs, key, {mode: cryptoJS.mode.ECB, padding: cryptoJS.pad.Pkcs7})
  49. return encrypted.toString();
  50. }
  51. //aes解密
  52. export function decrypt(word, keyStr) {
  53. keyStr = keyStr ? keyStr : 'QWERTASDFGZXCVBN';
  54. let key = cryptoJS.enc.Utf8.parse(keyStr);
  55. let decrypt = cryptoJS.AES.decrypt(word, key, {mode: cryptoJS.mode.ECB, padding: cryptoJS.pad.Pkcs7});
  56. return cryptoJS.enc.Utf8.stringify(decrypt).toString();
  57. }

上面这个vue的,AES用的是signature前16位做的AES。

下面是java端:

  1. public class AESUtil {
  2. public static String decrypt(String content, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
  3. //Key k = toKey(Base64.decodeBase64(key));
  4. Key k = toKey(key.getBytes());
  5. byte[] encoded = k.getEncoded();
  6. SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
  7. Cipher cipher = Cipher.getInstance("AES");
  8. cipher.init(Cipher.DECRYPT_MODE, aes);
  9. byte[] bytes = cipher.doFinal(Base64.decodeBase64(content));
  10. return new String(bytes, "UTF-8");
  11. }
  12. public static String encrypt(String data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
  13. //Key k = toKey(Base64.decodeBase64(key));
  14. Key k = toKey(key.getBytes());
  15. byte[] encoded = k.getEncoded();
  16. SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
  17. Cipher cipher = Cipher.getInstance("AES");
  18. cipher.init(Cipher.ENCRYPT_MODE, aes);
  19. byte[] bytes = cipher.doFinal(data.getBytes("UTF-8"));
  20. return Base64.encodeBase64String(bytes);
  21. }
  22. private static Key toKey(byte[] key){
  23. SecretKeySpec aes = new SecretKeySpec(key, "AES");
  24. return aes;
  25. }
  26. }

这里前端数据加密或解析然后发给后端,加密或解析。

都是一样的,在此记录下,以后越到此问题直接拷代码。

相关技术文章

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

提示信息

×

选择支付方式

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