Showing posts with label php encrypt. Show all posts
Showing posts with label php encrypt. Show all posts

Wednesday, August 20, 2014

best method to encrypt or decrypt in php

Here is a safe encrypt and decrypt function for php developer. You can Encrypt and Decrypt using md5. The md5() function calculates the MD5 hash of a string. md5() and sha1() provides the same functinality of encryption in php but they differ in a simple way that md5() generates 32 characters of encrypted string which sha1() generates same of 40 characters.

<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.com/
*/


function encrypt_decrypt($action, $string) {
   $output = false;

   $key = 'My strong random secret key';

   // initialization vector 
   $iv = md5(md5($key));

   if( $action == 'encrypt' ) {
       $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
       $output = base64_encode($output);
   }
   else if( $action == 'decrypt' ){
       $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv);
       $output = rtrim($output, "");
   }
   return $output;
}

?>

How to use encrypt or decrypt code:
 
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.com/
*/


$plain_txt = "This is my plain text";
$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted = $encrypted_txt\n";
echo "
"; $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt); echo "Decrypted = $decrypted_txt\n"; ?>

Wednesday, January 23, 2013

Simple method to encrypt and decrypt files in PHP

Simple way to encrypt and decrypt files in php

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/



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';

CryptFile($orginalfile,$crypt,$password);
DecryptFile($crypt,$decrypt,$password);

?>






 

© 2014 4everTutorials. All rights resevered.

Back To Top