[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Encrypt/AES/ -> Mcrypt.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Encrypt\AES;
  11  
  12  use Joomla\CMS\Encrypt\Randval;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Mcrypt implementation
  20   *
  21   * @since    4.0.0
  22   *
  23   * @deprecated 4.0.0 will be removed in 5.0.0
  24   */
  25  class Mcrypt extends AbstractAES implements AesInterface
  26  {
  27      /**
  28       * Cypher Type
  29       *
  30       * @var    string
  31       */
  32      protected $cipherType = MCRYPT_RIJNDAEL_128;
  33  
  34      /**
  35       * Cypher Mode
  36       *
  37       * @var    string
  38       */
  39      protected $cipherMode = MCRYPT_MODE_CBC;
  40  
  41      /**
  42       * Set the encryption mode
  43       *
  44       * @param   string   $mode      Encryption Mode
  45       * @param   integer  $strength  Encryption Strength
  46       *
  47       * @return   void
  48       */
  49      public function setEncryptionMode($mode = 'cbc', $strength = 128)
  50      {
  51          switch ((int) $strength) {
  52              default:
  53              case '128':
  54                  $this->cipherType = MCRYPT_RIJNDAEL_128;
  55                  break;
  56  
  57              case '192':
  58                  $this->cipherType = MCRYPT_RIJNDAEL_192;
  59                  break;
  60  
  61              case '256':
  62                  $this->cipherType = MCRYPT_RIJNDAEL_256;
  63                  break;
  64          }
  65  
  66          switch (strtolower($mode)) {
  67              case 'ecb':
  68                  $this->cipherMode = MCRYPT_MODE_ECB;
  69                  break;
  70  
  71              default:
  72              case 'cbc':
  73                  $this->cipherMode = MCRYPT_MODE_CBC;
  74                  break;
  75          }
  76      }
  77  
  78      /**
  79       * Encrypt the data
  80       *
  81       * @param   string  $plainText  Plaintext data
  82       * @param   string  $key        Encryption key
  83       * @param   string  $iv         IV for the encryption
  84       *
  85       * @return   string  Encrypted data
  86       */
  87      public function encrypt($plainText, $key, $iv = null)
  88      {
  89          $iv_size = $this->getBlockSize();
  90          $key     = $this->resizeKey($key, $iv_size);
  91          $iv      = $this->resizeKey($iv, $iv_size);
  92  
  93          if (empty($iv)) {
  94              $randVal   = new Randval();
  95              $iv        = $randVal->generate($iv_size);
  96          }
  97  
  98          $cipherText = mcrypt_encrypt($this->cipherType, $key, $plainText, $this->cipherMode, $iv);
  99          $cipherText = $iv . $cipherText;
 100  
 101          return $cipherText;
 102      }
 103  
 104      /**
 105       * Decrypt encrypted data
 106       *
 107       * @param   string  $cipherText  Encrypted data
 108       * @param   string  $key         Encryptionkey
 109       *
 110       * @return   string  Plaintext data
 111       */
 112      public function decrypt($cipherText, $key)
 113      {
 114          $iv_size    = $this->getBlockSize();
 115          $key        = $this->resizeKey($key, $iv_size);
 116          $iv         = substr($cipherText, 0, $iv_size);
 117          $cipherText = substr($cipherText, $iv_size);
 118          $plainText  = mcrypt_decrypt($this->cipherType, $key, $cipherText, $this->cipherMode, $iv);
 119  
 120          return $plainText;
 121      }
 122  
 123      /**
 124       * Is this adapter supported?
 125       *
 126       * @return  boolean
 127       */
 128      public function isSupported()
 129      {
 130          if (!\function_exists('mcrypt_get_key_size')) {
 131              return false;
 132          }
 133  
 134          if (!\function_exists('mcrypt_get_iv_size')) {
 135              return false;
 136          }
 137  
 138          if (!\function_exists('mcrypt_create_iv')) {
 139              return false;
 140          }
 141  
 142          if (!\function_exists('mcrypt_encrypt')) {
 143              return false;
 144          }
 145  
 146          if (!\function_exists('mcrypt_decrypt')) {
 147              return false;
 148          }
 149  
 150          if (!\function_exists('mcrypt_list_algorithms')) {
 151              return false;
 152          }
 153  
 154          if (!\function_exists('hash')) {
 155              return false;
 156          }
 157  
 158          if (!\function_exists('hash_algos')) {
 159              return false;
 160          }
 161  
 162          $algorigthms = mcrypt_list_algorithms();
 163  
 164          if (!\in_array('rijndael-128', $algorigthms)) {
 165              return false;
 166          }
 167  
 168          if (!\in_array('rijndael-192', $algorigthms)) {
 169              return false;
 170          }
 171  
 172          if (!\in_array('rijndael-256', $algorigthms)) {
 173              return false;
 174          }
 175  
 176          $algorigthms = hash_algos();
 177  
 178          if (!\in_array('sha256', $algorigthms)) {
 179              return false;
 180          }
 181  
 182          return true;
 183      }
 184  
 185      /**
 186       * Get the block size
 187       *
 188       * @return   integer
 189       */
 190      public function getBlockSize()
 191      {
 192          return mcrypt_get_iv_size($this->cipherType, $this->cipherMode);
 193      }
 194  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer