src/Services/Encryptage/AesEncrytageStrategy.php line 75

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Mehrez Labidi
  5.  */
  6. namespace App\Services\Encryptage;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  8. class AesEncrytageStrategy
  9. {
  10.     /**
  11.      * @var ContainerBagInterface
  12.      */
  13.     private $params;
  14.     /**
  15.      * @param ContainerBagInterface $params
  16.      */
  17.     public function __construct(ContainerBagInterface $params)
  18.     {
  19.         $this->params $params;
  20.     }
  21.     /**
  22.      * @param $toEncrypt
  23.      * @return string
  24.      */
  25.     public function encryptage($toEncrypt)
  26.     {
  27.         if( $this->checkIfAlreadyCrypted($toEncrypt) ){ // deja crypté
  28.             return $toEncrypt;
  29.         }
  30.         $keyEncrypt $this->params->get('encrypt.key');
  31.         $algoEncrypt $this->params->get('encrypt.algo');
  32.         $func $algoEncrypt;
  33.         $key $func($keyEncrypt);
  34.         $key $this->mysqlAesKey($key);
  35.         $padValue 16 - (strlen($toEncrypt) % 16);
  36.         $param1 =  16 * (floor(strlen($toEncrypt) / 16) + 1);
  37.         $param1  = (int)$param1;
  38.         $toEncrypt str_pad($toEncrypt$param1,chr($padValue));
  39.         
  40.         return strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128$key$toEncryptMCRYPT_MODE_ECBmcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM))));
  41.     }
  42.     /**
  43.      * @param $key
  44.      * @return string
  45.      */
  46.     private function mysqlAesKey($key)
  47.     {
  48.         $new_key str_repeat(chr(0), 16);
  49.         for ($i 0$len strlen($key); $i $len; ++$i) {
  50.             $new_key[$i 16] = $new_key[$i 16] ^ $key[$i];
  51.         }
  52.         return $new_key;
  53.     }
  54.     /**
  55.      * @param $toDecrypt
  56.      * @return string
  57.      */
  58.     public function decryptage($toDecrypt)
  59.     {
  60.         if( !$this->checkIfAlreadyCrypted($toDecrypt)  ){ // deja non crypté
  61.             return $toDecrypt;
  62.         }
  63.         $keyEncrypt $this->params->get('encrypt.key');
  64.         $algoEncrypt $this->params->get('encrypt.algo');
  65.         $func $algoEncrypt;
  66.         $key $func($keyEncrypt);
  67.         $toDecrypt pack('H*'$toDecrypt);
  68.         $key $this->mysqlAesKey($key);
  69.         return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128$key$toDecryptMCRYPT_MODE_ECB''), "\x00..\x1F");
  70.     }
  71.     /**
  72.      * @param string|null $str
  73.      * @return bool
  74.      */
  75.     private function checkIfAlreadyCrypted( ?string $str )
  76.     {         // strlen >= 16 + at least one capital letter  + at least one number
  77.         if(  (strlen($str)>=16) && (preg_match('/^[A-Z0-9]+$/'$str))  ){
  78.             return true;
  79.         }
  80.         return false;
  81.     }
  82. }