Home > Backend Development > PHP Tutorial > How Can I Encrypt and Decrypt Files Using OpenSSL in PHP?

How Can I Encrypt and Decrypt Files Using OpenSSL in PHP?

Susan Sarandon
Release: 2024-11-17 02:57:03
Original
254 people have browsed it

How Can I Encrypt and Decrypt Files Using OpenSSL in PHP?

Encrypting and Decrypting Files with MCrypt

The Mcrypt library provides functions for encryption and decryption operations in PHP. Here's an example of how to use it for encrypting and decrypting files:

// ENCRYPT FILE
function encryptFile() {
    $key = generateKey(); // Function to generate a secure encryption key
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc')); // Generate a random initialization vector

    $plaintext = file_get_contents(PATH . '/ftpd/' . $file);
    $encrypted = openssl_encrypt($plaintext, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv);

    $encryptedFile = fopen(PATH . '/encrypted/' . $file . '.txt', 'w');
    fwrite($encryptedFile, $iv . $encrypted);
    fclose($encryptedFile);

    unlink(PATH . '/ftpd/' . $file);
}

// DECRYPT FILE
function decryptFile() {
    $key = generateKey(); // Function to generate the same encryption key used in encryption

    if ($handle = opendir(PATH . '/encrypted')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $encryptedFile = fopen(PATH . '/encrypted/' . $file, 'r');
                $encryptedData = fread($encryptedFile, filesize(PATH . '/encrypted/' . $file));

                $iv = substr($encryptedData, 0, openssl_cipher_iv_length('aes-128-cbc'));
                $decrypted = openssl_decrypt(substr($encryptedData, openssl_cipher_iv_length('aes-128-cbc')), 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv);

                $decryptedFile = fopen(PATH . '/decrypted/' . $file, 'w');
                fwrite($decryptedFile, $decrypted);
                fclose($decryptedFile);

                // unlink(PATH . '/encrypted/' . $file);
            }
        }
        closedir($handle);
    }
}
Copy after login

Important notes:

  • MCrypt is an outdated library and is no longer recommended for use. Consider using newer and more secure alternatives, such as OpenSSL or Libsodium.
  • The encryption/decryption key must be securely generated and kept confidential.
  • Initialization vectors (IVs) provide additional security by making the ciphertext unique each time for the same plaintext and key.

The above is the detailed content of How Can I Encrypt and Decrypt Files Using OpenSSL in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template