| 222 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           namespace LeadersLinked\Library;
  | 
        
        
            | 
            | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           class EasyAES {
  | 
        
        
            | 
            | 
           8 | 
               private $iv;
  | 
        
        
            | 
            | 
           9 | 
               private $key;
  | 
        
        
            | 
            | 
           10 | 
               private $bit; //Only can use 128, 256
  | 
        
        
            | 
            | 
           11 | 
               private $mode;
  | 
        
        
            | 
            | 
           12 | 
              | 
        
        
            | 
            | 
           13 | 
               function __construct($key, $bit = 128, $iv = "") {
  | 
        
        
            | 
            | 
           14 | 
                   $this->bit = $bit;
  | 
        
        
            | 
            | 
           15 | 
                   // gen key
  | 
        
        
            | 
            | 
           16 | 
                   if($this->bit == 256){
  | 
        
        
            | 
            | 
           17 | 
                       $this->key = hash('SHA256', $key, true);
  | 
        
        
            | 
            | 
           18 | 
                       $this->mode = 'AES-256-CBC';
  | 
        
        
            | 
            | 
           19 | 
                   } else {
  | 
        
        
            | 
            | 
           20 | 
                       $this->key = hash('MD5', $key, true);
  | 
        
        
            | 
            | 
           21 | 
                       $this->mode = 'AES-128-CBC';
  | 
        
        
            | 
            | 
           22 | 
                   }
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
                   // gen iv
  | 
        
        
            | 
            | 
           25 | 
                   if($iv != ""){
  | 
        
        
            | 
            | 
           26 | 
                       $this->iv = hash('MD5', $iv, true);
  | 
        
        
            | 
            | 
           27 | 
                   } else {
  | 
        
        
            | 
            | 
           28 | 
                       //IV is not set. It doesn't recommend.
  | 
        
        
            | 
            | 
           29 | 
                       $this->iv = chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0);
  | 
        
        
            | 
            | 
           30 | 
                   }
  | 
        
        
            | 
            | 
           31 | 
               }
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
               function encrypt($str) {
  | 
        
        
            | 
            | 
           34 | 
                   if(version_compare(PHP_VERSION, '7.0.0', 'ge')) {
  | 
        
        
            | 
            | 
           35 | 
                       return $this->opensslEncrypt($str);
  | 
        
        
            | 
            | 
           36 | 
                   } else {
  | 
        
        
            | 
            | 
           37 | 
                       return $this->mcryptEncrypt($str);
  | 
        
        
            | 
            | 
           38 | 
                   }
  | 
        
        
            | 
            | 
           39 | 
               }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               function decrypt($str) {
  | 
        
        
            | 
            | 
           42 | 
                   if(version_compare(PHP_VERSION, '7.0.0', 'ge')) {
  | 
        
        
            | 
            | 
           43 | 
                       return $this->opensslDecrypt($str);
  | 
        
        
            | 
            | 
           44 | 
                   } else {
  | 
        
        
            | 
            | 
           45 | 
                       return $this->mcryptDecrypt($str);
  | 
        
        
            | 
            | 
           46 | 
                   }
  | 
        
        
            | 
            | 
           47 | 
               }
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               private function opensslEncrypt($str) {
  | 
        
        
            | 
            | 
           50 | 
                   $data = openssl_encrypt($str, $this->mode, $this->key, OPENSSL_RAW_DATA, $this->iv);
  | 
        
        
            | 
            | 
           51 | 
                   return base64_encode($data);
  | 
        
        
            | 
            | 
           52 | 
               }
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
               private function opensslDecrypt($str) {
  | 
        
        
            | 
            | 
           55 | 
                   $decrypted = openssl_decrypt(base64_decode($str), $this->mode, $this->key, OPENSSL_RAW_DATA, $this->iv);
  | 
        
        
            | 
            | 
           56 | 
                   return $decrypted;
  | 
        
        
            | 
            | 
           57 | 
               }
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
               private function mcryptEncrypt($str) {
  | 
        
        
            | 
            | 
           60 | 
                   $mcryptMode = MCRYPT_RIJNDAEL_128;
  | 
        
        
            | 
            | 
           61 | 
                   if($this->bit == 256){
  | 
        
        
            | 
            | 
           62 | 
                       $mcryptMode = MCRYPT_RIJNDAEL_256;
  | 
        
        
            | 
            | 
           63 | 
                   }
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   //Open
  | 
        
        
            | 
            | 
           66 | 
                   $module = mcrypt_module_open($mcryptMode, '', MCRYPT_MODE_CBC, '');
  | 
        
        
            | 
            | 
           67 | 
                   mcrypt_generic_init($module, $this->key, $this->iv);
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   //Padding
  | 
        
        
            | 
            | 
           70 | 
                   $block = mcrypt_get_block_size($mcryptMode, MCRYPT_MODE_CBC); //Get Block Size
  | 
        
        
            | 
            | 
           71 | 
                   $pad = $block - (strlen($str) % $block); //Compute how many characters need to pad
  | 
        
        
            | 
            | 
           72 | 
                   $str .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   //Encrypt
  | 
        
        
            | 
            | 
           75 | 
                   $encrypted = mcrypt_generic($module, $str);
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   //Close
  | 
        
        
            | 
            | 
           78 | 
                   mcrypt_generic_deinit($module);
  | 
        
        
            | 
            | 
           79 | 
                   mcrypt_module_close($module);
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
                   //Return
  | 
        
        
            | 
            | 
           82 | 
                   return base64_encode($encrypted);
  | 
        
        
            | 
            | 
           83 | 
               }
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
               private function mcryptDecrypt($str) {
  | 
        
        
            | 
            | 
           86 | 
                   $mcryptMode = MCRYPT_RIJNDAEL_128;
  | 
        
        
            | 
            | 
           87 | 
                   if($this->bit == 256){
  | 
        
        
            | 
            | 
           88 | 
                       $mcryptMode = MCRYPT_RIJNDAEL_256;
  | 
        
        
            | 
            | 
           89 | 
                   }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   //Open
  | 
        
        
            | 
            | 
           92 | 
                   $module = mcrypt_module_open($mcryptMode, '', MCRYPT_MODE_CBC, '');
  | 
        
        
            | 
            | 
           93 | 
                   mcrypt_generic_init($module, $this->key, $this->iv);
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   //Decrypt
  | 
        
        
            | 
            | 
           96 | 
                   $str = mdecrypt_generic($module, base64_decode($str)); //Get original str
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   //Close
  | 
        
        
            | 
            | 
           99 | 
                   mcrypt_generic_deinit($module);
  | 
        
        
            | 
            | 
           100 | 
                   mcrypt_module_close($module);
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   //Depadding
  | 
        
        
            | 
            | 
           103 | 
                   $slast = ord(substr($str, -1)); //pad value and pad count
  | 
        
        
            | 
            | 
           104 | 
                   $str = substr($str, 0, strlen($str) - $slast);
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   //Return
  | 
        
        
            | 
            | 
           107 | 
                   return $str;
  | 
        
        
            | 
            | 
           108 | 
               }
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
           | 223 | 
           efrain | 
           110 | 
               /*
  | 
        
        
           | 222 | 
           efrain | 
           111 | 
               static function encryptString($content) {
  | 
        
        
            | 
            | 
           112 | 
                   //Set password and iv string here
  | 
        
        
            | 
            | 
           113 | 
                   $aes = new EasyAES('****************', 128, '################');
  | 
        
        
            | 
            | 
           114 | 
                   return $aes->encrypt($content);
  | 
        
        
            | 
            | 
           115 | 
               }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
               static function decryptString($content) {
  | 
        
        
            | 
            | 
           118 | 
                   //Set password and iv string here
  | 
        
        
            | 
            | 
           119 | 
                   $aes = new  EasyAES('****************', 128, '################');
  | 
        
        
            | 
            | 
           120 | 
                   return $aes->decrypt($content);
  | 
        
        
           | 223 | 
           efrain | 
           121 | 
               }*/
  | 
        
        
           | 222 | 
           efrain | 
           122 | 
           }
  |