使用 PHP 创建加密的 zip 存档

2021-12-20 00:00:00 zip encryption php

我正在寻找一种将 .txt 文件加密为 zip 文件的方法,但采用安全的密码保护方式.我的目标是将此文件通过电子邮件发送给我,而任何人都无法阅读附件的内容.

I am searching for a way to encrypt a .txt file into a zip, but in a secure password protected way. My goal is to email this file to me, without anyone being able to read the content of the attachment.

有谁知道一种简单且最重要的是安全的方法来实现这一目标?我可以创建 zip 档案,但我不知道如何加密它们,也不知道这有多安全.

Does anybody know an easy, and above all, secure way to accomplish this ? I can create zip archives, but I do not know how to encrypt them, or, how secure this is.

推荐答案

注意:这个答案推荐了一种已知的加密方法不安全,即使密码很好.请查看评论中的链接和 AES 上的 Winzip QA.支持 in-php AES zip 加密附带 php 7.2(和 libzip 1.2.0),这意味着答案也很快就会过时.在那之前查看此答案了解如何调用 7z 而不是 zip 命令,它支持 winzip 的AES 加密.

Note: this answer recommends a cryptographic method that is known insecure, even with good password. Please see link from comments and the Winzip QA on AES. Support for in-php AES zip encryption arrives with php 7.2 (and libzip 1.2.0), which means this answer will soon be outdated too. Until then see this answer for how to call out to 7z instead of the zip command, which supports winzip's AES encryption.

你可以使用这个:

<?php echo system('zip -P pass file.zip file.txt'); ?>

其中 pass 是密码,file.txt 将被压缩到 file.zip 中.这应该适用于 Windows 和 Linux,您只需要获得适用于 Windows 的免费 zip 版本( http://www.info-zip.org/Zip.html#Win32 )

Where pass is the password, and file.txt will be zipped into file.zip. This should work on Windows and Linux, you just need to get a free version of zip for Windows ( http://www.info-zip.org/Zip.html#Win32 )

这种安全性可以被蛮力攻击、字典攻击等破坏.但这并不容易,特别是如果你选择了一个又长又难猜的密码.

This kind of security can be broken by brute force attacks, dictionary attacks and etc. But it's not that easy, specially if you chose a long and hard to guess password.

相关文章