]> git.mxchange.org Git - friendica.git/blobdiff - library/phpsec/Crypt/DES.php
Issue 3142: mcrypt is no more (as well as phpseclib)
[friendica.git] / library / phpsec / Crypt / DES.php
diff --git a/library/phpsec/Crypt/DES.php b/library/phpsec/Crypt/DES.php
deleted file mode 100644 (file)
index 431ad53..0000000
+++ /dev/null
@@ -1,945 +0,0 @@
-<?php\r
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
-\r
-/**\r
- * Pure-PHP implementation of DES.\r
- *\r
- * Uses mcrypt, if available, and an internal implementation, otherwise.\r
- *\r
- * PHP versions 4 and 5\r
- *\r
- * Useful resources are as follows:\r
- *\r
- *  - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}\r
- *  - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}\r
- *  - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}\r
- *\r
- * Here's a short example of how to use this library:\r
- * <code>\r
- * <?php\r
- *    include('Crypt/DES.php');\r
- *\r
- *    $des = new Crypt_DES();\r
- *\r
- *    $des->setKey('abcdefgh');\r
- *\r
- *    $size = 10 * 1024;\r
- *    $plaintext = '';\r
- *    for ($i = 0; $i < $size; $i++) {\r
- *        $plaintext.= 'a';\r
- *    }\r
- *\r
- *    echo $des->decrypt($des->encrypt($plaintext));\r
- * ?>\r
- * </code>\r
- *\r
- * LICENSE: This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Lesser General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2.1 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Lesser General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License along with this library; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
- * MA  02111-1307  USA\r
- *\r
- * @category   Crypt\r
- * @package    Crypt_DES\r
- * @author     Jim Wigginton <terrafrost@php.net>\r
- * @copyright  MMVII Jim Wigginton\r
- * @license    http://www.gnu.org/licenses/lgpl.txt\r
- * @version    $Id: DES.php,v 1.12 2010/02/09 06:10:26 terrafrost Exp $\r
- * @link       http://phpseclib.sourceforge.net\r
- */\r
-\r
-/**#@+\r
- * @access private\r
- * @see Crypt_DES::_prepareKey()\r
- * @see Crypt_DES::_processBlock()\r
- */\r
-/**\r
- * Contains array_reverse($keys[CRYPT_DES_DECRYPT])\r
- */\r
-define('CRYPT_DES_ENCRYPT', 0);\r
-/**\r
- * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])\r
- */\r
-define('CRYPT_DES_DECRYPT', 1);\r
-/**#@-*/\r
-\r
-/**#@+\r
- * @access public\r
- * @see Crypt_DES::encrypt()\r
- * @see Crypt_DES::decrypt()\r
- */\r
-/**\r
- * Encrypt / decrypt using the Counter mode.\r
- *\r
- * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.\r
- *\r
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29\r
- */\r
-define('CRYPT_DES_MODE_CTR', -1);\r
-/**\r
- * Encrypt / decrypt using the Electronic Code Book mode.\r
- *\r
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29\r
- */\r
-define('CRYPT_DES_MODE_ECB', 1);\r
-/**\r
- * Encrypt / decrypt using the Code Book Chaining mode.\r
- *\r
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29\r
- */\r
-define('CRYPT_DES_MODE_CBC', 2);\r
-/**#@-*/\r
-\r
-/**#@+\r
- * @access private\r
- * @see Crypt_DES::Crypt_DES()\r
- */\r
-/**\r
- * Toggles the internal implementation\r
- */\r
-define('CRYPT_DES_MODE_INTERNAL', 1);\r
-/**\r
- * Toggles the mcrypt implementation\r
- */\r
-define('CRYPT_DES_MODE_MCRYPT', 2);\r
-/**#@-*/\r
-\r
-/**\r
- * Pure-PHP implementation of DES.\r
- *\r
- * @author  Jim Wigginton <terrafrost@php.net>\r
- * @version 0.1.0\r
- * @access  public\r
- * @package Crypt_DES\r
- */\r
-class Crypt_DES {\r
-    /**\r
-     * The Key Schedule\r
-     *\r
-     * @see Crypt_DES::setKey()\r
-     * @var Array\r
-     * @access private\r
-     */\r
-    var $keys = "\0\0\0\0\0\0\0\0";\r
-\r
-    /**\r
-     * The Encryption Mode\r
-     *\r
-     * @see Crypt_DES::Crypt_DES()\r
-     * @var Integer\r
-     * @access private\r
-     */\r
-    var $mode;\r
-\r
-    /**\r
-     * Continuous Buffer status\r
-     *\r
-     * @see Crypt_DES::enableContinuousBuffer()\r
-     * @var Boolean\r
-     * @access private\r
-     */\r
-    var $continuousBuffer = false;\r
-\r
-    /**\r
-     * Padding status\r
-     *\r
-     * @see Crypt_DES::enablePadding()\r
-     * @var Boolean\r
-     * @access private\r
-     */\r
-    var $padding = true;\r
-\r
-    /**\r
-     * The Initialization Vector\r
-     *\r
-     * @see Crypt_DES::setIV()\r
-     * @var String\r
-     * @access private\r
-     */\r
-    var $iv = "\0\0\0\0\0\0\0\0";\r
-\r
-    /**\r
-     * A "sliding" Initialization Vector\r
-     *\r
-     * @see Crypt_DES::enableContinuousBuffer()\r
-     * @var String\r
-     * @access private\r
-     */\r
-    var $encryptIV = "\0\0\0\0\0\0\0\0";\r
-\r
-    /**\r
-     * A "sliding" Initialization Vector\r
-     *\r
-     * @see Crypt_DES::enableContinuousBuffer()\r
-     * @var String\r
-     * @access private\r
-     */\r
-    var $decryptIV = "\0\0\0\0\0\0\0\0";\r
-\r
-    /**\r
-     * mcrypt resource for encryption\r
-     *\r
-     * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
-     * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
-     *\r
-     * @see Crypt_AES::encrypt()\r
-     * @var String\r
-     * @access private\r
-     */\r
-    var $enmcrypt;\r
-\r
-    /**\r
-     * mcrypt resource for decryption\r
-     *\r
-     * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
-     * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
-     *\r
-     * @see Crypt_AES::decrypt()\r
-     * @var String\r
-     * @access private\r
-     */\r
-    var $demcrypt;\r
-\r
-    /**\r
-     * Does the (en|de)mcrypt resource need to be (re)initialized?\r
-     *\r
-     * @see setKey()\r
-     * @see setIV()\r
-     * @var Boolean\r
-     * @access private\r
-     */\r
-    var $changed = true;\r
-\r
-    /**\r
-     * Default Constructor.\r
-     *\r
-     * Determines whether or not the mcrypt extension should be used.  $mode should only, at present, be\r
-     * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC.  If not explictly set, CRYPT_DES_MODE_CBC will be used.\r
-     *\r
-     * @param optional Integer $mode\r
-     * @return Crypt_DES\r
-     * @access public\r
-     */\r
-    function Crypt_DES($mode = CRYPT_MODE_DES_CBC)\r
-    {\r
-        if ( !defined('CRYPT_DES_MODE') ) {\r
-            switch (true) {\r
-                case extension_loaded('mcrypt'):\r
-                    // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),\r
-                    // but since that can be changed after the object has been created, there doesn't seem to be\r
-                    // a lot of point...\r
-                    define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);\r
-                    break;\r
-                default:\r
-                    define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);\r
-            }\r
-        }\r
-\r
-        switch ( CRYPT_DES_MODE ) {\r
-            case CRYPT_DES_MODE_MCRYPT:\r
-                switch ($mode) {\r
-                    case CRYPT_DES_MODE_ECB:\r
-                        $this->mode = MCRYPT_MODE_ECB;\r
-                        break;\r
-                    case CRYPT_DES_MODE_CTR:\r
-                        $this->mode = 'ctr';\r
-                        //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;\r
-                        break;\r
-                    case CRYPT_DES_MODE_CBC:\r
-                    default:\r
-                        $this->mode = MCRYPT_MODE_CBC;\r
-                }\r
-\r
-                break;\r
-            default:\r
-                switch ($mode) {\r
-                    case CRYPT_DES_MODE_ECB:\r
-                    case CRYPT_DES_MODE_CTR:\r
-                    case CRYPT_DES_MODE_CBC:\r
-                        $this->mode = $mode;\r
-                        break;\r
-                    default:\r
-                        $this->mode = CRYPT_DES_MODE_CBC;\r
-                }\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sets the key.\r
-     *\r
-     * Keys can be of any length.  DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we\r
-     * only use the first eight, if $key has more then eight characters in it, and pad $key with the\r
-     * null byte if it is less then eight characters long.\r
-     *\r
-     * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.\r
-     *\r
-     * If the key is not explicitly set, it'll be assumed to be all zero's.\r
-     *\r
-     * @access public\r
-     * @param String $key\r
-     */\r
-    function setKey($key)\r
-    {\r
-        $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? substr($key, 0, 8) : $this->_prepareKey($key);\r
-        $this->changed = true;\r
-    }\r
-\r
-    /**\r
-     * Sets the initialization vector. (optional)\r
-     *\r
-     * SetIV is not required when CRYPT_DES_MODE_ECB is being used.  If not explictly set, it'll be assumed\r
-     * to be all zero's.\r
-     *\r
-     * @access public\r
-     * @param String $iv\r
-     */\r
-    function setIV($iv)\r
-    {\r
-        $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));\r
-        $this->changed = true;\r
-    }\r
-\r
-    /**\r
-     * Generate CTR XOR encryption key\r
-     *\r
-     * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the\r
-     * plaintext / ciphertext in CTR mode.\r
-     *\r
-     * @see Crypt_DES::decrypt()\r
-     * @see Crypt_DES::encrypt()\r
-     * @access public\r
-     * @param Integer $length\r
-     * @param String $iv\r
-     */\r
-    function _generate_xor($length, &$iv)\r
-    {\r
-        $xor = '';\r
-        $num_blocks = ($length + 7) >> 3;\r
-        for ($i = 0; $i < $num_blocks; $i++) {\r
-            $xor.= $iv;\r
-            for ($j = 4; $j <= 8; $j+=4) {\r
-                $temp = substr($iv, -$j, 4);\r
-                switch ($temp) {\r
-                    case "\xFF\xFF\xFF\xFF":\r
-                        $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);\r
-                        break;\r
-                    case "\x7F\xFF\xFF\xFF":\r
-                        $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);\r
-                        break 2;\r
-                    default:\r
-                        extract(unpack('Ncount', $temp));\r
-                        $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);\r
-                        break 2;\r
-                }\r
-            }\r
-        }\r
-\r
-        return $xor;\r
-    }\r
-\r
-    /**\r
-     * Encrypts a message.\r
-     *\r
-     * $plaintext will be padded with up to 8 additional bytes.  Other DES implementations may or may not pad in the\r
-     * same manner.  Other common approaches to padding and the reasons why it's necessary are discussed in the following\r
-     * URL:\r
-     *\r
-     * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}\r
-     *\r
-     * An alternative to padding is to, separately, send the length of the file.  This is what SSH, in fact, does.\r
-     * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that\r
-     * length.\r
-     *\r
-     * @see Crypt_DES::decrypt()\r
-     * @access public\r
-     * @param String $plaintext\r
-     */\r
-    function encrypt($plaintext)\r
-    {\r
-        if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {\r
-            $plaintext = $this->_pad($plaintext);\r
-        }\r
-\r
-        if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {\r
-            if ($this->changed) {\r
-                if (!isset($this->enmcrypt)) {\r
-                    $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');\r
-                }\r
-                mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);\r
-                $this->changed = false;\r
-            }\r
-\r
-            $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);\r
-\r
-            if (!$this->continuousBuffer) {\r
-                mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);\r
-            }\r
-\r
-            return $ciphertext;\r
-        }\r
-\r
-        if (!is_array($this->keys)) {\r
-            $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");\r
-        }\r
-\r
-        $ciphertext = '';\r
-        switch ($this->mode) {\r
-            case CRYPT_DES_MODE_ECB:\r
-                for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
-                    $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);\r
-                }\r
-                break;\r
-            case CRYPT_DES_MODE_CBC:\r
-                $xor = $this->encryptIV;\r
-                for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
-                    $block = substr($plaintext, $i, 8);\r
-                    $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);\r
-                    $xor = $block;\r
-                    $ciphertext.= $block;\r
-                }\r
-                if ($this->continuousBuffer) {\r
-                    $this->encryptIV = $xor;\r
-                }\r
-                break;\r
-            case CRYPT_DES_MODE_CTR:\r
-                $xor = $this->encryptIV;\r
-                for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
-                    $block = substr($plaintext, $i, 8);\r
-                    $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);\r
-                    $ciphertext.= $block ^ $key;\r
-                }\r
-                if ($this->continuousBuffer) {\r
-                    $this->encryptIV = $xor;\r
-                }\r
-        }\r
-\r
-        return $ciphertext;\r
-    }\r
-\r
-    /**\r
-     * Decrypts a message.\r
-     *\r
-     * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.\r
-     *\r
-     * @see Crypt_DES::encrypt()\r
-     * @access public\r
-     * @param String $ciphertext\r
-     */\r
-    function decrypt($ciphertext)\r
-    {\r
-        if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {\r
-            // we pad with chr(0) since that's what mcrypt_generic does.  to quote from http://php.net/function.mcrypt-generic :\r
-            // "The data is padded with "\0" to make sure the length of the data is n * blocksize."\r
-            $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));\r
-        }\r
-\r
-        if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {\r
-            if ($this->changed) {\r
-                if (!isset($this->demcrypt)) {\r
-                    $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');\r
-                }\r
-                mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);\r
-                $this->changed = false;\r
-            }\r
-\r
-            $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);\r
-\r
-            if (!$this->continuousBuffer) {\r
-                mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);\r
-            }\r
-\r
-            return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;\r
-        }\r
-\r
-        if (!is_array($this->keys)) {\r
-            $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");\r
-        }\r
-\r
-        $plaintext = '';\r
-        switch ($this->mode) {\r
-            case CRYPT_DES_MODE_ECB:\r
-                for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
-                    $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);\r
-                }\r
-                break;\r
-            case CRYPT_DES_MODE_CBC:\r
-                $xor = $this->decryptIV;\r
-                for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
-                    $block = substr($ciphertext, $i, 8);\r
-                    $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;\r
-                    $xor = $block;\r
-                }\r
-                if ($this->continuousBuffer) {\r
-                    $this->decryptIV = $xor;\r
-                }\r
-                break;\r
-            case CRYPT_DES_MODE_CTR:\r
-                $xor = $this->decryptIV;\r
-                for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
-                    $block = substr($ciphertext, $i, 8);\r
-                    $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);\r
-                    $plaintext.= $block ^ $key;\r
-                }\r
-                if ($this->continuousBuffer) {\r
-                    $this->decryptIV = $xor;\r
-                }\r
-        }\r
-\r
-        return $this->mode != CRYPT_DES_MODE_CTR ? $this->_unpad($plaintext) : $plaintext;\r
-    }\r
-\r
-    /**\r
-     * Treat consecutive "packets" as if they are a continuous buffer.\r
-     *\r
-     * Say you have a 16-byte plaintext $plaintext.  Using the default behavior, the two following code snippets\r
-     * will yield different outputs:\r
-     *\r
-     * <code>\r
-     *    echo $des->encrypt(substr($plaintext, 0, 8));\r
-     *    echo $des->encrypt(substr($plaintext, 8, 8));\r
-     * </code>\r
-     * <code>\r
-     *    echo $des->encrypt($plaintext);\r
-     * </code>\r
-     *\r
-     * The solution is to enable the continuous buffer.  Although this will resolve the above discrepancy, it creates\r
-     * another, as demonstrated with the following:\r
-     *\r
-     * <code>\r
-     *    $des->encrypt(substr($plaintext, 0, 8));\r
-     *    echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));\r
-     * </code>\r
-     * <code>\r
-     *    echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));\r
-     * </code>\r
-     *\r
-     * With the continuous buffer disabled, these would yield the same output.  With it enabled, they yield different\r
-     * outputs.  The reason is due to the fact that the initialization vector's change after every encryption /\r
-     * decryption round when the continuous buffer is enabled.  When it's disabled, they remain constant.\r
-     *\r
-     * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each\r
-     * encryption / decryption round, whereas otherwise, it'd remain constant.  For this reason, it's recommended that\r
-     * continuous buffers not be used.  They do offer better security and are, in fact, sometimes required (SSH uses them),\r
-     * however, they are also less intuitive and more likely to cause you problems.\r
-     *\r
-     * @see Crypt_DES::disableContinuousBuffer()\r
-     * @access public\r
-     */\r
-    function enableContinuousBuffer()\r
-    {\r
-        $this->continuousBuffer = true;\r
-    }\r
-\r
-    /**\r
-     * Treat consecutive packets as if they are a discontinuous buffer.\r
-     *\r
-     * The default behavior.\r
-     *\r
-     * @see Crypt_DES::enableContinuousBuffer()\r
-     * @access public\r
-     */\r
-    function disableContinuousBuffer()\r
-    {\r
-        $this->continuousBuffer = false;\r
-        $this->encryptIV = $this->iv;\r
-        $this->decryptIV = $this->iv;\r
-    }\r
-\r
-    /**\r
-     * Pad "packets".\r
-     *\r
-     * DES works by encrypting eight bytes at a time.  If you ever need to encrypt or decrypt something that's not\r
-     * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.\r
-     *\r
-     * Padding is enabled by default.  Sometimes, however, it is undesirable to pad strings.  Such is the case in SSH1,\r
-     * where "packets" are padded with random bytes before being encrypted.  Unpad these packets and you risk stripping\r
-     * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is\r
-     * transmitted separately)\r
-     *\r
-     * @see Crypt_DES::disablePadding()\r
-     * @access public\r
-     */\r
-    function enablePadding()\r
-    {\r
-        $this->padding = true;\r
-    }\r
-\r
-    /**\r
-     * Do not pad packets.\r
-     *\r
-     * @see Crypt_DES::enablePadding()\r
-     * @access public\r
-     */\r
-    function disablePadding()\r
-    {\r
-        $this->padding = false;\r
-    }\r
-\r
-    /**\r
-     * Pads a string\r
-     *\r
-     * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).\r
-     * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)\r
-     *\r
-     * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless\r
-     * and padding will, hence forth, be enabled.\r
-     *\r
-     * @see Crypt_DES::_unpad()\r
-     * @access private\r
-     */\r
-    function _pad($text)\r
-    {\r
-        $length = strlen($text);\r
-\r
-        if (!$this->padding) {\r
-            if (($length & 7) == 0) {\r
-                return $text;\r
-            } else {\r
-                user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);\r
-                $this->padding = true;\r
-            }\r
-        }\r
-\r
-        $pad = 8 - ($length & 7);\r
-        return str_pad($text, $length + $pad, chr($pad));\r
-    }\r
-\r
-    /**\r
-     * Unpads a string\r
-     *\r
-     * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong\r
-     * and false will be returned.\r
-     *\r
-     * @see Crypt_DES::_pad()\r
-     * @access private\r
-     */\r
-    function _unpad($text)\r
-    {\r
-        if (!$this->padding) {\r
-            return $text;\r
-        }\r
-\r
-        $length = ord($text[strlen($text) - 1]);\r
-\r
-        if (!$length || $length > 8) {\r
-            return false;\r
-        }\r
-\r
-        return substr($text, 0, -$length);\r
-    }\r
-\r
-    /**\r
-     * Encrypts or decrypts a 64-bit block\r
-     *\r
-     * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT.  See\r
-     * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general\r
-     * idea of what this function does.\r
-     *\r
-     * @access private\r
-     * @param String $block\r
-     * @param Integer $mode\r
-     * @return String\r
-     */\r
-    function _processBlock($block, $mode)\r
-    {\r
-        // s-boxes.  in the official DES docs, they're described as being matrices that\r
-        // one accesses by using the first and last bits to determine the row and the\r
-        // middle four bits to determine the column.  in this implementation, they've\r
-        // been converted to vectors\r
-        static $sbox = array(\r
-            array(\r
-                14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,\r
-                 3, 10 ,10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,\r
-                 4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,\r
-                15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13\r
-            ),\r
-            array(\r
-                15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,\r
-                 9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,\r
-                 0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,\r
-                 5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9\r
-            ),\r
-            array(\r
-                10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,\r
-                 1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,\r
-                13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,\r
-                11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12\r
-            ),\r
-            array(\r
-                 7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,\r
-                 1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,\r
-                10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,\r
-                15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14\r
-            ),\r
-            array(\r
-                 2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,\r
-                 8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,\r
-                 4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,\r
-                15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3\r
-            ),\r
-            array(\r
-                12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,\r
-                 0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,\r
-                 9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,\r
-                 7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13\r
-            ),\r
-            array(\r
-                 4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,\r
-                 3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,\r
-                 1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,\r
-                10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12\r
-            ),\r
-            array(\r
-                13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,\r
-                10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,\r
-                 7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,\r
-                 0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11\r
-            )\r
-        );\r
-\r
-        $keys = $this->keys;\r
-\r
-        $temp = unpack('Na/Nb', $block);\r
-        $block = array($temp['a'], $temp['b']);\r
-\r
-        // because php does arithmetic right shifts, if the most significant bits are set, right\r
-        // shifting those into the correct position will add 1's - not 0's.  this will intefere\r
-        // with the | operation unless a second & is done.  so we isolate these bits and left shift\r
-        // them into place.  we then & each block with 0x7FFFFFFF to prevennt 1's from being added\r
-        // for any other shifts.\r
-        $msb = array(\r
-            ($block[0] >> 31) & 1,\r
-            ($block[1] >> 31) & 1\r
-        );\r
-        $block[0] &= 0x7FFFFFFF;\r
-        $block[1] &= 0x7FFFFFFF;\r
-\r
-        // we isolate the appropriate bit in the appropriate integer and shift as appropriate.  in\r
-        // some cases, there are going to be multiple bits in the same integer that need to be shifted\r
-        // in the same way.  we combine those into one shift operation.\r
-        $block = array(\r
-            (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |\r
-            (($block[1] & 0x00400001) <<  7) | (($block[1] & 0x40000100) >>  2) |\r
-            (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |\r
-            (($block[0] & 0x00400001) <<  3) | (($block[0] & 0x40000100) >>  6) |\r
-            (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |\r
-            (($block[1] & 0x00100000) <<  1) | (($block[1] & 0x10000000) >>  8) |\r
-            (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) <<  6) |\r
-            (($block[0] & 0x00100000) >>  3) | (($block[0] & 0x10000000) >> 12) |\r
-            (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) <<  4) |\r
-            (($block[1] & 0x00040000) >>  5) | (($block[1] & 0x04000000) >> 14) |\r
-            (($block[0] & 0x00000004) <<  9) | ( $block[0] & 0x00000400       ) |\r
-            (($block[0] & 0x00040000) >>  9) | (($block[0] & 0x04000000) >> 18) |\r
-            (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |\r
-            (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)\r
-        ,\r
-            (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |\r
-            (($block[1] & 0x00800002) <<  6) | (($block[0] & 0x00000080) << 20) |\r
-            (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) <<  2) |\r
-            (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) <<  9) |\r
-            ( $block[1] & 0x00200000       ) | (($block[1] & 0x20000000) >>  9) |\r
-            (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) <<  5) |\r
-            (($block[0] & 0x00200000) >>  4) | (($block[0] & 0x20000000) >> 13) |\r
-            (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) <<  3) |\r
-            (($block[1] & 0x00080000) >>  6) | (($block[1] & 0x08000000) >> 15) |\r
-            (($block[0] & 0x00000008) <<  8) | (($block[0] & 0x00000800) >>  1) |\r
-            (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |\r
-            (($block[1] & 0x00000200) >>  3) | (($block[0] & 0x00000200) >>  7) |\r
-            (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |\r
-            (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |\r
-            ($msb[1] << 28) | ($msb[0] << 24)\r
-        );\r
-\r
-        for ($i = 0; $i < 16; $i++) {\r
-            // start of "the Feistel (F) function" - see the following URL:\r
-            // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png\r
-            $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)\r
-                  | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)\r
-                  | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)\r
-                  | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)\r
-                  | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)\r
-                  | (($sbox[5][(($block[1] & 0x00001F80) >>  7) ^ $keys[$mode][$i][5]]) <<  8)\r
-                  | (($sbox[6][(($block[1] & 0x000001F8) >>  3) ^ $keys[$mode][$i][6]]) <<  4)\r
-                  | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);\r
-\r
-            $msb = ($temp >> 31) & 1;\r
-            $temp &= 0x7FFFFFFF;\r
-            $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) <<  5)\r
-                      | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)\r
-                      | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) <<  6)\r
-                      | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) <<  9)\r
-                      | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)\r
-                      | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >>  8)\r
-                      | (($temp & 0x00004000) <<  4) | (($temp & 0x00000002) << 16)\r
-                      | (($temp & 0x00442000) >>  6) | (($temp & 0x40800000) >> 15)\r
-                      | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)\r
-                      | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) <<  3)\r
-                      | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >>  7)\r
-                      | (($temp & 0x00200000) >> 19) | ($msb << 23);\r
-            // end of "the Feistel (F) function" - $newBlock is F's output\r
-\r
-            $temp = $block[1];\r
-            $block[1] = $block[0] ^ $newBlock;\r
-            $block[0] = $temp;\r
-        }\r
-\r
-        $msb = array(\r
-            ($block[0] >> 31) & 1,\r
-            ($block[1] >> 31) & 1\r
-        );\r
-        $block[0] &= 0x7FFFFFFF;\r
-        $block[1] &= 0x7FFFFFFF;\r
-\r
-        $block = array(\r
-            (($block[0] & 0x01000004) <<  7) | (($block[1] & 0x01000004) <<  6) |\r
-            (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |\r
-            (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |\r
-            (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |\r
-            (($block[0] & 0x02000008) >>  2) | (($block[1] & 0x02000008) >>  3) |\r
-            (($block[0] & 0x00020000) <<  4) | (($block[1] & 0x00020000) <<  3) |\r
-            (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) <<  9) |\r
-            (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |\r
-            (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |\r
-            (($block[0] & 0x00040000) >>  5) | (($block[1] & 0x00040000) >>  6) |\r
-            (($block[0] & 0x00000400) <<  1) | ( $block[1] & 0x00000400       ) |\r
-            (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |\r
-            (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |\r
-            (($block[0] & 0x00000800) >>  8) | (($block[1] & 0x00000800) >>  9)\r
-        ,\r
-            (($block[0] & 0x10000040) <<  3) | (($block[1] & 0x10000040) <<  2) |\r
-            (($block[0] & 0x00100000) <<  9) | (($block[1] & 0x00100000) <<  8) |\r
-            (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |\r
-            (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |\r
-            (($block[0] & 0x20000080) >>  6) | (($block[1] & 0x20000080) >>  7) |\r
-            ( $block[0] & 0x00200000       ) | (($block[1] & 0x00200000) >>  1) |\r
-            (($block[0] & 0x00002000) <<  6) | (($block[1] & 0x00002000) <<  5) |\r
-            (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |\r
-            (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |\r
-            (($block[0] & 0x00400000) >>  9) | (($block[1] & 0x00400000) >> 10) |\r
-            (($block[0] & 0x00004000) >>  3) | (($block[1] & 0x00004000) >>  4) |\r
-            (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |\r
-            (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |\r
-            ($msb[0] <<  7) | ($msb[1] <<  6)\r
-        );\r
-\r
-        return pack('NN', $block[0], $block[1]);\r
-    }\r
-\r
-    /**\r
-     * Creates the key schedule.\r
-     *\r
-     * @access private\r
-     * @param String $key\r
-     * @return Array\r
-     */\r
-    function _prepareKey($key)\r
-    {\r
-        static $shifts = array( // number of key bits shifted per round\r
-            1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1\r
-        );\r
-\r
-        // pad the key and remove extra characters as appropriate.\r
-        $key = str_pad(substr($key, 0, 8), 8, chr(0));\r
-\r
-        $temp = unpack('Na/Nb', $key);\r
-        $key = array($temp['a'], $temp['b']);\r
-        $msb = array(\r
-            ($key[0] >> 31) & 1,\r
-            ($key[1] >> 31) & 1\r
-        );\r
-        $key[0] &= 0x7FFFFFFF;\r
-        $key[1] &= 0x7FFFFFFF;\r
-\r
-        $key = array(\r
-            (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |\r
-            (($key[1] & 0x00020408) <<  8) | (($key[1] & 0x02040800) >>  1) |\r
-            (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |\r
-            (($key[0] & 0x00020408) <<  4) | (($key[0] & 0x02040800) >>  5) |\r
-            (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |\r
-            (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |\r
-            (($key[0] & 0x00000010) >>  1) | (($key[0] & 0x00001000) >> 10) |\r
-            (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)\r
-        ,\r
-            (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |\r
-            (($key[1] & 0x00800000) <<  2) | (($key[0] & 0x00000080) << 16) |\r
-            (($key[0] & 0x00008000) <<  7) | (($key[0] & 0x00800000) >>  2) |\r
-            (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) <<  4) |\r
-            (($key[1] & 0x00400000) >>  5) | (($key[1] & 0x40000000) >> 14) |\r
-            (($key[0] & 0x00000040) <<  9) | ( $key[0] & 0x00004000       ) |\r
-            (($key[0] & 0x00400000) >>  9) | (($key[0] & 0x40000000) >> 18) |\r
-            (($key[1] & 0x00000020) <<  6) | (($key[1] & 0x00002000) >>  3) |\r
-            (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |\r
-            (($key[0] & 0x00000020) <<  2) | (($key[0] & 0x00002000) >>  7) |\r
-            (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |\r
-            (($key[1] & 0x00000010) >>  1) | (($key[1] & 0x00001000) >> 10) |\r
-            (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |\r
-            ($msb[1] << 24) | ($msb[0] << 20)\r
-        ); \r
-\r
-        $keys = array();\r
-        for ($i = 0; $i < 16; $i++) {\r
-            $key[0] <<= $shifts[$i];\r
-            $temp = ($key[0] & 0xF0000000) >> 28;\r
-            $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;\r
-\r
-            $key[1] <<= $shifts[$i];\r
-            $temp = ($key[1] & 0xF0000000) >> 28;\r
-            $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;\r
-\r
-            $temp = array(\r
-                (($key[1] & 0x00004000) >>  9) | (($key[1] & 0x00000800) >>  7) |\r
-                (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >>  2) |\r
-                (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)\r
-            ,\r
-                (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) <<  4) |\r
-                (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |\r
-                (($key[1] & 0x00000080) >>  6)\r
-            ,\r
-                ( $key[1] & 0x00000020       ) | (($key[1] & 0x00000200) >>  5) |\r
-                (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |\r
-                (($key[1] & 0x00000004) >>  1) | (($key[1] & 0x00100000) >> 20)\r
-            ,\r
-                (($key[1] & 0x00001000) >>  7) | (($key[1] & 0x00200000) >> 17) |\r
-                (($key[1] & 0x00000002) <<  2) | (($key[1] & 0x00000100) >>  6) |\r
-                (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)\r
-            ,\r
-                (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010       ) |\r
-                (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |\r
-                (($key[0] & 0x00000200) >>  8) | (($key[0] & 0x00000002) >>  1)\r
-            ,\r
-                (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |\r
-                (($key[0] & 0x00000020) >>  2) | (($key[0] & 0x00000800) >>  9) |\r
-                (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >>  8)\r
-            ,\r
-                (($key[0] & 0x00001000) >>  7) | (($key[0] & 0x00000088) >>  3) |\r
-                (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) <<  2) |\r
-                (($key[0] & 0x00400000) >> 21)\r
-            ,\r
-                (($key[0] & 0x00000400) >>  5) | (($key[0] & 0x00004000) >> 10) |\r
-                (($key[0] & 0x00000040) >>  3) | (($key[0] & 0x00100000) >> 18) |\r
-                (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)\r
-            );\r
-\r
-            $keys[] = $temp;\r
-        }\r
-\r
-        $temp = array(\r
-            CRYPT_DES_ENCRYPT => $keys,\r
-            CRYPT_DES_DECRYPT => array_reverse($keys)\r
-        );\r
-\r
-        return $temp;\r
-    }\r
-}\r
-\r
-// vim: ts=4:sw=4:et:\r
-// vim6: fdl=1:
\ No newline at end of file