· 6 years ago · Aug 30, 2019, 05:26 PM
1<?php
2
3class MBM_Encrypt_Decrypt {
4 const ENCRYPT_METHOD = 'AES-256-CBC'; // type of encryption
5 const SECRET_KEY = 'rappier_simple_secret_key'; // secret key
6 const SECRET_IV = 'rappier_simple_secret_iv'; // secret iv
7
8 public function encrypt($string) {
9 return $this->encrypt_decrypt('encrypt', $string);
10 }
11
12 public function decrypt($string) {
13 return $this->encrypt_decrypt('decrypt', $string);
14 }
15 private function encrypt_decrypt($action, $string)
16 {
17 $key = hash('sha256', self::SECRET_KEY, true);
18 $iv = substr(hash('sha256', self::SECRET_IV, true), 0, 16);
19 if ($action == 'encrypt') {
20 $output = openssl_encrypt($string, self::ENCRYPT_METHOD, $key, 0, $iv);
21 } else if ($action == 'decrypt') {
22 $output = openssl_decrypt($string, self::ENCRYPT_METHOD, $key, 0, $iv);
23 }
24 $output = (!empty($output)) ? $output : false;
25 return $output;
26 }
27}
28
29
30$class_encrypt = new MBM_Encrypt_Decrypt();
31
32$plain_txt = "ajitem";
33echo 'Plain Text: ' . $plain_txt . PHP_EOL;
34
35$encrypted_txt = $class_encrypt->encrypt($plain_txt);
36echo 'Ciphertext: ' . $encrypted_txt . PHP_EOL;