JAVA端封装
package com.zdgk.application.thirdparty.pay.compont;import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.net.util.Base64;
import org.springframework.core.env.Environment;
import org.springframework.util.Base64Utils;import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
public class AesUtil {private final int keySize = 128;private final int iterationCount = 147;private final Cipher cipher;private static String status = null;private static String passphase = null;public static String salt = null;public AesUtil() {try {cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {throw fail(e);}}public String encrypt(String salt, String iv, String passphrase, String plaintext) {try {SecretKey key = generateKey(salt, passphrase);byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));return base64(encrypted);} catch (UnsupportedEncodingException e) {throw fail(e);}}public String decrypt(String salt, String iv, String passphrase, String ciphertext) {try {SecretKey key = generateKey(salt, passphrase);byte[] text=base64(ciphertext);byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, text);return new String(decrypted, "UTF-8");} catch (UnsupportedEncodingException e) {throw fail(e);}}private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {try {cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));return cipher.doFinal(bytes);} catch (InvalidKeyException| InvalidAlgorithmParameterException| IllegalBlockSizeException| BadPaddingException e) {throw fail(e);}}private SecretKey generateKey(String salt, String passphrase) {try {SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");return key;} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {throw fail(e);}}public static String random(int length) {byte[] salt = new byte[length];new SecureRandom().nextBytes(salt);return hex(salt);}public static String base64(byte[] bytes) {try {return Base64.encodeBase64String(bytes);} catch (Exception e) {e.printStackTrace();}return null;}public static byte[] base64(String str) {try {String str1=str.replaceAll(" ","");return Base64.decodeBase64(str1);} catch (Exception e) {e.printStackTrace();}return null;}public static String hex(byte[] bytes) {return new String(Hex.encodeHex(bytes));}public static byte[] hex(String str) {try {return Hex.decodeHex(str.toCharArray());} catch (DecoderException e) {throw new IllegalStateException(e);}}private IllegalStateException fail(Exception e) {return new IllegalStateException(e);}public static String en(String originText,String iv) {if ("0".equals(status)) {return originText;}try {return new AesUtil().encrypt(salt,iv,passphase,originText);}catch (Exception e) {return "";}}public static String de(String aesText,String iv) {if ("0".equals(status)) {return aesText;}try {return new AesUtil().decrypt(salt,iv,passphase,aesText);}catch (Exception e) {e.printStackTrace();return "";}}public static void main(String[] args) throws Exception {String IV = "随机的IV"; String SALT = "随机SALT";String PLAIN_TEXT = "你是谁?";String PASSPHRASE = "铭文的KEY";AesUtil util = new AesUtil();String encrypt = util.encrypt(SALT, IV, PASSPHRASE, PLAIN_TEXT);System.out.println(encrypt);System.out.println(Base64Utils.encode(encrypt.getBytes()));String decrypt = util.decrypt(SALT, IV, PASSPHRASE, encrypt);System.out.println(decrypt);
}
}
VUE
安装依赖crypto-js
npm install crypto-js --save
import CryptoJS from 'crypto-js'let keySizes,iterationCounts;
let AesUtil = function (keySize, iterationCount) {keySizes = keySize / 32;iterationCounts = iterationCount;
};
const that = new AesUtil(128, 147);
AesUtil.prototype.generateKey = function (salt, passPhrase) {let key = CryptoJS.PBKDF2(passPhrase,CryptoJS.enc.Hex.parse(salt),{ keySize: keySizes, iterations: iterationCounts });return key;
}AesUtil.prototype.encrypt = function (salt, iv, passPhrase, plainText) {let key = that.generateKey(salt, passPhrase);let encrypted = CryptoJS.AES.encrypt(plainText,key,{ iv: CryptoJS.enc.Hex.parse(iv) });return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}AesUtil.prototype.decrypt = function (salt, iv, passPhrase, cipherText) {let key = that.generateKey(salt, passPhrase);let cipherParams = CryptoJS.lib.CipherParams.create({ciphertext: CryptoJS.enc.Base64.parse(cipherText)});let decrypted = CryptoJS.AES.decrypt(cipherParams,key,{ iv: CryptoJS.enc.Hex.parse(iv) });return decrypted.toString(CryptoJS.enc.Utf8);
}
export default {encrypt:that.encrypt,decrypt:that.decrypt,
}
导出来的对象里面encrypt和decrypt就是加密和解密函数
参考资料:点击跳转