Open SSL Encrypt/Decrypt file

PHP lacks a build-in function to encrypt and decrypt large files.

openssl_encrypt() can be used to encrypt strings, but loading a huge file into memory is a bad idea.

So we have to write a userland function doing that. This example uses the symmetric AES-128-CBC algorithm to encrypt smaller chunks of a large file and writes them into another file.

# Encrypt Files

Encrypt code is visible in PHP source.

# Decrypt Files

To decrypt files that have been encrypted with the above function you can use this function.

Decrypt code is visible in PHP source.

5844454259456830576b7853417a2f662f6257727a64676e6b73384b41485a776e7a2f4b654957493636733d
$enc_data = base64_encode( openssl_encrypt( $data, $method, $pass, true, $iv ) );
$dec_data = openssl_decrypt( base64_decode( $enc_data ), $method, $pass, true, $iv );

Source ➠ http://stackoverflow.com/documentation/php/5794/cryptography/25499/
(Source at StackOverflow does not exist anymore)

See PHP code of this page.

Jul 04 2022 17:06:03