我正在向 API 发送数据,他们使用 Java 函数,我在 PHP 中复制了加密,但我无法解密响应数据。我尝试了各种加密,但他们严格使用此加密解密,我无法更改加密我必须遵循这一点。加密功能正常工作:function encrypt($input, $key) { $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $input = pkcs5_pad($input, $size); $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $data = base64_encode($data); return $data; } function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); }我试过的解密函数:function decrypt($sStr, $sKey) { $decrypted = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $sKey, base64_decode($sStr), MCRYPT_MODE_ECB ); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s - 1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; }作为我得到的输出???这是错误的加密。不要重复这个因为我已经搜索了 3 个多小时,然后我发布了问题。
1 回答
红糖糍粑
TA贡献1815条经验 获得超6个赞
function decrypt($sStr, $sKey) {
$decrypted = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128, $sKey, base64_decode($sStr), MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, -$padding);
$decrypted = base64_encode($decrypted ); //This line Added.
return $decrypted;
}
我得到了字节格式,我没有将数据转换为 base64_encode,现在它正在工作。
- 1 回答
- 0 关注
- 199 浏览
添加回答
举报
0/150
提交
取消