java加密⽤php解密,JAVAPHP加密解密
试图扭转这种局⾯,我得到了⼀个错误
java.security.InvalidKeyException: Illegal key size
在线
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
为了使它⼯作,我将SecretKeySpec字节数组从Bytes(“UTF-8”)更改为key.substring(0,16).getBytes(“UTF-8”)并使⽤它如
下:
String md5Key= "e510a13edeea112b57683d724d5d70a6";
String detailsData = "{\n" +
" \"acc_no\": \"1234691007924321\",\n" +
国外java php
" \"exp\": \"07/19\",\n" +
" \"name\": \"MICHAEL XXXXXX\",\n" +
" \"type\": \"VIS\"\n" +
" }";
System.out.println(encrypt(md5Key, detailsData));
我有这样的输出:
iufp4Rl+x/yTO7hSQBH7uU63sXAyzxgLequ3+JkFYZFz3PWwhxDC87TEC+bZ4rirgZVasrkLE1ehWWRGFV42Z29vAok+TMdwO
为了在PHP中解密,我使⽤以下代码,使⽤密钥的前16个字符将其⽤作键和iv初始值设定项,如下所⽰:
$enc_data =
'iufp4Rl+x/yTO7hSQBH7uU63sXAyzxgLequ3+JkFYZFz3PWwhxDC87TEC+bZ4rirgZVasrkLE1ehWWRGFV42Z29vAok+TMdwOvO
$key = 'e510a13edeea112b57683d724d5d70a6';
$key16 = substr($key, 0, 16);
$key16Hex = unpack('H*', $key16);
print openssl_decrypt($enc_data, "AES-128-CBC", $key16, 0, hex2bin($key16Hex[1]));
当然,我得到了⽤Java加密的想要的JSON数据:
{
"acc_no": "1234691007924321",
"exp": "07/19",
"name": "MICHAEL XXXXXX",
"type": "VIS"
}
奇怪的是你没有在这⼀⾏得到错误:
SecretKeySpec skeySpec = new Bytes("UTF-8"), "AES");
因为我使⽤JDK 1.8:
pto.Cipher;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import dec.binary.Base64;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。