【发布时间】:2017-12-20 00:22:10
【问题描述】:
以前,我有一个工作系统来加密 PHP 中的数据并使用 JAVA 解密它。这是PHP代码:
function encrypt($message, $initialVector, $secretKey) {
return base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
md5($secretKey),
$message,
MCRYPT_MODE_CFB,
$initialVector
)
);
}
function decrypt($message, $initialVector, $secretKey) {
$decoded = base64_decode($message);
return mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
md5($secretKey),
$decoded,
MCRYPT_MODE_CFB,
$initialVector
);
}
还有java代码
public String decrypt(String encryptedData, String initialVectorString, String secretKey) {
String decryptedData = null;
try {
SecretKeySpec skeySpec = new SecretKeySpec(md5(secretKey).getBytes(), "AES");
IvParameterSpec initialVector = new IvParameterSpec(initialVectorString.getBytes());
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, initialVector);
byte[] encryptedByteArray = (new org.apache.commons.codec.binary.Base64()).decode(encryptedData.getBytes());
byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray);
decryptedData = new String(decryptedByteArray, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return decryptedData;
}
但是,我最近从 PHP 5.x 切换到 7.1,现在收到以下消息:
“函数 mcrypt_encrypt() 已弃用”
所以看起来 mcrypt 不再是一个好的选择了。我用谷歌搜索了很多,但大多数例子仍然使用 mcrypt。唯一的其他好的选择是指像 RNCryptor 或 defuse 这样的工具,但没有任何工作示例。是否有一些适用于 PHP 和 JAVA 的简单工作示例?我需要能够将数据解密为其原始形式,因为我需要使用它执行某些任务。
提前致谢
【问题讨论】:
-
所以你有两个解密函数?干什么用的?
-
@Oliver 正如 OP 解释的那样,一个是 PHP,一个是 Java
-
MD5不是安全密钥派生函数,NIST 建议是PBKDF2,有时也称为Rfc2898DeriveBytes。推导函数应该使用大约 100 毫秒的 CPU 时间以确保安全。
标签: java php encryption cryptography