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.

0 0 vote
Article Rating
Subscribe
Notify of
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SUBHANGI NAYAK
SUBHANGI NAYAK
2 years ago

sir what todoif we want to encrypt any other type of file like doc,image or excel.
can you please help me .

arjun
2 years ago
Reply to  SUBHANGI NAYAK

You can use this script for all type of file,just set the Content-type header according

SUBHANGI NAYAK
SUBHANGI NAYAK
2 years ago
Reply to  arjun

Sir actually I tried so but the results of doc file contains some weird variables and symbols.
I am unable to understand why this is happening.

arjun
2 years ago
Reply to  SUBHANGI NAYAK

I will check in my free time, I have PHP 7 setup now, I have to downgrade to verify this.

For now I have verified below code with .js file and it is working you can try this and let me know if you face any issues: ( code borrowed from internet)

/* @const CIPHER encryption algorithm */
const CIPHER = ‘AES-256-CTR’;
/* other strong ciphers: AES-256-CBC, CAMELLIA-256-CBC */

/* @const KEY_HASH, hash for password hashing and stretching */
const KEY_HASH = ‘SHA512’;

/* @const KEY_STRETCHES, number of key stretch iterations */
const KEY_STRETCHES = 2 ** 16; /* increase towards paranoia */

/* @const HMAC_HASH, hash for HMAC */
const HMAC_HASH = ‘SHA512’;

/* @const HMAC_LEN, HMAC_HASH byte length */
const HMAC_LEN = 64;

/* @const FILE_EXT, encrypted file extension suffix */
const FILE_EXT = ‘.osl’;

/**
* Encrypt a file.
*
* @param string $sFilename, the file to encrypt
* @param string $sPassword, the password
* @return string, message
*/

function encrypt($sFilename, $sPassword) {

$sPlaintext = file_get_contents($sFilename);

$sKey = $sPassword;

for ($i = 0; $i < KEY_STRETCHES; $i++) {
$sKey = openssl_digest($sKey, KEY_HASH);
}

$iIVLen = openssl_cipher_iv_length(CIPHER);
$IV = random_bytes($iIVLen);

$sCiphertextRaw = openssl_encrypt($sPlaintext, CIPHER, $sKey, OPENSSL_RAW_DATA, $IV);

$HMAC = hash_hmac(HMAC_HASH, $sCiphertextRaw, $sKey, $binary=true);

$sCiphertext = $IV . $HMAC . $sCiphertextRaw;

$iF = file_put_contents($sFilename . FILE_EXT, $sCiphertext);

if ($iF)
{
return PHP_EOL . ' '' . $sFilename . FILE_EXT . '' saved' . PHP_EOL;
}
else
{
return PHP_EOL . ' encryption failed' . PHP_EOL;
}
}

/**
* Decrypt a file.
*
* @param string $sFilename, the file to decrypt
* @param string $sPassword, the password
* @return string, message
*/

function decrypt($sFilename, $sPassword = '')
{

$sCiphertext = file_get_contents($sFilename);

$sKey = $sPassword;

for ($i = 0; $i < KEY_STRETCHES; $i++)
{
$sKey = openssl_digest($sKey, KEY_HASH);
}

$iIVLen = openssl_cipher_iv_length(CIPHER);
$IV = substr($sCiphertext, 0, $iIVLen);
$HMAC = substr($sCiphertext, $iIVLen, HMAC_LEN);

$sCiphertextRaw = substr($sCiphertext, $iIVLen + HMAC_LEN);
$sDecryptedText = openssl_decrypt($sCiphertextRaw, CIPHER, $sKey, OPENSSL_RAW_DATA, $IV);
$sCalcMAC = hash_hmac(HMAC_HASH, $sCiphertextRaw, $sKey, $binary=true);

if (hash_equals($HMAC, $sCalcMAC))
{
$iF = file_put_contents(basename($sFilename, FILE_EXT), $sDecryptedText);

if ($iF)
{
return PHP_EOL . ' decrypted file '' . basename($sFilename, FILE_EXT) . '' saved' . PHP_EOL;
}
else
{
return PHP_EOL . ' file creation failed' . PHP_EOL;
}
}
else
{
return PHP_EOL . ' authentication failure: incorrect password (or file corruption) ' . PHP_EOL;
}
}

//encrypt('./d.js','test');

decrypt('./d.js.osl','test');

Vignesvaran b
Vignesvaran b
2 years ago

Is there any other way to use decrypted data/image into image tag like

Vishnu
Vishnu
2 years ago

Insalled the mcrypt and use this code on laravel , error : stream_filter_append(): unable to locate filter mcrypt.tripledes

Douglas Hipocreme
Douglas Hipocreme
2 years ago

can i contact you ? we need decrypt some files

ram rajput
ram rajput
1 year ago

hi,

i m using php 7 in windows 10, how can i install mcrypt?

ayua
ayua
1 month ago

how can i get the full source code

DMCA.com Protection Status
10
0
Would love your thoughts, please comment.x
()
x