【问题标题】:How to make a file encrypt and decrypt with TripleDes in PHP如何在 PHP 中使用 TripleDes 对文件进行加密和解密
【发布时间】:2012-05-11 17:06:49
【问题描述】:

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

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

感谢您的关注。

【问题讨论】:

    标签: php encryption tripledes


    【解决方案1】:

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

    $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);
    

    【讨论】:

    • 你可以做$buffer = $file . str_repeat("\0", 8 - (strlen($file) % 8));。另外,解密后不应该删除NUL字节吗?
    • 谢谢,我现在正在尝试。但是那个 $output 变量等于什么?是要下载的加密文件吗?
    • 我的错,应该是 $encrypted_file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多