使用php rijndael-128解密数据
我尝试用PHP解密数据,我尝试了这段代码,但它不会打印“几乎”,也不会打印解密的数据或“完成”。它仅打印此行“4b3b798e708d81a939ca084d387136145” 这是我的密码:使用php rijndael-128解密数据,php,Php,我尝试用PHP解密数据,我尝试了这段代码,但它不会打印“几乎”,也不会打印解密的数据或“完成”。它仅打印此行“4b3b798e708d81a939ca084d387136145” 这是我的密码: <?php class MCrypt { private $iv = 'fedcba9876543210'; #Same as in JAVA private $key = '0123456789abcdef'; #Same as in JAVA function
<?php
class MCrypt
{
private $iv = 'fedcba9876543210'; #Same as in JAVA
private $key = '0123456789abcdef'; #Same as in JAVA
function __construct()
{
}
/**
* @param string $str
* @param bool $isBinary whether to encrypt as binary or not. Default is: false
* @return string Encrypted data
*/
function encrypt($str, $isBinary = false)
{
echo "3";
$iv = $this->iv;
$str = $isBinary ? $str : utf8_decode($str);
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? $encrypted : bin2hex($encrypted);
}
/**
* @param string $code
* @param bool $isBinary whether to decrypt as binary or not. Default is: false
* @return string Decrypted data
*/
function decrypt($code, $isBinary = false)
{
echo "4";
$code = $isBinary ? $code : $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
}
protected function hex2bin($hexdata)
{
echo "5";
$bindata = '';
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
}
$encrypted="4b3b798e708d81a939ca084d387136";
echo $encrypted;
$mcrypt = new MCrypt();
echo "1";
$decrypted = $mcrypt->decrypt($encrypted);
echo "almost";
echo $decrypted;
echo "finish";
?>
您的IV和钥匙可能都是二进制形式。至于你的Java应用程序是如何获得它的密钥的。。。¯\_(ツ)_/““您的IV和密钥可能都应该是二进制形式。至于Java应用程序如何派生其密钥…”\_(ツ)_/¯