解密在php中使用AES加密的java字符串
当我运行PHP代码时,我得到以下2个细节,我将它们复制到我的java代码中解密在php中使用AES加密的java字符串,java,php,encryption,aes,Java,Php,Encryption,Aes,当我运行PHP代码时,我得到以下2个细节,我将它们复制到我的java代码中 public static String decryptWithIV(byte[] key, String encrypted) throws GeneralSecurityException { if (key.length != 16) { throw new IllegalArgumentException("Invalid key size."); } S
public static String decryptWithIV(byte[] key, String encrypted)
throws GeneralSecurityException {
if (key.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[16]));
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted
.getBytes()));
return new String(original);
}
public static final String KEY=“œ8[,#BÌümÕe-您的代码存在一些问题
- 您在Java中的密钥长度只有31个字符,而不是32个。字节不能始终表示为字符串,因为有时会有不值钱的字符。您可以使用
bin2hex()
将密钥打印为十六进制,或使用base64\u encode()
将密钥打印为base64,并使用Java中的类似或base64.decodeBase64()的方式反转过程
- 使用与PHP中相同的操作和填充模式。即使用
“AES/CBC/NoPadding”
。由于PHP使用零填充,因此需要在解密后删除结尾处的零字节
- 在PHP中,IV是在密文的前面加上的,但在Java中并没有切掉IV。此外,还提供了一个零字节的IV
您在PHP中使用CBC模式,但在Java中使用ECB模式以及不同的填充。@ArtjomB.很抱歉,我只是想让我的注释可读。@AlexK.我是安全方面的新手,请您指导我正确的方向来解决这个问题。谢谢您的回复,但我的密钥已经是字节。问题是我目前正在本地服务器上工作,首先我在本地服务器上工作运行PHP代码并获取对称密钥和加密数据。从那里我复制并粘贴了java字符串。我认为问题在于我的密钥已经在字节中,因为我正在使用openssl_random_pseudo_bytes PHP函数,该函数返回随机字节。是的,您应该首先将其编码为十六进制或base64。字符串hex=Long.toHexString(Long.parseLong(“·ñ+«r–A'andÎAo§èèè229;[èÎd6è:“,2))这给了我数字格式的例外。我认为它无法识别字节为有效格式。您需要在php端将密钥编码为十六进制/base64,将其复制到Java程序,然后将其解码回字节数组。非常感谢您的帮助。最后一件事,我得到了解密的字符串,但它在开始和结束时添加了一些数据.RESULT-“Íè7}Vÿ¼MaÎaga此字符串是AES-256/CBC/ZeroBytePadding加密的。我还更新了解密函数。
public static String decryptWithIV(byte[] key, String encrypted)
throws GeneralSecurityException {
if (key.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
new byte[16]));
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted
.getBytes()));
return new String(original);
}
public static final String KEY = "œ8[,#BÌüïmÕe-<Æ 1^Ž—½R™t3§¡ÚI";
public static final String ENCRYPTED_DATA = "ASfLnSMyfp9vHN2UTO4TRilUIRywzVfJfrfkrp4gPsP0+lENwEHJ3/YzstfuIESgVFfpkxHGTxuiO+aWZObG5aPoZfrcoIDQLVXeRiysA4s=";
public static String decryptWithIV(byte[] key, String encrypted)
throws GeneralSecurityException {
if (key.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}
byte[] ciphertextBytes = Base64.decodeBase64(encrypted.getBytes());
IvParameterSpec iv = new IvParameterSpec(ciphertextBytes, 0, 16);
ciphertextBytes = Arrays.copyOfRange(ciphertextBytes, 16,
ciphertextBytes.length);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(ciphertextBytes);
// remove zero bytes at the end
int lastLength = original.length;
for (int i = original.length - 1; i > original.length - 16; i--) {
if (original[i] == (byte) 0) {
lastLength--;
} else {
break;
}
}
return new String(original, 0, lastLength);
}