1.前端加密,后台解密
1)前端加密(vue)
- 注意:需要在package.json中添加如下依赖
"dependencies": {
"crypto-js": "^4.0.0",
"jsencrypt": "^3.0.0-rc.1"
}
- 注意:在main.js中引入使用,例如我的加解密js工具类叫做cryptUtils.js
import CryptoJS from './utils/cryptUtils'
Vue.prototype.cryptoJS = CryptoJS
import {
JSEncrypt } from 'jsencrypt'
Vue.prototype.JSEncrypt = JSEncrypt
import CryptoJS from 'crypto-js'
export default {
encryptData(queryParam){
var AES_KEY=this.generator();
var AES_IV=this.generator();
var PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCKrO17v4IfHkDB1glq0v4Xd61gkdlritFtv8KjRZr7tH1OexNKS6buS3AA8vHSj3IGvcl6bQsn5HRPOHhS9uiFsG6eYJPNIbtX6/+zkw/v8F3srsVTNeGYpWMHyGyN+HQ3WTZgRf6cu+AQEcOspltVlK4My7Luf0dE79u5xCN2JwIDAQAB";
var jsEncrypt = new JSEncrypt();
jsEncrypt.setPublicKey(PUBLIC_KEY);
var aesKey = jsEncrypt.encrypt(AES_KEY);
var aesIv = jsEncrypt.encrypt(AES_IV);
var json = JSON.stringify(queryParam);
json =this.encrypt(json, AES_KEY, AES_IV);
var returnMap = new Map();
returnMap.put("aesKey",aesKey);
returnMap.put("aesIv",aesIv);
returnMap.put("json",json);
return returnMap;
},
encrypt(word, keyStr, ivkey) {
var key = CryptoJS.enc.Utf8.parse(keyStr)
var iv = CryptoJS.enc.Utf8.parse(ivkey);
var srcs = CryptoJS.enc.Utf8.parse(word)
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString()
},
generator(){
var num = '';
for(var i=0; i<16; i++){
num += Math.floor(Math.random()*16).toString(16);
}
return num;
}
}
2)后台解密(java)
String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIqs7Xu/gh8eQMHWCWrS/hd3rWCR2WuK0W2/wqNFmvu0fU57E0pLpu5LcADy8dKPcga9yXptCyfkdE84eFL26IWwbp5gk80hu1fr/7OTD+/wXeyuxVM14ZilYwfIbI34dDdZNmBF/py74BARw6ymW1WUrgzLsu5/R0Tv27nEI3YnAgMBAAECgYA46en5nMp5nP/0zbRFvFx+Iybr+E/kPC/hvA7U1Y9a+GNpH0f2eQAE0r8gFJU81pR0wdV7wl8Ou0sk7EctvZoGCw2R8PU0iMOTYjidzXE38MeXicKeDL2NlJ6NCDE+QfGDaA6kHnJTIppyKKolkoYE5joXRtlHGXxp58/JG9sJUQJBAPuTFiviz/N4ACY1d6gz8gvaLE/M5PE/eLbKjEIdgzoAu0ToMsKYvth1/pf7Fy4bxCKYLcgVZ3/latFihVeJ6/8CQQCNHWx8KLfVmpJuxNF122EFO1Pa9QtRV+R2laZH76GgsPAOw/zAt4E7u+A9OAthFf2L3oU5TzcntEgpXWAiIZXZAkEAhGFTy2ggsSc2POTpwDMFbUMzChLxbhlONjHDOI87HGyCHrJo84lY86Kzg9n2yqaFwQZ/IMhSnDRmzyOesv+6lQJABPpswVSOZ7K0R8QZv5alrZGSO785Tv1SyfwtiYwin5CeZ2bym7ndjUXrez1m6V3lO/H1sTfiMjxh71Oj+JBGwQJBAN3hUa6n5QBcc8avOCY7rV0GHNu63bDLTk8ATI3S7P31mkQYKlBtLFd1lnBsjeRb/Vwn8d6eBJ+BYXx7McFcGk0=";
aesKey = decryptRSA(aesKey, privateKey);
aesIv = decryptRSA(aesIv, privateKey);
json = decryptAES(json, aesKey, aesIv);
private String decryptRSA(String data, String key){
try{
byte[] inputByte = Base64.decodeBase64(data.getBytes("UTF-8"));
byte[] decoded = Base64.decodeBase64(key);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;
}catch (Exception ex){
ex.printStackTrace();
}
return data;
}
private String decryptAES(String data, String key, String iv){
try{
key = AESUtil.byteArrayTransferToString(key.getBytes());
iv = AESUtil.byteArrayTransferToString(iv.getBytes());
data = AESUtil.decrypt(data, key, iv);
}catch (Exception ex){
ex.printStackTrace();
}
return data;
}
2.后台加密,前端解密
1)后台加密
package com.wonders.platform.comm.utils;
import com.wonders.common.utils.AESUtil;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class EncryptUtils {
private static Map<Integer, String> keyMap = new HashMap<Integer, String>(2);
public static Map<String,String> encrypt(String json){
Map<String,String> result = new HashMap<String,String>(3);
try{
genKeyPair();
String publicKey = keyMap.get(0);
String aesKey = randomHexString(16);
String aesIv = randomHexString(16);
json = encryptAES(json, aesKey, aesIv);
aesKey = encryptRSA(aesKey, publicKey);
aesIv = encryptRSA(aesIv, publicKey);
result.put("json",json);
result.put("aesKey",aesKey);
result.put("aesIv",aesIv);
result.put("privateKey",keyMap.get(1));
return result;
}catch (Exception ex){
ex.printStackTrace();
}
result.put("json",json);
return result;
}
public static void genKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024,new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
keyMap.put(0,publicKeyString);
keyMap.put(1,privateKeyString);
}
public static String encryptAES(String data, String key, String iv){
try{
key = AESUtil.byteArrayTransferToString(key.getBytes());
iv = AESUtil.byteArrayTransferToString(iv.getBytes());
data = AESUtil.encrypt(AESUtil.byteArrayTransferToString(data.getBytes()), key, iv);
}catch (Exception ex){
ex.printStackTrace();
}
return data;
}
public static String randomHexString(int len) {
try {
StringBuffer result = new StringBuffer();
for(int i=0;i<len;i++) {
result.append(Integer.toHexString(new Random().nextInt(16)));
}
return result.toString().toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encryptRSA(String data, String publicKey) {
try{
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Base64.encodeBase64String(cipher.doFinal(data.getBytes("UTF-8")));
return outStr;
}catch (Exception ex){
ex.printStackTrace();
}
return data;
}
}
2)前端解密
import CryptoJS from 'crypto-js'
export default {
decryptData(privateKey,aesKey,aesIv,json){
var jsEncrypt = new JSEncrypt();
jsEncrypt.setPrivateKey(privateKey);
aesKey = jsEncrypt.decrypt(aesKey);
aesIv = jsEncrypt.decrypt(aesIv);
json = this.decrypt(json, aesKey, aesIv);
return JSON.parse(json);
},
decrypt(word, keyStr, ivStr) {
var key = CryptoJS.enc.Utf8.parse(keyStr);
var iv = CryptoJS.enc.Utf8.parse(ivStr);
var decrypt = CryptoJS.AES.decrypt(word, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypt.toString(CryptoJS.enc.Utf8);
}
}
ps:工具类AESUtil.java
package com.wonders.common.utils;
import java.security.Security;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AESUtil {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String ENCODING = "UTF-8";
public static final String ALGORITHM_AES = "AES";
public static final String KEY_AES = "AES";
public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
private static final String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String CBC_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
public static final int CODE_BASE64 = 1;
public static final int CODE_HEX = 2;
public AESUtil() {
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src != null && src.length > 0) {
for(int i = 0; i < src.length; ++i) {
int v = src[i] & 255;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
} else {
return null;
}
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString != null && !hexString.equals("")) {
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for(int i = 0; i < length; ++i) {
int pos = i * 2;
d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
} else {
return null;
}
}
private static byte charToByte(char c) {
return (byte)"0123456789ABCDEF".indexOf(c);
}
public static String encode(String secret, String iv, String content) throws Exception {
byte[] secretBytes = secret.getBytes();
SecretKey secretKey = new SecretKeySpec(secretBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(1, secretKey, ivParameterSpec);
byte[] contentBytes = content.getBytes("UTF-8");
byte[] bytes = cipher.doFinal(contentBytes);
String encode = new String((new BASE64Encoder()).encode(bytes));
return encode;
}
public static String decode(String secret, String iv, String content) throws Exception {
byte[] secretKeyBytes = secret.getBytes();
SecretKey secretKey = new SecretKeySpec(secretKeyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(2, secretKey, ivParameterSpec);
byte[] contentBytes = (new BASE64Decoder()).decodeBuffer(content);
byte[] decodeBytes = cipher.doFinal(contentBytes);
String decode = new String(decodeBytes, "UTF-8");
return decode;
}
public static String decrypt(String data, String secretKeyString, String ivString) throws Exception {
byte[] secretKey = stringTransferToByteArray(secretKeyString);
byte[] iv = stringTransferToByteArray(ivString);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
cipher.init(2, secretKeySpec, new IvParameterSpec(iv));
String result = new String(cipher.doFinal(stringTransferToByteArray(data)));
return result;
}
public static String encrypt(String data, String secretKeyString, String ivString) throws Exception {
byte[] secretKey = stringTransferToByteArray(secretKeyString);
byte[] iv = stringTransferToByteArray(ivString);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
cipher.init(1, secretKeySpec, new IvParameterSpec(iv));
String result = byteArrayTransferToString(cipher.doFinal(stringTransferToByteArray(data)));
return result;
}
public static String byteArrayTransferToString(byte[] bytes) {
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(bytes);
}
public static byte[] stringTransferToByteArray(String s) {
Decoder decoder = Base64.getDecoder();
return decoder.decode(s);
}
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
byte[] secretKey = keyGenerator.generateKey().getEncoded();
secretKey = "cc6f96c4079656ba".getBytes();
String s = byteArrayTransferToString(secretKey);
System.out.println("aes密钥:" + s);
byte[] iv = "83682737f03c4212".getBytes();
String ivString = byteArrayTransferToString(iv);
System.out.println("aes偏移量:" + ivString);
String str = "hello";
String s1 = encrypt(byteArrayTransferToString(str.getBytes()), s, ivString);
System.out.println("加密结果:" + s1);
String s2 = decrypt(s1, s, ivString);
System.out.println("解密结果:" + s2);
}
}