Last active
December 17, 2015 20:38
-
-
Save javagrails/5668805 to your computer and use it in GitHub Desktop.
Simple Class for PHP Encryption Decryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class EnDecryption | |
{ | |
public static $saltKey = ''; //You Can Change This as Your Own By Calling Constructor | |
function __construct($salt = 'setDefaultSalt') | |
{ | |
//print 'Default Salt : ' . $salt; | |
EnDecryption::$saltKey = $salt; | |
//print '<br/>EnDecryption::saltKey : ' . EnDecryption::$saltKey . '<br/>'; | |
} | |
private static function doBase64Encode($string) | |
{ | |
$data = base64_encode($string); | |
$data = str_replace(array('+', '/', '='), array('-', '_', ''), $data); | |
return $data; | |
} | |
private static function doBase64Decode($string) | |
{ | |
$data = str_replace(array('-', '_'), array('+', '/'), $string); | |
$modBy4 = strlen($data) % 4; | |
if ($modBy4) { | |
$data .= substr('====', $modBy4); | |
} | |
return base64_decode($data); | |
} | |
public static function encrypt($eyValue) | |
{ | |
if (!$eyValue) { | |
return false; | |
} | |
$text = $eyValue; | |
$initialVectorSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); | |
$initializeVector = mcrypt_create_iv($initialVectorSize, MCRYPT_RAND); | |
$encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, EnDecryption::$saltKey, $text, MCRYPT_MODE_ECB, $initializeVector); | |
return trim(EnDecryption::doBase64Encode($encryptText)); | |
} | |
public static function decrypt($dyValue) | |
{ | |
if (!$dyValue) { | |
return false; | |
} | |
$encryptText = EnDecryption::doBase64Decode($dyValue); | |
$initialVectorSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); | |
$initializeVector = mcrypt_create_iv($initialVectorSize, MCRYPT_RAND); | |
$decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, EnDecryption::$saltKey, $encryptText, MCRYPT_MODE_ECB, $initializeVector); | |
return trim($decryptText); | |
} | |
} | |
/*use like below in any PHP script*/ | |
echo '<!DOCTYPE html>'; | |
echo '<html>'; | |
echo '<head>'; | |
echo '<title>'; | |
echo 'PHP Encryption Decryption By Salman'; | |
echo '</title>'; | |
echo '</head>'; | |
echo '<body>'; | |
echo '<div style="border:1px solid blue;text-align:left;padding:50px 50px 50px 50px;background-color:#E0E6F8;">'; | |
echo 'Sample Call of PHP Class <b>EnDecryption</b><br/>'; | |
$realData = "encrypt Salman The Bad Boy"; | |
$mySalt = "youAreSalty420"; | |
//Encryption Decryption Operations | |
$enDecryptionObj = new EnDecryption($mySalt); //to set salt not mandatory | |
$encryptedValue = EnDecryption::encrypt($realData); //encrypt real data | |
$decryptedValue = EnDecryption::decrypt($encryptedValue); //decrypt encrypted value of real data | |
echo '<br/>'; | |
echo '<div style="color:blue;">realData : '.$realData.'</div>'; | |
echo '<br/>'; | |
echo '<div style="color:red;">encryptedValue : '.$encryptedValue.'</div>'; | |
echo '<br/>'; | |
echo '<div style="color:green;">decryptedValue : '.$decryptedValue.'</div>'; | |
echo '<br/>'; | |
echo '<hr/>'; | |
echo '<div style="color:black;">Java Grails Gist Link : <a href="https://gist.github.com/javagrails/5668805">Click for Source</a></div>'; | |
echo '</div>'; | |
echo '</body>'; | |
echo '</html>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment