使用 Mcrypt 加密/解密文件
尝试编写几个函数来加密或解密文件,并使用此处找到的类来尝试完成此操作:
http://www.itnewb.com/v/PHP-Encryption-Decryption-Using-the-MCrypt-Library-libmcrypt
加密函数下面似乎有效,因为它似乎加密了文件并将其放置在预期的目录中。我现在正在尝试解密该文件,它只是死了,并显示消息“无法完成解密”(其中编码...) php 错误日志中没有任何内容,所以我不确定为什么失败,但由于 mcrypt 对我来说是全新的,我非常倾向于相信我在这里做错了什么......
以下是函数:
//ENCRYPT FILE
function encryptFile() {
global $cryptastic;
$pass = PGPPASS;
$salt = PGPSALT;
$key = $cryptastic->pbkdf2($pass, $salt, 1000, 32) or die("Failed to generate secret key.");
if ($handle = opendir(PATH.'/ftpd')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$newfile = PATH.'/encrypted/'.$file.'.txt';
$msg = file_get_contents(PATH.'/ftpd/'.$file);
$encrypted = $cryptastic->encrypt($msg, $key) or die("Failed to complete encryption.");
$nfile = fopen($newfile, 'w');
fwrite($nfile, $encrypted);
fclose($nfile);
unlink(PATH.'/ftpd/'.$file);
}
}
closedir($handle);
}
//DECRYPT FILE
function inFTP() {
global $cryptastic;
$pass = PGPPASS;
$salt = PGPSALT;
$key = $cryptastic->pbkdf2($pass, $salt, 1000, 32) or die("Failed to generate secret key.");
if ($handle = opendir(PATH.'/encrypted')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$newfile = PATH.'/decrypted/'.$file;
$msg = PATH.'/encrypted/'.$file;
$decrypted = $cryptastic->decrypt($msg, $key) or die("Failed to complete decryption.");
$nfile = fopen($newfile, 'w');
fwrite($nfile, $decrypted);
fclose($nfile);
//unlink(PATH.'/encrypted/'.$file);
}
}
closedir($handle);
}
//$crypt->decrypt($file);
}
Trying to write a couple of functions that will encrypt or decrypt a file and am using the class found here to try and accomplish this:
http://www.itnewb.com/v/PHP-Encryption-Decryption-Using-the-MCrypt-Library-libmcrypt
The encryption function below seems to work, in that it appears to encrypt the file and place it in the intended directory. I'm trying to decrypt the file now, and it just dies with the message "Failed to complete decryption" (which is coded in there...) There's nothing in the php error logs, so I'm not sure why it's failing, but as mcrypt is entirely new to me, I'm more than inclined to believe I'm doing something wrong here...
Here are the functions:
//ENCRYPT FILE
function encryptFile() {
global $cryptastic;
$pass = PGPPASS;
$salt = PGPSALT;
$key = $cryptastic->pbkdf2($pass, $salt, 1000, 32) or die("Failed to generate secret key.");
if ($handle = opendir(PATH.'/ftpd')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$newfile = PATH.'/encrypted/'.$file.'.txt';
$msg = file_get_contents(PATH.'/ftpd/'.$file);
$encrypted = $cryptastic->encrypt($msg, $key) or die("Failed to complete encryption.");
$nfile = fopen($newfile, 'w');
fwrite($nfile, $encrypted);
fclose($nfile);
unlink(PATH.'/ftpd/'.$file);
}
}
closedir($handle);
}
//DECRYPT FILE
function inFTP() {
global $cryptastic;
$pass = PGPPASS;
$salt = PGPSALT;
$key = $cryptastic->pbkdf2($pass, $salt, 1000, 32) or die("Failed to generate secret key.");
if ($handle = opendir(PATH.'/encrypted')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$newfile = PATH.'/decrypted/'.$file;
$msg = PATH.'/encrypted/'.$file;
$decrypted = $cryptastic->decrypt($msg, $key) or die("Failed to complete decryption.");
$nfile = fopen($newfile, 'w');
fwrite($nfile, $decrypted);
fclose($nfile);
//unlink(PATH.'/encrypted/'.$file);
}
}
closedir($handle);
}
//$crypt->decrypt($file);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于mcrypt 是废弃软件,不再建议使用,这里有一个使用 openssl 的示例。
输出:
旧答案
尝试使用 mcrypt 这个 PHP5 类进行加密。在本例中,它使用 AES 加密。您需要更改使用它的每个站点的密钥。如果您不使用它,至少它可以指导您编写自己的版本。
用法:
注意:
Since mcrypt is abandonware and no longer recommended to be used, here's an example using openssl.
Output:
Old Answer
Try this PHP5 class for encryption using mcrypt. In this case it's using AES encryption. You'll want to change the key for each site you use it on. If you don't use it at least it may guide you on writing your own version of it.
Usage:
Notes:
虽然 Johns 的回答很好,但仅仅使用 Base64 编码来解决二进制安全问题就有点矫枉过正了,并且会使您的加密文件比原始文件大 33%。这是我的 AES Crypt 文件格式的 PHP 实现,它透明地解决了上述所有问题。
https://github.com/philios33/PHP-AES-File-Encryption
它是二进制安全的,并包含经过身份验证的加密。由于它使用开源 aes crypt 文件格式 (.aes),因此与其他 .aes 软件完全兼容。
https://www.aescrypt.com/
无论您是加密还是解密,界面都非常简单。您只需给它一个源文件和密码即可。
While Johns answer is good, using base64 encoding just to fix the binary safety issue is overkill and will make your encrypted files 33% larger than the original. Here is my PHP Implementation of the AES Crypt file format which solves all the above issues transparently.
https://github.com/philios33/PHP-AES-File-Encryption
It is binary safe and includes authenticated encryption. Since it uses the open source aes crypt file format (.aes) it is fully compatible with other .aes software.
https://www.aescrypt.com/
The interface is pretty simple whether you are encrypting or decrypting. You just give it a source file and password.
您不应该使用 Mcrypt 来加密/解密数据。正如您的问题和接受的答案所示,数据未经身份验证,这意味着它将成为所选密文攻击的受害者。
此外,我们还付出了大量的努力来确保开发人员正确地将加密原语组合在一起。因此,您应该在 PHP 项目中使用 libsodium,而不是 Mcrypt。 libsodium 是 NaCl 的一个分支。编写 NaCl/libsodium 是为了消除开发人员发现的许多加密陷阱,例如通过 MAC 标签验证进行定时攻击。
Mcrypt 在 PHP 7.1 中已弃用,libsodim 是 PHP 中处理加密的首选方法。
在 PHP 项目中使用 libsodium 既简单又安全。 Scott Arciszewski 撰写了一本关于在 PHP 中使用 libsodium 的内容丰富的电子书,网址为 https://paragonie.com/book/pecl -lib钠。对于任何从事 PHP 密码学研究的人来说,这本书都值得一读。
You should not be using Mcrypt to encrypt/decrypt data. As shown in your question, and in the accepted answer, the data is not authenticated, which means it will fall victim to chosen ciphertext attacks.
Further, a great deal of effort has been done to make sure that developers put together cryptographic primitives correctly. As such, instead of Mcrypt, you should be using libsodium for your PHP projects. libsodium is a fork of NaCl. NaCl/libsodium is written to remove a lot of the cryptographic pitfalls that developers find themselves in, such as timing attacks with verification of MAC tags.
Mcrypt is deprecated in PHP 7.1, and libsodim is the preferred way to handle cryptography in PHP.
Using libsodium in your PHP project is easy, and secure. Scott Arciszewski has written an extensive ebook on using libsodium with PHP at https://paragonie.com/book/pecl-libsodium. It's worth the read for anyone doing PHP cryptography.
CakePHP 有一个相当不错的rijndael 的实现。我不会直接在这里发布代码,因为不确定法律后果。
这里是
Security::rijndael( )
方法。如果对文件进行编码,您需要在使用“
加密
”调用此方法之前使用base64_encode()
,并在调用此方法之后使用base64_decode()
与“解密
”CakePHP has a pretty good implementation of rijndael. I'm not posting code directly here because not sure the legal ramifications.
Here are the api docs for the
Security::rijndael()
method.If encoding a file, you will want to
base64_encode()
before calling this method with 'encrypt
', andbase64_decode()
after calling this method with 'decrypt
'