后端利用3DES加解密工具类实现特殊字段加解密,比如个人信息、请求参数等 ,可以自定义密钥和IV加密,代码如下,直接使用即可。
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;/*** 3DES加解密工具类(CBC模式,PKCS5Padding填充)*/
public class CryptoUtils {// 默认密钥(24字节)private static final String DEFAULT_KEY = "www.xxxx.comwww.xxxx.com";// 默认IV(8字节)private static final byte[] DEFAULT_IV = {12, 34, 56, 78, 90, 87, 65, 43};/*** 使用默认密钥和IV加密*/public static String encrypt(String plainText) throws Exception {return encrypt(plainText, DEFAULT_KEY, DEFAULT_IV);}/*** 使用默认密钥和IV解密*/public static String decrypt(String cipherText) throws Exception {return decrypt(cipherText, DEFAULT_KEY, DEFAULT_IV);}/*** 自定义密钥和IV加密*/public static String encrypt(String plainText, String keyString, byte[] iv) throws Exception {// 参数校验validateKeyAndIV(keyString, iv);byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);DESedeKeySpec spec = new DESedeKeySpec(keyData);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey key = keyFactory.generateSecret(spec);Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");IvParameterSpec ivSpec = new IvParameterSpec(iv);cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encrypted);}/*** 自定义密钥和IV解密*/public static String decrypt(String cipherText, String keyString, byte[] iv) throws Exception {// 参数校验validateKeyAndIV(keyString, iv);byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);DESedeKeySpec spec = new DESedeKeySpec(keyData);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey key = keyFactory.generateSecret(spec);Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");IvParameterSpec ivSpec = new IvParameterSpec(iv);cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);byte[] decoded = Base64.getDecoder().decode(cipherText);byte[] decrypted = cipher.doFinal(decoded);return new String(decrypted, StandardCharsets.UTF_8);}/*** 密钥和IV校验*/private static void validateKeyAndIV(String key, byte[] iv) throws IllegalArgumentException {if (key == null || key.getBytes(StandardCharsets.UTF_8).length != 24) {throw new IllegalArgumentException("Key must be 24 bytes (192 bits)");}if (iv == null || iv.length != 8) {throw new IllegalArgumentException("IV must be 8 bytes");}}}
可以测试一下,测试代码如下所示:
public static void main(String[] args) {try {// 测试1:使用默认参数String original = "Hello, World! 你好,世界!";String encrypted = CryptoUtils.encrypt(original);String decrypted = CryptoUtils.decrypt(encrypted);System.out.println("测试1 - 默认参数:");System.out.println("原文: " + original);System.out.println("加密后: " + encrypted);System.out.println("解密后: " + decrypted);System.out.println("结果: " + (original.equals(decrypted) ? "成功" : "失败"));// 测试2:使用自定义参数String customKey = "ThisIs24BytesLongKey12345";byte[] customIV = {1,2,3,4,5,6,7,8};String encrypted2 = CryptoUtils.encrypt(original, customKey, customIV);String decrypted2 = CryptoUtils.decrypt(encrypted2, customKey, customIV);System.out.println("\n测试2 - 自定义参数:");System.out.println("解密结果: " + (original.equals(decrypted2) ? "成功" : "失败"));// 测试3:异常参数测试try {CryptoUtils.encrypt(original, "shortKey", customIV);} catch (IllegalArgumentException e) {System.out.println("\n测试3 - 异常参数捕获成功: " + e.getMessage());}} catch (Exception e) {e.printStackTrace();}}