Skip to content

Instantly share code, notes, and snippets.

@asahasrabuddhe
Last active August 19, 2019 03:47
Show Gist options
  • Save asahasrabuddhe/a8d86c439e34d566c1808bb0b98c8b31 to your computer and use it in GitHub Desktop.
Save asahasrabuddhe/a8d86c439e34d566c1808bb0b98c8b31 to your computer and use it in GitHub Desktop.
encrypt and decrypt
<?php
class MBM_Encrypt_Decrypt {
const ENCRYPT_METHOD = 'AES-256-CBC'; // type of encryption
const SECRET_KEY = 'rappier_simple_secret_key'; // secret key
const SECRET_IV = 'rappier_simple_secret_iv'; // secret iv
public function encrypt($string) {
return $this->encrypt_decrypt('encrypt', $string);
}
public function decrypt($string) {
return $this->encrypt_decrypt('decrypt', $string);
}
private function encrypt_decrypt($action, $string)
{
$key = hash('sha256', self::SECRET_KEY, true);
$iv = substr(hash('sha256', self::SECRET_IV, true), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, self::ENCRYPT_METHOD, $key, 0, $iv);
} else if ($action == 'decrypt') {
$output = openssl_decrypt($string, self::ENCRYPT_METHOD, $key, 0, $iv);
}
$output = (!empty($output)) ? $output : false;
return $output;
}
}
$class_encrypt = new MBM_Encrypt_Decrypt();
$plain_txt = "ajitem";
echo 'Plain Text: ' . $plain_txt . PHP_EOL;
$encrypted_txt = $class_encrypt->encrypt($plain_txt);
echo 'Ciphertext: ' . $encrypted_txt . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment