[英]Java can't decrypt string encrypted in PHP
Hello everyone i have string encrypted in PHP by openssl_encrypt
with algorithm 'aes-256-cbc' 大家好,我用openssl_encrypt
用算法'aes-256-cbc'在PHP中加密了字符串
Key: C4E30455853D4949A8E91B2C366BE9DE
密钥: C4E30455853D4949A8E91B2C366BE9DE
Vector: 5686044872102713
矢量图5686044872102713
: 5686044872102713
Encrypted string: ak9YSTd6RXU5TENocUxQUGxieVhpZ3VqSlFiQUdndGZrbVJvbEliTGZjZz0=
加密的字符串: ak9YSTd6RXU5TENocUxQUGxieVhpZ3VqSlFiQUdndGZrbVJvbEliTGZjZz0=
And here is my Java function for decrypt: 这是我用于解密的Java函数:
public static String Decrypt_AES_FromBase64(String AEncryptedText, String AKey32Bytes, String AVectorNum16Bytes) {
try {
byte[] vEncryptedBytes = Base64.getDecoder().decode(AEncryptedText);
Key SecretKey = new SecretKeySpec(AKey32Bytes.getBytes(), "AES");
IvParameterSpec vSpec = new IvParameterSpec(AVectorNum16Bytes.getBytes());
Cipher vCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
vCipher.init(Cipher.DECRYPT_MODE, SecretKey, vSpec);
return new String(vCipher.doFinal(vEncryptedBytes));
} catch (Exception e) {
Common.mContext.getLogger().log(e.toString());
return "";
}
}
When i try to decrypt i have error: 当我尝试解密时出现错误:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
Can somebody tell what the wrong? 有人能说出什么错吗?
The encrypted string AKey32Bytes
is double Base64 encoded. 加密的字符串AKey32Bytes
是双重Base64编码的。
Instead of AKey32Bytes.getBytes()
you need to double Base64 decode the encrypted data to binary. 而不是AKey32Bytes.getBytes()
您需要将Base64解码后的数据加倍为二进制。
Encrypted string: 加密的字符串:
ak9YSTd6RXU5TENocUxQUGxieVhpZ3VqSlFiQUdndGZrbVJvbEliTGZjZz0=
After one Base64 decode: 经过一次Base64解码后:
jOXI7zEu9LChqLPPlbyXigujJQbAGgtfkmRolIbLfcg=
After a second Base64 decode (displayed in hex because it is not binary): 在第二次Base64解码之后(以十六进制显示,因为它不是二进制文件):
8CE5C8EF312EF4B0A1A8B3CF95BC978A0BA32506C01A0B5F9264689486CB7DC8
That is what needs to be provided to the decryption function. 那就是需要提供给解密功能的东西。
The decrypted result is: 解密结果为:
(in hex) 257531362A2179704B40577255516272
(十六进制) 257531362A2179704B40577255516272
(in ASCII): "%u16*!ypK@WrUQbr" (all valid ASCII characters) (以ASCII表示):“%u16 *!ypK @ WrUQbr”(所有有效的ASCII字符)
Note: there is a full block of PKCS#7 padding (in hex): 10101010101010101010101010101010
注意:有完整的PKCS#7填充块(以十六进制表示): 10101010101010101010101010101010
As much as it pains me to say this, from the correct padding I can assume the decryption was successful. 这么说让我很痛苦,从正确的填充中我可以认为解密是成功的。
See Cryptomathic AES CALCULATOR 请参阅隐数学AES计算器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.