Crypt
[ class tree: Crypt ] [ index: Crypt ] [ all elements ]

Source for file CBC.php

Documentation is available at CBC.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2008 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Colin Viebrock <colin@viebrock.ca>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: CBC.php,v 1.7 2008/10/01 21:23:11 cmv Exp $
  20. //
  21.  
  22. require_once 'PEAR.php';
  23.  
  24.  
  25. /**
  26.  * Class to emulate Perl's Crypt::CBC module
  27.  *
  28.  * Blowfish support that is compatable with Perl requires libmcrypt >= 2.4.9.
  29.  * If you are using libmcrypt <= 2.4.8, Blowfish encryption will work,
  30.  * but your data will not be readable by Perl scripts.  It will work
  31.  * "internally" .. i.e. this class will be able to encode/decode the data.
  32.  *
  33.  * Blowfish support that is compatable with PHP applications using
  34.  * libmcrypt <= 2.4.8 requies you to use 'BLOWFISH-COMPAT' when
  35.  * specifying the cipher.  Check the libmcrypt docs when in doubt.
  36.  *
  37.  * This class no longer works with libmcrypt 2.2.x versions.
  38.  *
  39.  * NOTE: the cipher names in this class may change depending on how
  40.  * the author of libcrypt decides to name things internally.
  41.  *
  42.  *
  43.  * @version  $Revision: 1.7 $
  44.  * @author   Colin Viebrock <colin@easydns.com>
  45.  * @access   public
  46.  * @package  Crypt
  47.  */
  48.  
  49. class Crypt_CBC extends PEAR {
  50.  
  51.     /**
  52.     * supported procedures
  53.     * @var array 
  54.     */
  55.     var $known_ciphers = array (
  56.         'DES'               => MCRYPT_DES,
  57.         'BLOWFISH'          => MCRYPT_BLOWFISH,
  58.         'BLOWFISH-COMPAT'   => MCRYPT_BLOWFISH_COMPAT,
  59.     );
  60.  
  61.     /**
  62.     * used cipher
  63.     * @var string 
  64.     */
  65.     var $cipher;
  66.  
  67.     /**
  68.     * crypt resource, for 2.4.x
  69.     * @var string 
  70.     */  
  71.     var $TD;
  72.  
  73.     /**
  74.     * crypt deinit function, for backwards compatability
  75.     * @var string 
  76.     */  
  77.     var $deinit_function;
  78.  
  79.     /**
  80.     * blocksize of cipher
  81.     * @var string 
  82.     */    
  83.     var $blocksize;
  84.  
  85.     /**
  86.     * keysize of cipher
  87.     * @var int 
  88.     */    
  89.     var $keysize;
  90.  
  91.     /**
  92.     * mangled key
  93.     * @var string 
  94.     */        
  95.     var $keyhash;
  96.  
  97.     /**
  98.     * source type of the initialization vector for creation
  99.     * possible types are MCRYPT_RAND or MCRYPT_DEV_URANDOM or MCRYPT_DEV_RANDOM
  100.     * @var int 
  101.     */            
  102.     var $rand_source    = MCRYPT_RAND;
  103.  
  104.     /**
  105.     * header
  106.     * @var string 
  107.     */           
  108.     var $header_spec    = 'RandomIV';
  109.  
  110.     /**
  111.     * debugging
  112.     * @var string 
  113.     */           
  114.     var $_last_clear;
  115.  
  116.     /**
  117.     * debugging
  118.     * @var string 
  119.     */              
  120.     var $_last_crypt;
  121.  
  122.     /**
  123.     * Constructor
  124.     * $key is the key to use for encryption. $cipher can be DES, BLOWFISH or
  125.     * BLOWFISH-COMPAT
  126.     *
  127.     * @param    $key        encryption key
  128.     * @param    $cipher     which algorithm to use, defaults to DES
  129.     *
  130.     * @return   $return     either a PEAR error or true
  131.     *
  132.     * @access   public
  133.     *
  134.     */
  135.  
  136.     function Crypt_CBC ($key$cipher='DES')
  137.     {
  138.  
  139.         if (!extension_loaded('mcrypt')) {
  140.             return $this->raiseError('mcrypt module is not compiled into PHP'null
  141.                 PEAR_ERROR_DIEnull'compile PHP using "--with-mcrypt"' );
  142.         }
  143.         if (!function_exists('mcrypt_module_open')) {
  144.             return $this->raiseError('libmcrypt version insufficient'null
  145.                 PEAR_ERROR_DIEnull'this class requires libmcrypt >= 2.4.x, preferably >= 2.5.5' );
  146.         }
  147.         if (function_exists('mcrypt_generic_deinit')) {
  148.             $this->deinit_function = 'mcrypt_generic_deinit';
  149.         else if (function_exists('mcrypt_generic_end')) {
  150.             $this->deinit_function = 'mcrypt_generic_end';
  151.         else {
  152.             return $this->raiseError('PHP version insufficient'null
  153.                 PEAR_ERROR_DIEnull'this class requires PHP >= 4.0.2, preferably >= 4.1.1' );
  154.         }
  155.  
  156.  
  157.         /* initialize */
  158.  
  159.         $this->header_spec = 'RandomIV';
  160.  
  161.         /* check for key */
  162.  
  163.         if (!$key{
  164.             return $this->raiseError('no key specified');
  165.         }
  166.  
  167.         /* check for cipher */
  168.  
  169.         $cipher strtoupper($cipher);
  170.         if (!isset($this->known_ciphers[$cipher])) {
  171.             return $this->raiseError('unknown cipher "'.$cipher.'"' );
  172.         }
  173.  
  174.         $this->cipher = $this->known_ciphers[$cipher];
  175.  
  176.         /* initialize cipher */
  177.  
  178.         $this->TD = mcrypt_module_open ($this->cipher'''ecb''');
  179.         $this->blocksize = mcrypt_enc_get_block_size($this->TD);
  180.         $this->keysize = mcrypt_enc_get_key_size($this->TD);
  181.  
  182.         /* mangle key with MD5 */
  183.  
  184.         $this->keyhash = $this->_md5perl($key);
  185.         whilestrlen($this->keyhash$this->keysize {
  186.             $this->keyhash .= $this->_md5perl($this->keyhash);
  187.         }
  188.  
  189.         $this->key substr($this->keyhash0$this->keysize);
  190.  
  191.         return true;
  192.  
  193.     }
  194.  
  195.  
  196.     /**
  197.     * Destructor
  198.     *
  199.     */
  200.  
  201.     function _Crypt_CBC ()
  202.     {
  203.         @mcrypt_module_close($this->TD);
  204.     }
  205.  
  206.  
  207.     /**
  208.     * Encryption method
  209.     *
  210.     * @param    $clear      plaintext
  211.     *
  212.     * @return   $crypt      encrypted text, or PEAR error
  213.     *
  214.     * @access   public
  215.     *
  216.     */
  217.  
  218.     function encrypt($clear)
  219.     {
  220.  
  221.         $this->last_clear $clear;
  222.  
  223.         /* new IV for each message */
  224.  
  225.         $iv = mcrypt_create_iv($this->blocksize$this->rand_source);
  226.  
  227.         /* create the message header */
  228.  
  229.         $crypt $this->header_spec . $iv;
  230.  
  231.         /* pad the cleartext */
  232.  
  233.         $padsize $this->blocksize - (strlen($clear$this->blocksize);
  234.         $clear .= str_repeat(pack ('C*'$padsize)$padsize);
  235.  
  236.  
  237.         /* do the encryption */
  238.  
  239.         $start = 0;
  240.         while $block substr($clear$start$this->blocksize) ) {
  241.             $start += $this->blocksize;
  242.             if (mcrypt_generic_init($this->TD$this->key$iv< 0 {
  243.                 return $this->raiseError('mcrypt_generic_init failed' );
  244.             }
  245.             $cblock = mcrypt_generic($this->TD$iv^$block );
  246.             $iv $cblock;
  247.             $crypt .= $cblock;
  248.             call_user_func($this->deinit_function$this->TD);
  249.         }
  250.  
  251.         $this->last_crypt $crypt;
  252.         return $crypt;
  253.  
  254.     }
  255.  
  256.  
  257.  
  258.     /**
  259.     * Decryption method
  260.     *
  261.     * @param    $crypt      encrypted text
  262.     *
  263.     * @return   $clear      plaintext, or PEAR error
  264.     *
  265.     * @access   public
  266.     *
  267.     */
  268.  
  269.     function decrypt($crypt{
  270.  
  271.         $this->last_crypt $crypt;
  272.  
  273.         /* get the IV from the message header */
  274.  
  275.         $iv_offset strlen($this->header_spec);
  276.         $header substr($crypt0$iv_offset);
  277.         $iv substr ($crypt$iv_offset$this->blocksize);
  278.         if $header != $this->header_spec {
  279.             return $this->raiseError('no initialization vector');
  280.         }
  281.  
  282.         $crypt substr($crypt$iv_offset+$this->blocksize);
  283.  
  284.         /* decrypt the message */
  285.  
  286.         $start = 0;
  287.         $clear '';
  288.  
  289.         while $cblock substr($crypt$start$this->blocksize) ) {
  290.             $start += $this->blocksize;
  291.             if (mcrypt_generic_init($this->TD$this->key$iv< 0 {
  292.                 return $this->raiseError('mcrypt_generic_init failed' );
  293.             }
  294.             $block $iv ^ mdecrypt_generic($this->TD$cblock);
  295.             $iv $cblock;
  296.             $clear .= $block;
  297.             call_user_func($this->deinit_function$this->TD);
  298.         }
  299.  
  300.         /* remove the padding from the end of the cleartext */
  301.  
  302.         $padsize ord(substr($clear-1));
  303.         $clear substr($clear0-$padsize);
  304.  
  305.         $this->last_clear $clear;
  306.         return $clear;
  307.  
  308.     }
  309.  
  310.  
  311.  
  312.     /**
  313.     * Emulate Perl's MD5 function, which returns binary data
  314.     *
  315.     * @param    $string     string to MD5
  316.     *
  317.     * @return   $hash       binary hash
  318.     *
  319.     * @access private
  320.     *
  321.     */
  322.  
  323.     function _md5perl($string)
  324.     {
  325.         return pack('H*'md5($string));
  326.     }
  327. }
  328. ?>

Documentation generated on Mon, 11 Mar 2019 15:25:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.