使用openSSL在PHP中加密(大)文件 [英] Encrypting (large) files in PHP with openSSL

查看:315
本文介绍了使用openSSL在PHP中加密(大)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AES加密(大)的PHP文件,并使用Mcrypt和OpenSSL进行了调查,问题是我迄今为止发现的只有加密字符串的解决方案,而我正在加密的文件将会触发PHP的最大内存限制(不幸的是不能设置更高),我该如何去实现?

解决方案

您可以使用Mcrypt使用CBC加密,然后一次加密一段数据。确保该段是所使用密码的块大小的x倍(例如AES的16个字节)。加密段并取最终生成的密文块,并将其用作下一段的IV。最后一段应该是PKCS#7填充(大量例子包括在 mcrypt_encrypt 注释中)。



通过将段链接在一起,您将获得一个与单个加密无法区分的密文(使用此信息测试您的代码)。解密是相同的,使用密文作为IV。要查看它的工作原理,请查看CBC加密方法:








编辑:如果可能,您应该使用OpenSSL等效功能。这不是很好的记录,但你应该能够使用在Scott提到的评论链接中找到的代码。请注意,您应该首先执行所有没有填充的内容,然后使用填充进行最终细分。


I'm trying to encrypt (big) files in PHP using AES and have looked into using Mcrypt and OpenSSL, the problem is all solutions I have found so far only encrypt strings, and the files I'm trying to encrypt would trigger the max memory limit for PHP (which unfortunately can't be set higher), how would I go about achieving this?

解决方案

You could use CBC encryption using Mcrypt and then encrypt a segment of data at a time. Make sure that the segment is x times the block size of the used cipher (e.g. 16 bytes for AES). Encrypt the segment and take the last block of the generated ciphertext and use it as IV for the next segment. The final segment should be PKCS#7 padded (plenty of examples out there including in the mcrypt_encrypt comments).

By chaining the segments together you get a ciphertext indistinguishable from a single encrypt (test your code using this information). Decryption is identical, using the ciphertext as IV. To see how it works, look at the CBC encryption method:


EDIT: if possible you should use the OpenSSL equivalent functionality. That's not (well) documented, but you should be able to do the same using the code found in the link within the comment that Scott mentioned. Note that you should first perform everything without padding, and then for the final segment with padding.

这篇关于使用openSSL在PHP中加密(大)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆