Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  /**
   3   * Copyright 2005-2008 Matthew Fonda <mfonda@php.net>
   4   * Copyright 2008 Philippe Jausions <jausions@php.net>
   5   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file LICENSE for license information (LGPL). If you
   8   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   9   *
  10   * @author   Matthew Fonda <mfonda@php.net>
  11   * @author   Philippe Jausions <jausions@php.net>
  12   * @author   Michael Slusarz <slusarz@horde.org>
  13   * @category Horde
  14   * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
  15   * @package  Crypt_Blowfish
  16   */
  17  
  18  /**
  19   * PHP implementation of the Blowfish algorithm in CBC mode.
  20   *
  21   * @author    Matthew Fonda <mfonda@php.net>
  22   * @author    Philippe Jausions <jausions@php.net>
  23   * @author    Michael Slusarz <slusarz@horde.org>
  24   * @category  Horde
  25   * @copyright 2005-2008 Matthew Fonda
  26   * @copyright 2008 Philippe Jausions
  27   * @copyright 2012-2017 Horde LLC
  28   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  29   * @package   Crypt_Blowfish
  30   */
  31  class Horde_Crypt_Blowfish_Php_Cbc extends Horde_Crypt_Blowfish_Php_Base
  32  {
  33      /**
  34       */
  35      public function encrypt($text, $iv)
  36      {
  37          $cipherText = '';
  38          $len = strlen($text);
  39  
  40          list(, $Xl, $Xr) = unpack('N2', substr($text, 0, 8) ^ $iv);
  41          $this->_encipher($Xl, $Xr);
  42          $cipherText .= pack('N2', $Xl, $Xr);
  43  
  44          for ($i = 8; $i < $len; $i += 8) {
  45              list(, $Xl, $Xr) = unpack('N2', substr($text, $i, 8) ^ substr($cipherText, $i - 8, 8));
  46              $this->_encipher($Xl, $Xr);
  47              $cipherText .= pack('N2', $Xl, $Xr);
  48          }
  49  
  50          return $cipherText;
  51      }
  52  
  53      /**
  54       */
  55      public function decrypt($text, $iv)
  56      {
  57          $plainText = '';
  58          $len = strlen($text);
  59  
  60          list(, $Xl, $Xr) = unpack('N2', substr($text, 0, 8));
  61          $this->_decipher($Xl, $Xr);
  62          $plainText .= (pack('N2', $Xl, $Xr) ^ $iv);
  63  
  64          for ($i = 8; $i < $len; $i += 8) {
  65              list(, $Xl, $Xr) = unpack('N2', substr($text, $i, 8));
  66              $this->_decipher($Xl, $Xr);
  67              $plainText .= (pack('N2', $Xl, $Xr) ^ substr($text, $i - 8, 8));
  68          }
  69  
  70          return $plainText;
  71      }
  72  
  73  }