项目需要 需要java可以解密 php加密的 字符串 ,
使用的方法是 AES128位加解密
坑一踩完 ,还是直接上代码
package com.xxx.init.utils;
import com.xxx.init.utils.BaseDataUtil;
import com.xxx.init.exception.xxxRuntimeException;
import com.xxx.init.out.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* User:Json
* Date: 2024/4/23
*
**/
@Slf4j
public class AesHelper {
/*
* aes 128
*/
public static String AES_128_ECB = "AES/ECB/PKCS5Padding";
/*
* aes 256
*/
public static final String AES_256_ECB = "AES/256/ECB/PKCS5Padding"; // 使用 AES-256-ECB 模式
//获取nacos配置文件下的密钥
public static String checkSecretKey() {
return checkSecretKey(BaseDataUtil.getSystemConfigNacos().getAes128Key());
}
//获取密钥
public static String checkSecretKey(String secretKey) {
if (StringUtils.isEmpty(secretKey)) {
secretKey = BaseDataUtil.getSystemConfigNacos().getAes128Key();
}
if (StringUtils.isEmpty(secretKey)) {
throw new xxxRuntimeException(ResultCode.AES_KEY_ERROR);
}
return secretKey;
}
public static String encrypt128(String data) {
String secretKey = padToLengthBytes(checkSecretKey(), 16);
return encrypt(data, secretKey, AES_128_ECB, null);
}
//加密方法,对数据进行加密,返回加密后的数据.
public static String encrypt(String data, String secretKey, String method, String iv) {
try {
secretKey = checkSecretKey(secretKey);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(method); // 使用指定的加密模式
if (StringUtils.isEmpty(iv)) {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
} else {
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
}
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
//php那边 openssl_encrypt 返回值直接是 base64 然后拿到值后又base64_encode 一次
// 所以 java 加密 也要base64 两遍 / 解密也一样 需要两遍
return Base64.getEncoder().encodeToString(Base64.getEncoder().encodeToString(encryptedBytes).getBytes());
} catch (Exception e) {
log.error("【AES加密失败】:" + e.getMessage());
throw new xxxRuntimeException(ResultCode.AES_ENCRYPT_ERROR);
}
}
public static String decrypt128(String data) {
String secretKey = padToLengthBytes(checkSecretKey(), 16);
return decrypt(data, secretKey, AES_128_ECB, null);
}
//解密方法,对数据进行解密,返回解密后的数据.
public static String decrypt(String data, String secretKey, String method, String iv) {
try {
secretKey = checkSecretKey(secretKey);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(method); // 使用指定的解密模式
if (StringUtils.isEmpty(iv)) {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
} else {
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
}
//base64 解密两次
byte[] decodedBytes = Base64.getDecoder().decode(new String(Base64.getDecoder().decode(data)));
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
} catch (Exception e) {
log.error("【AES解密失败】:" + e.getMessage());
throw new xxxRuntimeException(ResultCode.AES_DECRYPT_ERROR);
}
}
//如果密钥 不足16位 或者超过16位的 处理方案 保证和php 那边一样 php的openssl_encrypt会自动补全 java需要手动补全
//input 要补全的字符串 , length 跟多少位补全 常用的 16位 32位
public static String padToLengthBytes(String input, int length) {
// 转换input为字节数组
byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
// 如果输入的字节数大于指定长度,只取前指定长度字节;否则,补全到指定长度字节
byte[] keyBytes = new byte[length];
if (inputBytes.length > length) {
System.arraycopy(inputBytes, 0, keyBytes, 0, length);
} else {
int paddingLength = length - inputBytes.length % length;
byte[] paddedKeyBytes = new byte[inputBytes.length + paddingLength];
System.arraycopy(inputBytes, 0, paddedKeyBytes, 0, inputBytes.length);
keyBytes = paddedKeyBytes;
}
// 将keyBytes转换为字符串
return new String(keyBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
String encrypted = AesHelper.encrypt("哈哈,我是谁!",padToLengthBytes("11111",16),AES_128_ECB,null);
String decrypted = AesHelper.decrypt("ZkN4UUoyL0RqOEtYYWJNblk2VjI3YjNjN3l4d0ZGSEYwc1J5WXNzZDlxVT0=",padToLengthBytes("111111",16),AES_128_ECB,null);
System.out.println("加密值:" + encrypted);
System.out.println("解密值:" + decrypted);
}
}
如果疑问 php openssl_encrypt() 为什么加密后会直接base64 可以了解一下 openssl_encrypt 下面3个常量
所以java这边需要不需要 base64 两边取决于 php 用没用 下面三个常量
OPENSSL_RAW_DATA=1
OPENSSL_ZERO_PADDING=2
OPENSSL_NO_PADDING=3
结束