1: <?php
2:
3: namespace Genetsis\core;
4:
5: 6: 7: 8: 9: 10: 11: 12:
13: class Encryption
14: {
15:
16: private $_skey = "yourSecretKey";
17:
18: 19: 20:
21: public function __construct($client_secret)
22: {
23: $this->_skey = str_pad(trim((string)$client_secret), 32, '\0');
24: }
25:
26: 27: 28: 29: 30: 31:
32: public function encode($value)
33: {
34: if (!$value) {
35: return false;
36: }
37: $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
38: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
39: $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_skey, $value, MCRYPT_MODE_ECB, $iv);
40: return trim($this->safe_b64encode($crypttext));
41: }
42:
43: 44: 45: 46: 47: 48:
49: public function safe_b64encode($string)
50: {
51: return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode($string));
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function decode($value)
61: {
62: if (!$value) {
63: return false;
64: }
65: $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
66: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
67: $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->_skey, $this->safe_b64decode($value), MCRYPT_MODE_ECB, $iv);
68: return trim($decrypttext);
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function safe_b64decode($string)
78: {
79: $data = str_replace(array('-', '_'), array('+', '/'), $string);
80: $mod4 = (strlen($data) % 4);
81: if ($mod4) {
82: $data .= substr('====', $mod4);
83: }
84: return base64_decode($data);
85: }
86: }