如果您想在已棄用 mcrypt 的較新版 PHP 上使用它,請嘗試使用它的 shim。
https://github.com/phpseclib/mcrypt_compat
如果您想在已棄用 mcrypt 的較新版 PHP 上使用它,請嘗試使用它的 shim。
https://github.com/phpseclib/mcrypt_compat
安裝 php7.2-mcrypt 時問題已解決
我也遇到過同樣的問題。查看此連結 https://stackoverflow.com/q/48275494/7713811 以獲得在 PHP 中安裝它的正確解決方案。
如果沒有安裝 mcrypt,使用 phpMyAdmin 的人會被重新導向到本手冊。如果您想在 Debian 上安裝 mcrypt,請先檢查您的 PHP 版本。
yourserver# php --version
然後安裝適當版本的 mcrypt(如果您的 PHP 版本是 5.x,則安裝 php5-mcrypt)。
yourserver# apt-get install php4-mcrypt
...或...
yourserver# apt-get install php5-mcrypt
這先前已被另一位使用者發布,但被負評。我只想確認,我們在 CentOS (PHP 5.6.32) 上遇到了與 mcrypt 相關的嚴重效能問題,而這些問題在其他 Linux 版本中並不存在。
與 Ubuntu 相比,在 CentOS 7 上執行 mcrypt 時,25,000 次加密/解密的抽樣時間要長 4-5 倍。在 CentOS 上將 mcrypt 切換為 OpenSSL 將會大幅提升效能。
對於流量較低的網站來說,這可以忽略不計,但是當您開始看到大量的流量/負載時,它會很快導致伺服器癱瘓。
請注意,在許多 CentOS 版本上,PHP mcrypt 存在嚴重的效能問題。請參閱此 CentOS 錯誤報告。
http://bugs.centos.org/view.php?id=8954
如果您想要快速查看您的伺服器支援哪些加密、模式、金鑰、區塊和 iv 大小,請嘗試以下操作。
注意:我在 Mac OS X 上使用了這個簡單的 bash 命令:`locate libmcrypt`,從終端機找出演算法和模式目錄的安裝路徑。許多函數呼叫會針對某些加密產生警告,因此使用了錯誤抑制。
<?php
$modes = mcrypt_list_modes();
$algorithms = mcrypt_list_algorithms();
foreach($algorithms as $cipher)
{
echo "<h1 style=\"border-top:1px solid black;\">".$cipher."</h1>\n";
foreach($modes as $mode)
{
echo "<h3>".$mode."</h3>\n";
@$td = mcrypt_module_open(
$cipher,
'/usr/local/libmcrypt-2.5.8/modules/algorithms/',
$mode,
'/usr/local/libmcrypt-2.5.8/modules/modes/');
@$key_size = mcrypt_enc_get_key_size($td);
@$block_size = mcrypt_get_block_size($cipher,$mode);
@$iv_size = mcrypt_get_iv_size($cipher, $mode);
@mcrypt_module_close($td);
echo "
<pre>
key_size: ". ($key_size?$key_size:'n/a')
." block_size: ". ($block_size?$block_size:'n/a')
." iv_size: ". ($iv_size?$iv_size:'n/a')
." </pre>\n";
$td=NULL;
$key_size=NULL;
$block_size=NULL;
$iv_size=NULL;
}
}
?>
這是我建立的兩個使用 mcrypt 進行 256 位元加密/解密的簡單函式。我決定使用 MCRYPT_RIJNDAEL_128,因為它符合 AES 標準,並且使用 MCRYPT_MODE_CBC。(ECB 模式不適用於許多用途,因為它不使用 IV。)
此函式會儲存資料的雜湊值,以驗證資料是否已成功解密,但如果需要,可以輕鬆移除此功能。
<?php
function encrypt($decrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt . $password, true);
// Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
// Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it's just to verify successful decryption.
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
// We're done!
return $iv_base64 . $encrypted;
}
function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt . $password, true);
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
$iv = base64_decode(substr($encrypted, 0, 22) . '==');
// Remove $iv from $encrypted.
$encrypted = substr($encrypted, 22);
// Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
// Retrieve $hash which is the last 32 characters of $decrypted.
$hash = substr($decrypted, -32);
// Remove the last 32 characters from $decrypted.
$decrypted = substr($decrypted, 0, -32);
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
if (md5($decrypted) != $hash) return false;
// Yay!
return $decrypted;
}
?>