Encrypt and Decrypt files using PHP

In my recent project I have implemented PDF file encryption and decryption. In this project, data is very sensitive so to protect files from unauthorized access and to keep them safe and secure we have used file based encryption/decryption method. In this article, I will teach you how to encrypt/decrypt files in PHP.

We gonna use “Mcrypt” php extension to encrypt/decrypt files using a given key and salt vector, so please make sure Mcrypt is with your PHP instillation. If you do not have this extension, please install it first, Here is tutorial – How to install Mcrypt

Here is the function for encrypt file.

Here is the function for decrypt. The decrypted data can be returned as a string or served for download.

You can use above function as shown below.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.