使用 php/mcrypt 加密文件会损坏 docx(和其他 Microsoft Office 2007)文件,但其他文件都很好

发布于 2024-11-08 00:05:01 字数 1532 浏览 0 评论 0原文

我正在使用 CodeIgniter 应用程序来加密上传的文件,然后在下载时解密它们,以便可以安全地存储它们(以及其他安全措施)。除了 docx(和其他 Microsoft Office 2007+)文件之外,这一切都工作正常。下载后,它们已成功解密,但 Office 检测到它们已损坏。这些可以修复,因此它们并未完全损坏,但文件中的某些内容与原始版本相比发生了变化,这使得 Office 将它们视为损坏。正如我所提到的,这似乎不会发生在任何其他文件类型上(我已经注意到)。 docx 文件的大小也略有不同(仅字节问题),而其他类型则不然。

为了加密,我上传文件,使用 file_get_contents() 将内容读入字符串,然后通过加密库中的 CodeIgniter 的encode() 运行该字符串,然后将文件保存回磁盘,删除原始文件:

function mcrypt_encode($data, $key)
{
    $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
    $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
    return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
}

并解码:

function mcrypt_decode($data, $key)
{
    $data = $this->_remove_cipher_noise($data, $key);
    $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());

    if ($init_size > strlen($data))
    {
        return FALSE;
    }

    $init_vect = substr($data, 0, $init_size);
    $data = substr($data, $init_size);
    return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
}

然后运行通过 CodeIgniter 的 force_download()

默认密码为 MCRYPT_RIJNDAEL_256,模式为 MCRYPT_MODE_CBC。我的服务器上启用了 Mcrypt 以及 MCRYPT_RIJNDAEL_256。

如果有人能指出我为什么会发生这种情况的正确方向,我将不胜感激。

非常感谢

I'm working with a CodeIgniter application to encrypt uploaded files then decrypt them when downloaded so they can be stored safely (alongside other security measures). This is all working fine, apart from docx (and other Microsoft Office 2007+) files. When these are downloaded they are successfully decrypted but Office detects them as corrupt. These can be repaired, so they're not totally corrupted, but something has changed in the file from the original version which is making Office see them as corrupt. As I mentioned, this doesn't seem to be happening to any other file types (that I have noticed). The docx files are also slightly different in size (only a matter of bytes), whereas other types are not.

To encrypt, I upload the file, read the contents into a string with file_get_contents(), then run the string through CodeIgniter's encode() in the encrypt library then save the file back to disk, deleting the original:

function mcrypt_encode($data, $key)
{
    $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
    $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
    return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
}

and to decode:

function mcrypt_decode($data, $key)
{
    $data = $this->_remove_cipher_noise($data, $key);
    $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());

    if ($init_size > strlen($data))
    {
        return FALSE;
    }

    $init_vect = substr($data, 0, $init_size);
    $data = substr($data, $init_size);
    return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
}

Then run the decoded string through CodeIgniter's force_download()

The default cipher is MCRYPT_RIJNDAEL_256 and mode is MCRYPT_MODE_CBC. Mcrypt is enabled on my server along with MCRYPT_RIJNDAEL_256.

If anyone could point me in the right direction as to why this might be happening it would be greatly appreciated.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-11-15 00:05:01

这是 file_get_contents 和二进制数据的一个已知错误。 http://bugs.php.net/bug.php?id=42661

This is a known bug with file_get_contents and binary data. http://bugs.php.net/bug.php?id=42661

霓裳挽歌倾城醉 2024-11-15 00:05:01

base64_encode() file_get_contents(),然后加密。

base64_decode() 解密后的下载之前加密文件的 file_get_contents()

base64_encode() the file_get_contents() of file, then encrypt.

base64_decode() the decrypted file_get_contents() of encrypted file before downloading.

小巷里的女流氓 2024-11-15 00:05:01

我正在做几乎同样的事情,并且是从谷歌来到这里的。

我现在解决了。问题不在file_get_contents()中,问题在于codeigniter中的encode函数。该函数删除某些特殊字符,从而从文件中删除数据。有趣的是 Microsoft Office 如何能够修复它。

I was doing almost the same thing and got here from google.

I solved it now. The problem is not in the file_get_contents(), the problem is the encode function in codeigniter. The function removes certain special characters thus removing data from the file. It's interesting how Microsoft office was able to repair it back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文