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

如何在PHP中使用TripleDes对文件进行加密和解密,php,encryption,tripledes,Php,Encryption,Tripledes,我找不到足够的关于这个主题的资源,我需要学习如何在PHP中使用TripleDes对文件进行加密和解密(上传时应该加密文件,下载时应该解密文件) 我也找到了一些例子,但我无法实现它 http://stackoverflow.com/questions/10548386/issue-with-encrypt-and-decrypt-a-word-docx-file-in-php 感谢您的关注。您可以使用此代码加密字符串: $buffer = $file; // get the amount of

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

我也找到了一些例子,但我无法实现它 http://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);

您可以只做
$buffer=$file。stru重复(“\0”,8-(strlen($file)%8))。还有,解密后你不应该删除NUL字节吗?谢谢,我正在尝试。但是$output变量等于什么?下载的是加密文件吗?我的错,那应该是$encrypted\u文件
$decrypted_file = mcrypt_cbc(MCRYPT_3DES, $key, $encrypted_file, MCRYPT_DECRYPT, $iv);