Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Last active July 5, 2016 21:20
Show Gist options
  • Save omarkdev/bf0f0942910e801aa7015cb79f8b8c52 to your computer and use it in GitHub Desktop.
Save omarkdev/bf0f0942910e801aa7015cb79f8b8c52 to your computer and use it in GitHub Desktop.
Encryption and Decryption in PHP
<?php
class mCode{
protected function enc($string){
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'secret-key-1';
$secret_iv = 'secret-key-2';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
return $output;
}
protected function dec($string){
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'secret-key-1';
$secret_iv = 'secret-key-2';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment