mdecrypt_generic
(PHP 4 >= 4.0.2, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)
mdecrypt_generic — 解密數據
警告
本函式已自 PHP 7.1.0 起廢棄。強烈建議不要使用本函式。
說明
mdecrypt_generic(resource
$td
, string $data
): string解密數據。 請注意,由於存在數據補齊的情況, 返回字串的長度可能和明文的長度不相等。
範例
示例 #1 mdecrypt_generic() 例程
<?php
/* 數據 */
$key = 'this is a very long key, even too long for the cipher';
$plain_text = 'very important data';
/* 打開加密模組,並且建立初始向量 */
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
/* 初始化加密控制代碼 */
if (mcrypt_generic_init($td, $key, $iv) != -1) {
/* 加密數據 */
$c_t = mcrypt_generic($td, $plain_text);
mcrypt_generic_deinit($td);
/* 為解密重新初始化緩衝區 */
mcrypt_generic_init($td, $key, $iv);
$p_t = mdecrypt_generic($td, $c_t);
/* 執行清理工作 */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if (strncmp($p_t, $plain_text, strlen($plain_text)) == 0) {
echo "ok\n";
} else {
echo "error\n";
}
?>
上例中演示瞭如何檢測 解密后的數據是否和原始明文長度一致。 需要著重提醒的是,在對數據進行機密之前, 必須使用 mcrypt_generic_init() 函式來重新初始化緩衝區。
呼叫本函式之前, 必須使用金鑰和初始向量來呼叫 mcrypt_generic_init() 函式 對解密控制代碼進行初始化。 加解密工作完成之後,需要呼叫 mcrypt_generic_deinit() 來釋放加解密緩衝區。 例程請參見 mcrypt_module_open()。
參見
- mcrypt_generic() - 加密數據
- mcrypt_generic_init() - 初始化加密所需的緩衝區
- mcrypt_generic_deinit() - 對加密模組進行清理工作