所以我正在编写一个Web应用程序,并且在某些用途中我需要对字符串进行加密并在以后对其进行解密,并且我的代码中的所有内容都可以在El Capitan 10.11.4和XAMPP 5.6.15-1上的Macbook上的localhost上完美运行,但是我在服务器上传代码它不会工作。我发现了一个问题(我也试过多个服务器)。
所以这是我的代码:
<?php
session_start();
header("Content-Type: text/html;charset=UTF-8");
if (isset($_POST["file"])) {
$filename = $_POST["file"];
//$filename = $_GET["file"];
$filename = substr($filename, 12);
$username = $_SESSION["username"];
$key = $_SESSION["key"];
$filename = "../users/$username/text/" . $filename;
$fileNumber = $_POST["number"];
///Cloude/users/antonio/text/teext/file2.txt
// Cloude/script
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
$decrypt = str_replace(" ", "+", $contents);
echo " ------ 1 ------ ";
$decrypt = explode('|', $decrypt.'|');
$decoded = base64_decode($decrypt[0]);
$iv = base64_decode($decrypt[1]);
echo " ------ 2 ------";
if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; }
echo " ------ 3 ------";
$key = pack('H*', $key);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
$mac = substr($decrypted, -64);
$decrypted = substr($decrypted, 0, -64);
$calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
if($calcmac!==$mac){ return false; }
$decrypted = unserialize($decrypted);
echo json_encode($decrypted . "qJB0rGtIn5UB1xG03efyCp55");
}
并且,这个回声仅用于测试哪条线路不起作用。因此,当我尝试运行它时,它只会打印&#34; ------ 1 ------和------ 2 ------&#34;,代码之后
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)
只是不在服务器上工作,有谁知道为什么?
编辑:我发现了这个错误,这是托管问题,他们做了一些奇怪的事情,谢谢!答案 0 :(得分:2)
您不应再使用MCrypt功能了。为什么?因为 MCrypt被视为放弃软件。该库不再被主动维护,并且a long list of known bugs很长时间没有修复。
那么,你的问题会有什么解决方案? 快速,简便,安全的选择是使用drop in library。
以简单的方式做强加密的例子:
// Assuming a PSR-4 compatible autoloader
use Driftwood\SymmetricEncryption;
$password = 'correct horse battery staple';
$crypto = new SymmetricEncryption(20);
$encrypted = $crypto->encrypt('Never roll your own crypto.', $password);
$decrypted = $crypto->decrypt($encrypted, $password);
echo $decrypted; // Never roll your own crypto.
如果你真的想要创建自己的加密库(你不应该),那么推荐的解决方案是使用PHP的OpenSSL extension。
但是:加密很难,非常难。一个好的加密包装器需要多个密码工作者和PHP专家一起工作,互相检查并仔细检查代码中的每个变化。仔细审查每一个决定。