【发布时间】:2016-04-22 03:50:10
【问题描述】:
$secretKey = "MYSECRETKEY";
$plaintext = 'Plain Text Will Be here';
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ivDecode = base64_encode(mcrypt_create_iv($iv_size, MCRYPT_RAND));
$encrypted = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
substr(sha1($secretKey), 0, 32),
$plaintext,
MCRYPT_MODE_CBC,
$iv), "\0..\32");
$encrypted = $iv . $encrypted;
$ciphertext_base64 = base64_encode($encrypted);
#echo $ciphertext_base64 . "\n";
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
substr(sha1($secretKey), 0, 32),
base64_decode($ciphertext_base64),
MCRYPT_MODE_CBC,
base64_decode($ivDecode)), "\0..\32");
echo $decrypted;
当我运行上面的代码时,我得到了这个输出。
»_w>ø9â„6ÅkžPlain Text Will Be here
我无法编辑$decrypted 字符串,因为我无法访问它。我只能编辑 $encrypted 。那么如何通过编辑$encrypted 字符串从输出中删除额外的特殊字符(»_w>ø9â„6Åkž)。我想使用 JSON 将加密文本发送到不同的服务器进行解密。
【问题讨论】:
-
我尝试清除 cookie 但它不起作用。 :)
-
从
mcrypt_切换到openssl_将使您的生活更轻松。 -
@ScottArciszewski 你用openssl_测试了上面的例子吗?它不能解决问题。问题仍未解决。您可以添加工作示例吗?
标签: php encryption mcrypt