· 7 years ago · Jun 19, 2018, 08:22 AM
1<?php
2 class Encryptdecrypt{
3 private $iv; #Same as in JAVA
4 private $key; #Same as in JAVA
5 private $secret_key; #Same as in JAVA
6 private static $OPENSSL_CIPHER_NAME = "aes-128-cbc"; //Name of OpenSSL Cipher
7 private static $CIPHER_KEY_LEN = 500; //128 bits
8 function __construct($params = array()){
9 $param = $params;
10 $this->iv = isset($param['information_key'])?$param['information_key']:'';
11 $this->key = isset($param['token'])?$param['token']:'';
12 $this->secret_key = isset($param['secret_key'])?$param['secret_key']:'';
13 }
14
15 /**
16 * @param string $str
17 * @param bool $isBinary whether to encrypt as binary or not. Default is: false
18 * @return string Encrypted data
19 */
20 /*function encrypt($str, $isBinary = false){
21 $iv = $this->iv;
22 $str = $isBinary ? $str : utf8_decode($str);
23 $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
24 mcrypt_generic_init($td, $this->key, $iv);
25 $encrypted = mcrypt_generic($td, $str);
26 mcrypt_generic_deinit($td);
27 mcrypt_module_close($td);
28 return $isBinary ? $encrypted : bin2hex($encrypted);
29 }*/
30 function _decrypt_secret_key(){
31 $secret_key = base64_decode($this->secret_key);
32 $fp = fopen("./assets/certificates/mpesachama.key", "r");
33 $privateKey = fread($fp, 8192);
34 fclose($fp);
35 $res = openssl_get_privatekey($privateKey);
36 if(!$res){
37 echo "Cannot get public key";die;
38 }
39 openssl_private_decrypt($secret_key, $decrypted, $privateKey);
40 return $decrypted?:FALSE;
41 }
42
43 function _encrypt_secret_key($string = 0){
44 if($string){
45 $fp=fopen("./assets/certificates/mpesachama.crt","r");
46 $pub_key_string=fread($fp,8192);
47 fclose($fp);
48 $PK=openssl_get_publickey($pub_key_string);
49 if (!$PK) {
50 echo "Cannot get public key";die;
51 }
52 openssl_public_encrypt($string,$crypttext,$pub_key_string);
53 return(base64_encode($crypttext));
54 }else{
55 return FALSE;
56 }
57 }
58 function encrypt($string,$isBinary = false){
59 $iv = time().'123456';
60 $secret_key = '1234656789';
61 $key = $this->_encrypt_secret_key($secret_key);
62 if (strlen($key) < Encryptdecrypt::$CIPHER_KEY_LEN) {
63 $key = str_pad("$key", Encryptdecrypt::$CIPHER_KEY_LEN, "0"); //0 pad to len 16
64 } else if (strlen($key) > Encryptdecrypt::$CIPHER_KEY_LEN) {
65 $key = substr($key, 0, Encryptdecrypt::$CIPHER_KEY_LEN); //truncate to 16 bytes
66 }
67 echo $key.'<br/>';
68 $encodedEncryptedData = base64_encode(openssl_encrypt($string, Encryptdecrypt::$OPENSSL_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv));
69 $encodedIV = base64_encode($iv);
70 $encryptedPayload = $encodedEncryptedData.":".$encodedIV;
71
72 return $encryptedPayload;
73 }
74
75 function decrypt($encrypted,$isBinary = false){
76 $encyption_key = $this->_decrypt_secret_key();
77 if (strlen($encyption_key) < Encryptdecrypt::$CIPHER_KEY_LEN) {
78 $encyption_key = str_pad("$encyption_key", Encryptdecrypt::$CIPHER_KEY_LEN, "0"); //0 pad to len 16
79 } else if (strlen($encyption_key) > Encryptdecrypt::$CIPHER_KEY_LEN) {
80 $encyption_key = substr($str, 0, Encryptdecrypt::$CIPHER_KEY_LEN); //truncate to 16 bytes
81 }
82 $parts = explode(':', $encrypted); //Separate Encrypted data from iv.
83 $decryptedData = openssl_decrypt(base64_decode($parts[0]), Encryptdecrypt::$OPENSSL_CIPHER_NAME, $encyption_key, OPENSSL_RAW_DATA, base64_decode($parts[1]));
84
85 return $decryptedData;
86 }
87
88 /**
89 * @param string $code
90 * @param bool $isBinary whether to decrypt as binary or not. Default is: false
91 * @return string Decrypted data
92 */
93 /*function decrypt($code, $isBinary = false){
94 $code = $isBinary ? $code : $this->hex2bin($code);
95 $iv = $this->iv;
96 $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
97 mcrypt_generic_init($td, $this->key, $iv);
98 $decrypted = mdecrypt_generic($td, $code);
99 mcrypt_generic_deinit($td);
100 mcrypt_module_close($td);
101 return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
102 }*/
103
104 protected function hex2bin($hexdata){
105 $bindata = '';
106 for ($i = 0; $i < strlen($hexdata); $i += 2) {
107 $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
108 }
109 return $bindata;
110 }
111}
112?>