如何在PHP中使用TripleDes加密和解密文件


How to make a file encrypt and decrypt with TripleDes in PHP

我找不到关于这个主题的足够资源,我需要学习如何在 PHP 中使用 TripleDes 加密和解密文件(上传时应该加密文件,下载文件时它应该解密)。

我也找到了一些例子,但我无法实现它http://php.net/manual/en/mcrypt.examples.phphttp://stackoverflow.com/questions/10548386/issue-with-encrypt-and-decrypt-a-word-docx-file-in-php

感谢您的关注。

您可以使用此代码加密字符串:

$buffer = $file; 
// get the amount of bytes to pad
$extra = 8 - (strlen($buffer) % 8);
// add the zero padding
if($extra > 0) {
    for($i = 0; $i < $extra; $i++) {
        $buffer .= "'0";
    }
}
// very simple ASCII key and IV
$key = "passwordDR0wSS@P6660juht";
$iv = "password";
// hex encode the return value
$encrypted_file = mcrypt_cbc(MCRYPT_3DES, $key, $buffer, MCRYPT_ENCRYPT, $iv);

而这个来解密它:

$decrypted_file = mcrypt_cbc(MCRYPT_3DES, $key, $encrypted_file, MCRYPT_DECRYPT, $iv);