In this article, you going to see how one can encrypt and decrypt PHP source code or any file in which contains important information.
Before going to discuss regarding encrypt and decrypt topic, let’s check with some important point of encryption and decryption.
Let’s start with important points of encrypt and decrypt.
About encryption and why it is necessary? ( encrypt, decrypt PHP code)
Today you can say it is internet world.
One can send data information from one source to another destination is URL.
While sending of this data consist of important data.
Now the important point is how one user can send secure data information from one network to another destination network.
When you send data using URL at that time of processing anyone can see the data and use that data for misuse purpose.
To avoid this misusing of data, We will going to send encrypted data and important information or a message.
On the another end or you can called destination page we will decrypt this data or important information.
Encryption is a process of manipulation or changing of information in a such manner that one cannot make it unreadable.
Encryption process is become very important in our day to day life in case of secure data sending.
It is used by Businesses, Government and Many individuals to protect personal information, corporate secrets to secure against theft.
Below you will see syntax of encrypt.
mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] ) : string
Parameter | Description |
cipher | It is the name of the algorithm which is MCRYPT_ciphername constants |
key | Data will be encrypted with the help of this key. |
data | With the help of given cipher and mode data get encrypted. |
mode | It is a constant called MCRYPT_MODE_modename. |
iv | It is used for the initialization process. |
How to encrypt data?
Below is that example for encrypting of data.
$key = pack('H*', "bcb8907e103a0cd8b54763051cfd08bc55abe029fvfghj5e1d417e2ffb2a00a3");
define('KEY', $key);
function encrypt($ENDATA)
{
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, KEY,
$ENDATA, MCRYPT_MODE_ECB, mcrypt_create_iv(
mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
MCRYPT_RAND))));
}
Now our next step is to decrypt data which we get ant destination point.
How to decrypt data?
Below is syntax for decrypt.
mcrypt_decrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] ) : string
Parameter | Description |
cipher | The algorithm which is MCRYPT_ciphername constants |
key | It is the key that helps in data decryption at the destination point. |
data | It is the secret data which is getting to be decrypted |
mode | It is a constant MCRYPT_MODE_modename. |
iv | Using this you can do initialization. |
Below is the example for decrypt of data.
$key = pack('H*', "bcb8907e103a0cd8b54763051cfd08bc55abe029fvfghj5e1d417e2ffb2a00a3");
define('KEY', $key);
function decrypt($DEDATA)
{
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, KEY,
base64_decode($DEDATA), MCRYPT_MODE_ECB,
mcrypt_create_iv(mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
$encryptmsg = encrypt("message");
echo decrypt($encryptmsg);
“[Guide] htaccess remove PHP not working“
Another method to do encrypt or decrypt or encrypt and decrypt php source code.
In this method one file get encrypt or decrypt.
<?php
function CryptFile($Infilename,$Outfilename,$password){
//check the file if exists
if (file_exists($Infilename)){
//get file content as string
$Infile = file_get_contents($Infilename);
// get string length
$StrLen = strlen($Infile);
// get string char by char
for ($i = 0; $i < $StrLen ; $i++){
//current char
$chr = substr($Infile,$i,1);
//get password char by char
$modulus = $i % strlen($password);
$passwordchr = substr($password,$modulus, 1);
//encryption algorithm
$Outfile .= chr(ord($chr)+ord($passwordchr));
}
$Outfile = base64_encode($Outfile);
//write to a new file
if($newfile = fopen($Outfilename, "c")){
file_put_contents($Outfilename,$Outfile);
fclose($newfile);
return true;
}else{
return false;
}
}else{
return false;
}
}
function DecryptFile($Infilename,$Outfilename,$password){
//check the file if exists
if (file_exists($Infilename)){
//get file content as string
$Infile = file_get_contents($Infilename);
$Infile = base64_decode($Infile);
// get string length
$StrLen = strlen($Infile);
// get string char by char
for ($i = 0; $i < $StrLen ; $i++){
//current char
$chr = substr($Infile,$i,1);
//get password char by char
$modulus = $i % strlen($password);
$passwordchr = substr($password,$modulus, 1);
//encryption algorithm
$Outfile .= chr(ord($chr)-ord($passwordchr));
}
//write to a new file
if($newfile = fopen($Outfilename, "c")){
file_put_contents($Outfilename,$Outfile);
fclose($newfile);
return true;
}else{
return false;
}
}else{
return false;
}
}
//Example
$orginalfile = 'orginal.jpg';
$crypt = 'crypt.jpg';
$decrypt = 'decrypt.jpg';
$password = 'pass';
CryptedFile($orginalfile,$crypt,$password);
DecryptedFile($crypt,$decrypt,$password);
?>
I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.