加密值:673a487f841c4c0b405cb3fee05b3385
加密密钥:Y4IC99u0pqLKIrpr
解密值:96895018020
我们尝试使用下面的 Java 代码并且它正在工作。
public class oomaHeDecryption {
private static final String CipherMode = "AES/ECB/NoPadding";
public static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content, String password) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
result = (result.length() > 11) ? result.substring(0, 11) : result;
return result;
}
public static SecretKeySpec createKey(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(16);
sb.append(password);
while (sb.length() < 16) {
sb.append("0");
}
if (sb.length() > 16) {
sb.setLength(16);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
@SuppressWarnings("null")
public static <ASCII> String asciiToHex(String asciiValue) {
char[] chars = ((String) asciiValue).toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
hex.append(Integer.toHexString((int) chars[i]));
}
return hex.toString();
}
}
在 PHP 中,我们有如下代码,但它给出了一些垃圾值。
$decrypted = openssl_decrypt('673a487f841c4c0b405cb3fee05b3385', 'AES-128-ECB', 'Y4IC99u0pqLKIrpr', OPENSSL_ZERO_PADDING);
var_dump($decrypted);
那么您能帮助我们解决 PHP-OPENSSL 中的错误吗?
$encrypted = hex2bin('673a487f841c4c0b405cb3fee05b3385');
$decrypted = openssl_decrypt($encrypted, 'AES-128-ECB', 'Y4IC99u0pqLKIrpr',
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$decrypted = rtrim($decrypted, "\0");