Encrypt and Decrypt files using PHP

Today, We want to share with you php encrypt file.In this post we will show you php encryption and decryption code, hear for php encryption library we will give you demo and example for implement.In this post, we will learn about JavaScript Client Side Password Hashing And Encryption with an example.

PHP – file encryption/decryption

In my recent website I have implemented step by step PDF file encryption and decryption. In this website, all the data is very useful and sensitive therefor to high security and protect all the files from unauthorized access as well as to keep them safe as well as secure i have used file based encryption/decryption best way. In this post, I will learn you how to encrypt/decrypt files using PHP.

i am going to use “Mcrypt” php extension file to encrypt/decrypt files format using a given key as well as some salt vector, therefor please 100% make sure Mcrypt is with your main PHP instillation. If you do not have this extension, step by step install it first, Here is Best way – install Mcrypt Extension

Read Also:  Push Notification System with PHP & MySQL Demo [updated - 2021]

Here is the PHP function for encrypt file.

function encrypt_file($file, $destination, $all_arguments) {
     
	$handle = fopen($file, "rb") or die("sorry, Could not open a file."); 

	$contents = fread($handle, filesize($file));
 
	fclose($handle); 

	$iv = substr(md5("\x1B\x3C\x58".$all_arguments, true), 0, 8);
	$key = substr(md5("\x2D\xFC\xD8".$all_arguments, true) . md5("\x2D\xFC\xD9".$all_arguments, true), 0, 24);
	$opts = array('iv'=>$iv, 'key'=>$key);
	$fp = fopen($destination, 'wb') or die("Sorry, Could not open file for writing.");

	stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts); 

	fwrite($fp, $contents) or die("sorry, Could not write to file."); 

	fclose($fp); 
 
}

  • Open the file and returns a file pointer resource.
  • Returns the read string.
  • Close the opened file pointer.
  • Add the Mcrypt stream filter with Triple DES
  • Write content in the destination file.
  • Close the opened file pointer.
Read Also:  PHPMailer Sending Emails using PHP Code

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

function decrypt_file($file,$passphrase) {
	$iv = substr(md5("\x1B\x3C\x58".$passphrase, true), 0, 8);
	$key = substr(md5("\x2D\xFC\xD8".$passphrase, true) .
	md5("\x2D\xFC\xD9".$passphrase, true), 0, 24);
	$opts = array('iv'=>$iv, 'key'=>$key);
	$fp = fopen($file, 'rb');
	stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
	return $fp;
}

You can use above function as shown below.

encrypt_file('/path/to/file','./upload','YOUR_SECRATE_FILE');


$pass_decrypted = decrypt_file('/path/to/file','YOUR_SECRATE_FILE');
header('Content-type: application/pdf');
fpassthru($pass_decrypted);

I hope you get an idea about php encrypt file.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.