In PHP, generally we use base64_encode() and base64_decode() methods to encrypt the data but it is not secure, anyone can extract data behind this encryption, But if you apply some extra security layer this will make user’s data more secure, means add some solt to data and then encrypt the data.
<?php/*
* Author: prashanthi
* Website: http://prashanthikontemukkula.blogspot.in/
* Date: 01-06-2016
* App Name: encrypt, decrypt data
* Description: A simple OOPS based call to encrypt, decrypt your data
*/
class Secure {
// Set your unique has keys
private static $secretKey = 'phpdeveloper';
private static $secretIv = 'ils';
// Encryption method
private static $encryptMethod = "AES-256-CBC";
// pass string/number which you want to encrypt
public static function encrypt($data) {
$key = hash('sha256', self::$secretKey);
$iv = substr(hash('sha256', self::$secretIv), 0, 16);
$result = openssl_encrypt($data, self::$encryptMethod, $key, 0, $iv);
return $result= base64_encode($result);
}
// pass encrypted data to decrypt
public static function decrypt($data) {
$key = hash('sha256', self::$secretKey);
$iv = substr(hash('sha256', self::$secretIv), 0, 16);
$result = openssl_decrypt(base64_decode($data), self::$encryptMethod, $key, 0, $iv);
return $result;
}
}
?>
First include above class one page where you want to use it after that calling it’s function.
include_once('secure.php');
//encrypt the data echo $has = secure::encrypt("prashanthi");
//decrypt the data
echo secure::decrypt('SXpTekhPN2NsNlJFWlRCWnVrNGxOZz09');