Java前后端AES加密解密
作者:互联网
之前要写接口aes加密传输,涉及到经常会遇到
后端代码
import cn.hutool.core.codec.Base64; import cn.hutool.core.util.HexUtil; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.stereotype.Component; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.Security; import java.util.Arrays; /** * @Date: 1900/7/7 11:01 * @Description: AES加密工具类 * @Version: 1.0 */ @Component public class AesUtil { /** * @author ngh * AES128 算法 * <p> * CBC 模式 * <p> * PKCS7Padding 填充模式 * <p> * CBC模式需要添加一个参数iv * <p> * 介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别 */ private Key key; private Cipher cipher; String aesKey = "1111bat1977"; byte[] iv = "7773066605060999".getBytes(); byte[] keyBytes = aesKey.getBytes(); public void init(byte[] keyBytes) { // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要 int base = 16; if (keyBytes.length % base != 0) { int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0); byte[] temp = new byte[groups * base]; Arrays.fill(temp, (byte) 0); System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length); keyBytes = temp; } // 初始化 Security.addProvider(new BouncyCastleProvider()); // 转化成JAVA的密钥格式 key = new SecretKeySpec(keyBytes, CipherType.AES_ALGORITHM); try { // 初始化cipher cipher = Cipher.getInstance(CipherType.AES_CBC_PKC7PADDING, "BC"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String encrypt(String content) { return Base64.encode(encrypt(content.getBytes(), keyBytes)); } public String encrypt(String content, String keyBytes) { return Base64.encode(encrypt(content.getBytes(), keyBytes.getBytes())); } public String encryptByHex(String content, String keyBytes) { return HexUtil.encodeHexStr(encrypt(content.getBytes(), keyBytes.getBytes())); } public String encryptByHex(String content) { return HexUtil.encodeHexStr(encrypt(content.getBytes(), keyBytes)); } /** * 加密方法 * * @param content 要加密的字符串 * @param keyBytes 加密密钥 * @return */ public byte[] encrypt(byte[] content, byte[] keyBytes) { byte[] encryptedText = null; keyBytes = new String(keyBytes).getBytes(); init(keyBytes); try { cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); encryptedText = cipher.doFinal(content); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return encryptedText; } public String decrypt(String encryptedData) { return new String(decrypt(Base64.decode(encryptedData), keyBytes)); } public String decrypt(String encryptedData, String keyData) { return new String(decrypt(Base64.decode(encryptedData), keyData.getBytes())); } public String decryptByHex(String encryptedData, String keyData) { return new String(decrypt(HexUtil.decodeHex(encryptedData), keyData.getBytes())); } public String decryptByHex(String encryptedData) { return new String(decrypt(HexUtil.decodeHex(encryptedData), keyBytes)); } /** * 解密方法 * * @param encryptedData 要解密的字符串 * @param keyBytes 解密密钥 * @return */ public byte[] decrypt(byte[] encryptedData, byte[] keyBytes) { byte[] encryptedText = null; init(keyBytes); try { cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); encryptedText = cipher.doFinal(encryptedData); } catch (Exception e) { e.printStackTrace(); } return encryptedText; } public static void main(String[] args) { AesUtil aes = new AesUtil(); //加密字符串 String content = "{\"userName\":\"12\",\"userPhone\":\"123\"}"; System.out.println("加密前的:" + content); // 加密方法 String encStr = aes.encryptByHex(content); System.out.println("加密后的内容:" + encStr); // 解密方法 String decStr = aes.decryptByHex(encStr); System.out.println("解密后的内容:" + decStr); } }
跑一下main方法,执行结果
加密前的:{"userName":"12","userPhone":"123"} 加密后的内容:989f16833c907f8168a6dc81424f136726a2fdf587f83360d8e24ef4506ffbfd69f0a7453a97dcc609acb3d312b4bf77 解密后的内容:{"userName":"12","userPhone":"123"}
前端代码
import CryptoJS from 'crypto-js'; const KEY = '1111bat1977';
const IV = '7773066605060999';
// 添加偏移量 function fillKey(key, keySize) { keySize = keySize || 128; let filledKey = Buffer.alloc(keySize / 8); let keys = Buffer.from(key); if (keys.length < filledKey.length) { for (let i = 0; i < filledKey.length; i++) { filledKey[i] = keys[i]; } } return filledKey; } // 加密 export const encrypt = message => { const keyAccept = CryptoJS.lib.WordArray.create(fillKey(KEY, 128)); const giv = CryptoJS.enc.Utf8.parse(IV); let messageJson = ''; if (typeof message === 'object') { messageJson = JSON.stringify(message); } else { messageJson = message; } return base64ToHex(CryptoJS.AES.encrypt(messageJson, keyAccept, { iv: giv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, }).toString()); }; // 解密 export const decrypt = encryptedBase64Str => { const resBase64 = hexToBase64(encryptedBase64Str) const keyResponse = CryptoJS.lib.WordArray.create(fillKey(KEY, 128)); const giv = CryptoJS.enc.Utf8.parse(IV); const decrypt = CryptoJS.AES.decrypt(resBase64, keyResponse, { iv: giv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, }); const message = decrypt.toString(CryptoJS.enc.Utf8); return JSON.parse(message); }; export const hexToBase64 = data => { const d = CryptoJS.enc.Hex.parse(data); return CryptoJS.enc.Base64.stringify(d); }; export const base64ToHex = data => { const d = CryptoJS.enc.Base64.parse(data); return CryptoJS.enc.Hex.stringify(d); };
1111bat1977
标签:AES,return,String,keyBytes,解密,content,Java,CryptoJS,const 来源: https://www.cnblogs.com/shamo89/p/16470226.html