Source for file aes.php

Documentation is available at aes.php

  1. <?php
  2. /**
  3.  * @package    FrameworkOnFramework
  4.  * @subpackage encrypt
  5.  * @copyright  Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license    GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. /**
  12.  * A simple implementation of AES-128, AES-192 and AES-256 encryption using the
  13.  * high performance mcrypt library.
  14.  *
  15.  * @package  FrameworkOnFramework
  16.  * @since    1.0
  17.  */
  18. {
  19.     /** @var string The AES cipher to use (this is an mcrypt identifier, not the bit strength) */
  20.     private $_cipherType 0;
  21.  
  22.     /** @var string Cipher mode. Can be CBC or ECB. We recommend using CBC */
  23.     private $_cipherMode 0;
  24.  
  25.     /** @var string The cipher key (password) */
  26.     private $_keyString '';
  27.  
  28.     /**
  29.      * Initialise the AES encryption object
  30.      *
  31.      * @param   string  $key       The encryption key (password). It can be a raw key (32 bytes) or a passphrase.
  32.      * @param   int     $strength  Bit strength (128, 192 or 256)
  33.      * @param   string  $mode      Ecnryption mode. Can be ebc or cbc. We recommend using cbc.
  34.      */
  35.     public function __construct($key$strength 256$mode 'cbc')
  36.     {
  37.         $this->_keyString $key;
  38.  
  39.         switch ($strength)
  40.         {
  41.             case 256:
  42.             default:
  43.                 $this->_cipherType MCRYPT_RIJNDAEL_256;
  44.                 break;
  45.  
  46.             case 192:
  47.                 $this->_cipherType MCRYPT_RIJNDAEL_192;
  48.                 break;
  49.  
  50.             case 128:
  51.                 $this->_cipherType MCRYPT_RIJNDAEL_128;
  52.                 break;
  53.         }
  54.  
  55.         switch (strtoupper($mode))
  56.         {
  57.             case 'ECB':
  58.                 $this->_cipherMode MCRYPT_MODE_ECB;
  59.                 break;
  60.  
  61.             case 'CBC':
  62.                 $this->_cipherMode MCRYPT_MODE_CBC;
  63.                 break;
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Encrypts a string using AES
  69.      *
  70.      * @param   string  $stringToEncrypt  The plaintext to encrypt
  71.      * @param   bool    $base64encoded    Should I Base64-encode the result?
  72.      *
  73.      * @return   string  The cryptotext. Please note that the first 16 bytes of
  74.      *                    the raw string is the IV (initialisation vector) which
  75.      *                    is necessary for decoding the string.
  76.      */
  77.     public function encryptString($stringToEncrypt$base64encoded true)
  78.     {
  79.         if (strlen($this->_keyString!= 32)
  80.         {
  81.             $key hash('sha256'$this->_keyStringtrue);
  82.         }
  83.         else
  84.         {
  85.             $key $this->_keyString;
  86.         }
  87.  
  88.         // Set up the IV (Initialization Vector)
  89.         $iv_size mcrypt_get_iv_size($this->_cipherType$this->_cipherMode);
  90.         $iv mcrypt_create_iv($iv_sizeMCRYPT_DEV_URANDOM);
  91.  
  92.         if (empty($iv))
  93.         {
  94.             $iv mcrypt_create_iv($iv_sizeMCRYPT_DEV_RANDOM);
  95.         }
  96.  
  97.         if (empty($iv))
  98.         {
  99.             $iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
  100.         }
  101.  
  102.         // Encrypt the data
  103.         $cipherText mcrypt_encrypt($this->_cipherType$key$stringToEncrypt$this->_cipherMode$iv);
  104.  
  105.         // Prepend the IV to the ciphertext
  106.         $cipherText $iv $cipherText;
  107.  
  108.         // Optionally pass the result through Base64 encoding
  109.         if ($base64encoded)
  110.         {
  111.             $cipherText base64_encode($cipherText);
  112.         }
  113.  
  114.         // Return the result
  115.         return $cipherText;
  116.     }
  117.  
  118.     /**
  119.      * Decrypts a ciphertext into a plaintext string using AES
  120.      *
  121.      * @param   string  $stringToDecrypt  The ciphertext to decrypt. The first 16 bytes of the raw string must contain the IV (initialisation vector).
  122.      * @param   bool    $base64encoded    Should I Base64-decode the data before decryption?
  123.      *
  124.      * @return   string  The plain text string
  125.      */
  126.     public function decryptString($stringToDecrypt$base64encoded true)
  127.     {
  128.         if (strlen($this->_keyString!= 32)
  129.         {
  130.             $key hash('sha256'$this->_keyStringtrue);
  131.         }
  132.         else
  133.         {
  134.             $key $this->_keyString;
  135.         }
  136.  
  137.         if ($base64encoded)
  138.         {
  139.             $stringToDecrypt base64_decode($stringToDecrypt);
  140.         }
  141.  
  142.         // Calculate the IV size
  143.         $iv_size mcrypt_get_iv_size($this->_cipherType$this->_cipherMode);
  144.  
  145.         // Extract IV
  146.         $iv substr($stringToDecrypt0$iv_size);
  147.         $stringToDecrypt substr($stringToDecrypt$iv_size);
  148.  
  149.         // Decrypt the data
  150.         $plainText mcrypt_decrypt($this->_cipherType$key$stringToDecrypt$this->_cipherMode$iv);
  151.  
  152.         return $plainText;
  153.     }
  154.  
  155.     /**
  156.      * Is AES encryption supported by this PHP installation?
  157.      *
  158.      * @return boolean 
  159.      */
  160.     public static function isSupported()
  161.     {
  162.         if (!function_exists('mcrypt_get_key_size'))
  163.         {
  164.             return false;
  165.         }
  166.  
  167.         if (!function_exists('mcrypt_get_iv_size'))
  168.         {
  169.             return false;
  170.         }
  171.  
  172.         if (!function_exists('mcrypt_create_iv'))
  173.         {
  174.             return false;
  175.         }
  176.  
  177.         if (!function_exists('mcrypt_encrypt'))
  178.         {
  179.             return false;
  180.         }
  181.  
  182.         if (!function_exists('mcrypt_decrypt'))
  183.         {
  184.             return false;
  185.         }
  186.  
  187.         if (!function_exists('mcrypt_list_algorithms'))
  188.         {
  189.             return false;
  190.         }
  191.  
  192.         if (!function_exists('hash'))
  193.         {
  194.             return false;
  195.         }
  196.  
  197.         if (!function_exists('hash_algos'))
  198.         {
  199.             return false;
  200.         }
  201.  
  202.         if (!function_exists('base64_encode'))
  203.         {
  204.             return false;
  205.         }
  206.  
  207.         if (!function_exists('base64_decode'))
  208.         {
  209.             return false;
  210.         }
  211.  
  212.         $algorightms mcrypt_list_algorithms();
  213.  
  214.         if (!in_array('rijndael-128'$algorightms))
  215.         {
  216.             return false;
  217.         }
  218.  
  219.         if (!in_array('rijndael-192'$algorightms))
  220.         {
  221.             return false;
  222.         }
  223.  
  224.         if (!in_array('rijndael-256'$algorightms))
  225.         {
  226.             return false;
  227.         }
  228.  
  229.         $algorightms hash_algos();
  230.  
  231.         if (!in_array('sha256'$algorightms))
  232.         {
  233.             return false;
  234.         }
  235.  
  236.         return true;
  237.     }
  238. }

Documentation generated on Tue, 19 Nov 2013 14:53:38 +0100 by phpDocumentor 1.4.3