php AES、DES java
一、AES
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| byte[] tmp = asBin(encrypted);
byte[] key = asBin(sKey);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, skeySpec);
byte[] decrypted = cipher.doFinal(tmp);
return new String(decrypted);
private static byte[] asBin(String src) {
byte[] encrypted = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
encrypted[i] = (byte)(high * 16 + low);
}
return encrypted;
}
|
php
1
2
3
| $d1 = hex2bin($src);
$d2 = hex2bin($key);
$out0 = openssl_decrypt($d1, 'aes-128-ecb', $d2, OPENSSL_RAW_DATA);
|
二、DES
1
2
3
4
5
6
7
8
9
10
| public static byte[] decrypt1(byte[] msg, String key) throws Exception {
Security.addProvider(new SunJCE());
byte[] keyBytes = genKey(key);
SecretKey deskey = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec ivspec = genIV();
byte[] src = msg;
Cipher c1 = Cipher.getInstance("DESede/CBC/NoPadding");
c1.init(2, deskey, ivspec);
return c1.doFinal(src);
}
|
php
1
| $out0 = openssl_decrypt($d1, 'DES-EDE3-CBC', $d2, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
|