这里我直接贴代码了。
vue cli中,要先导入
npm install crypto-js
vue代码如下:
- import axios from 'axios'
- import cryptoJS from "crypto-js/crypto-js";
-
- axios.defaults.timeout = 35 * 1000;
-
- /**
- * Get方法
- * @param {*} url
- * @param {*} params
- */
- export function fetch(url, params = {}) {
-
- return new Promise((resolve, reject) => {
-
- params['timestamp'] = new Date().getTime();
-
- let oriInfo = "";
- for (let key in params) {
-
- oriInfo += params[key] + "$";
- }
- let paraString = oriInfo.substr(0, oriInfo.length - 1);
- params['signature'] = this.$md5(paraString);
- let key = params['signature'].substring(0, 16);
- let shieldStr = encrypt(params['signature'], key)
- console.log("shield:" + shieldStr);
- console.log("origin:" + decrypt(shieldStr, key))
- params['shield'] = shieldStr;
-
- axios.get(url, {
-
- params: params
- }).then(response => {
-
- resolve(response);
- }).catch(err => {
-
- reject(err)
- })
- })
- }
-
-
- export function post(url, data = {}) {
-
- return new Promise((resolve, reject) => {
-
- axios.post(url, data)
- .then(response => {
-
- resolve(response);
- },
- err => {
-
- reject(err);
- })
- })
- }
-
-
- //aes加密
- export function encrypt(word, keyStr) {
-
- keyStr = keyStr ? keyStr : 'QWERTASDFGZXCVBN';
- let key = cryptoJS.enc.Utf8.parse(keyStr);
- let srcs = cryptoJS.enc.Utf8.parse(word);
- let encrypted = cryptoJS.AES.encrypt(srcs, key, {mode: cryptoJS.mode.ECB, padding: cryptoJS.pad.Pkcs7})
- return encrypted.toString();
- }
-
- //aes解密
- export function decrypt(word, keyStr) {
-
- keyStr = keyStr ? keyStr : 'QWERTASDFGZXCVBN';
- let key = cryptoJS.enc.Utf8.parse(keyStr);
- let decrypt = cryptoJS.AES.decrypt(word, key, {mode: cryptoJS.mode.ECB, padding: cryptoJS.pad.Pkcs7});
- return cryptoJS.enc.Utf8.stringify(decrypt).toString();
- }
上面这个vue的,AES用的是signature前16位做的AES。
下面是java端:
- public class AESUtil {
-
- public static String decrypt(String content, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
-
- //Key k = toKey(Base64.decodeBase64(key));
- Key k = toKey(key.getBytes());
- byte[] encoded = k.getEncoded();
- SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, aes);
-
- byte[] bytes = cipher.doFinal(Base64.decodeBase64(content));
-
- return new String(bytes, "UTF-8");
- }
-
- public static String encrypt(String data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
-
- //Key k = toKey(Base64.decodeBase64(key));
- Key k = toKey(key.getBytes());
- byte[] encoded = k.getEncoded();
- SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, aes);
- byte[] bytes = cipher.doFinal(data.getBytes("UTF-8"));
- return Base64.encodeBase64String(bytes);
- }
-
- private static Key toKey(byte[] key){
-
- SecretKeySpec aes = new SecretKeySpec(key, "AES");
- return aes;
- }
-
- }
这里前端数据加密或解析然后发给后端,加密或解析。
都是一样的,在此记录下,以后越到此问题直接拷代码。