Encrypt (large) files in PHP using openSSL

I am trying to encrypt (large) files in PHP using AES and have studied the use of Mcrypt and OpenSSL, the problem is that all the solutions that I found only encrypt strings, and the files that I try to encrypt run the maximum memory limit for PHP ( which, unfortunately, cannot be installed above), how could I achieve this?

+4
source share
4 answers

You can use CBC encryption with Mcrypt, and then encrypt the data segment at a time. Make sure that the segment has x times the block size of the cipher being used (for example, 16 bytes for AES). Encrypt the segment and take the last block of generated ciphertext and use it as IV for the next segment. The final segment should contain PKCS # 7 (many examples, including in the comments mcrypt_encrypt).

By linking the segments together, you get encrypted text indistinguishable from one encryption (check your code using this information). Decryption is identical using ciphertext as IV. To find out how this works, take a look at the CBC encryption method:

enter image description here


EDIT: , OpenSSL. () , , , , . , , .

+3

I published two functions that encrypt and decrypt even large files openssl_encrypt()using the AES-128-CBC algorithm.

Please refer to openssl_encrypt () .

0
source

Using SSL on your site will take care of this for you. Any files that are transferred are encrypted by the client browser and server using the HTTPS protocol.

As for storing encrypted versions of files, I would not recommend it.

-2
source

All Articles