Вход Регистрация
Файл: oc-includes/phpseclib/Crypt/Blowfish.php
Строк: 347
<?php

/**
 * Pure-PHP implementation of Blowfish.
 *
 * Uses mcrypt, if available, and an internal implementation, otherwise.
 *
 * PHP versions 4 and 5
 *
 * Useful resources are as follows:
 *
 *  - {@link http://en.wikipedia.org/wiki/Blowfish_(cipher) Wikipedia description of Blowfish}
 *
 * Here's a short example of how to use this library:
 * <code>
 * <?php
 *    include 'Crypt/Blowfish.php';
 *
 *    $blowfish = new Crypt_Blowfish();
 *
 *    $blowfish->setKey('12345678901234567890123456789012');
 *
 *    $plaintext = str_repeat('a', 1024);
 *
 *    echo $blowfish->decrypt($blowfish->encrypt($plaintext));
 * ?>
 * </code>
 *
 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @category  Crypt
 * @package   Crypt_Blowfish
 * @author    Jim Wigginton <terrafrost@php.net>
 * @author    Hans-Juergen Petrich <petrich@tronic-media.com>
 * @copyright 2007 Jim Wigginton
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link      http://phpseclib.sourceforge.net
 */

/**
 * Include Crypt_Base
 *
 * Base cipher class
 */
if (!class_exists('Crypt_Base')) {
    include_once 
'Base.php';
}

/**#@+
 * @access public
 * @see Crypt_Blowfish::encrypt()
 * @see Crypt_Blowfish::decrypt()
 */
/**
 * Encrypt / decrypt using the Counter mode.
 *
 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
 *
 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
 */
define('CRYPT_BLOWFISH_MODE_CTR'CRYPT_MODE_CTR);
/**
 * Encrypt / decrypt using the Electronic Code Book mode.
 *
 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
 */
define('CRYPT_BLOWFISH_MODE_ECB'CRYPT_MODE_ECB);
/**
 * Encrypt / decrypt using the Code Book Chaining mode.
 *
 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
 */
define('CRYPT_BLOWFISH_MODE_CBC'CRYPT_MODE_CBC);
/**
 * Encrypt / decrypt using the Cipher Feedback mode.
 *
 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
 */
define('CRYPT_BLOWFISH_MODE_CFB'CRYPT_MODE_CFB);
/**
 * Encrypt / decrypt using the Cipher Feedback mode.
 *
 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
 */
define('CRYPT_BLOWFISH_MODE_OFB'CRYPT_MODE_OFB);
/**#@-*/

/**
 * Pure-PHP implementation of Blowfish.
 *
 * @package Crypt_Blowfish
 * @author  Jim Wigginton <terrafrost@php.net>
 * @author  Hans-Juergen Petrich <petrich@tronic-media.com>
 * @access  public
 */
class Crypt_Blowfish extends Crypt_Base
{
    
/**
     * Block Length of the cipher
     *
     * @see Crypt_Base::block_size
     * @var Integer
     * @access private
     */
    
var $block_size 8;

    
/**
     * The default password key_size used by setPassword()
     *
     * @see Crypt_Base::password_key_size
     * @see Crypt_Base::setPassword()
     * @var Integer
     * @access private
     */
    
var $password_key_size 56;

    
/**
     * The namespace used by the cipher for its constants.
     *
     * @see Crypt_Base::const_namespace
     * @var String
     * @access private
     */
    
var $const_namespace 'BLOWFISH';

    
/**
     * The mcrypt specific name of the cipher
     *
     * @see Crypt_Base::cipher_name_mcrypt
     * @var String
     * @access private
     */
    
var $cipher_name_mcrypt 'blowfish';

    
/**
     * Optimizing value while CFB-encrypting
     *
     * @see Crypt_Base::cfb_init_len
     * @var Integer
     * @access private
     */
    
var $cfb_init_len 500;

    
/**
     * The fixed subkeys boxes ($sbox0 - $sbox3) with 256 entries each
     *
     * S-Box 0
     *
     * @access private
     * @var    array
     */
    
var $sbox0 = array (
        
0xd1310ba60x98dfb5ac0x2ffd72db0xd01adfb70xb8e1afed0x6a267e960xba7c90450xf12c7f99,
        
0x24a199470xb3916cf70x0801f2e20x858efc160x636920d80x71574e690xa458fea30xf4933d7e,
        
0x0d95748f0x728eb6580x718bcd580x82154aee0x7b54a41d0xc25a59b50x9c30d5390x2af26013,
        
0xc5d1b0230x286085f00xca4179180xb8db38ef0x8e79dcb00x603a180e0x6c9e0e8b0xb01e8a3e,
        
0xd71577c10xbd314b270x78af2fda0x55605c600xe65525f30xaa55ab940x574898620x63e81440,
        
0x55ca396a0x2aab10b60xb4cc5c340x1141e8ce0xa15486af0x7c72e9930xb3ee14110x636fbc2a,
        
0x2ba9c55d0x741831f60xce5c3e160x9b87931e0xafd6ba330x6c24cf5c0x7a3253810x28958677,
        
0x3b8f48980x6b4bb9af0xc4bfe81b0x662821930x61d809cc0xfb21a9910x487cac600x5dec8032,
        
0xef845d5d0xe98575b10xdc2623020xeb651b880x23893e810xd396acc50x0f6d6ff30x83f44239,
        
0x2e0b44820xa48420040x69c8f04a0x9e1f9b5e0x21c668420xf6e96c9a0x670c9c610xabd388f0,
        
0x6a51a0d20xd8542f680x960fa7280xab5133a30x6eef0b6c0x137a3be40xba3bf0500x7efb2a98,
        
0xa1f1651d0x39af01760x66ca593e0x82430e880x8cee86190x456f9fb40x7d84a5c30x3b8b5ebe,
        
0xe06f75d80x85c120730x401a449f0x56c16aa60x4ed3aa620x363f77060x1bfedf720x429b023d,
        
0x37d0d7240xd00a12480xdb0fead30x49f1c09b0x075372c90x80991b7b0x25d479d80xf6e8def7,
        
0xe3fe501a0xb6794c3b0x976ce0bd0x04c006ba0xc1a94fb60x409f60c40x5e5c9ec20x196a2463,
        
0x68fb6faf0x3e6c53b50x1339b2eb0x3b52ec6f0x6dfc511f0x9b30952c0xcc8145440xaf5ebd09,
        
0xbee3d0040xde334afd0x660f28070x192e4bb30xc0cba8570x45c8740f0xd20b5f390xb9d3fbdb,
        
0x5579c0bd0x1a60320a0xd6a100c60x402c72790x679f25fe0xfb1fa3cc0x8ea5e9f80xdb3222f8,
        
0x3c7516df0xfd616b150x2f501ec80xad0552ab0x323db5fa0xfd2387600x53317b480x3e00df82,
        
0x9e5c57bb0xca6f8ca00x1a87562e0xdf1769db0xd542a8f60x287effc30xac6732c60x8c4f5573,
        
0x695b27b00xbbca58c80xe1ffa35d0xb8f011a00x10fa3d980xfd2183b80x4afcb56c0x2dd1d35b,
        
0x9a53e4790xb6f845650xd28e49bc0x4bfb97900xe1ddf2da0xa4cb7e330x62fb13410xcee4c6e8,
        
0xef20cada0x36774c010xd07e9efe0x2bf11fb40x95dbda4d0xae9091980xeaad8e710x6b93d5a0,
        
0xd08ed1d00xafc725e00x8e3c5b2f0x8e7594b70x8ff6e2fb0xf2122b640x8888b8120x900df01c,
        
0x4fad5ea00x688fc31c0xd1cff1910xb3a8c1ad0x2f2f22180xbe0e17770xea752dfe0x8b021fa1,
        
0xe5a0cc0f0xb56f74e80x18acf3d60xce89e2990xb4a84fe00xfd13e0b70x7cc43b810xd2ada8d9,
        
0x165fa2660x809577050x93cc73140x211a14770xe6ad20650x77b5fa860xc75442f50xfb9d35cf,
        
0xebcdaf0c0x7b3e89a00xd6411bd30xae1e7e490x00250e2d0x2071b35e0x226800bb0x57b8e0af,
        
0x2464369b0xf009b91e0x5563911d0x59dfa6aa0x78c143890xd95a537f0x207d5ba20x02e5b9c5,
        
0x832603760x6295cfa90x11c819680x4e734a410xb3472dca0x7b14a94a0x1b5100520x9a532915,
        
0xd60f573f0xbc9bc6e40x2b60a4760x81e674000x08ba6fb50x571be91f0xf296ec6b0x2a0dd915,
        
0xb66365210xe7b9f9b60xff34052e0xc58556640x53b02d5d0xa99f8fa10x08ba47990x6e85076a
    
);

    
/**
     * S-Box 1
     *
     * @access private
     * @var    array
     */
    
var $sbox1 = array(
        
0x4b7a70e90xb5b329440xdb75092e0xc41926230xad6ea6b00x49a7df7d0x9cee60b80x8fedb266,
        
0xecaa8c710x699a17ff0x5664526c0xc2b19ee10x193602a50x75094c290xa05913400xe4183a3e,
        
0x3f54989a0x5b429d650x6b8fe4d60x99f73fd60xa1d29c070xefe830f50x4d2d38e60xf0255dc1,
        
0x4cdd20860x8470eb260x6382e9c60x021ecc5e0x09686b3f0x3ebaefc90x3c9718140x6b6a70a1,
        
0x687f35840x52a0e2860xb79c53050xaa5007370x3e07841c0x7fdeae5c0x8e7d44ec0x5716f2b8,
        
0xb03ada370xf0500c0d0xf01c1f040x0200b3ff0xae0cf51a0x3cb574b20x25837a580xdc0921bd,
        
0xd19113f90x7ca92ff60x943247730x22f547010x3ae5e5810x37c2dadc0xc8b576340x9af3dda7,
        
0xa94461460x0fd0030e0xecc8c73e0xa4751e410xe238cd990x3bea0e2f0x3280bba10x183eb331,
        
0x4e548b380x4f6db9080x6f420d030xf60a04bf0x2cb812900x24977c790x5679b0720xbcaf89af,
        
0xde9a771f0xd99308100xb38bae120xdccf3f2e0x5512721f0x2e6b71240x501adde60x9f84cd87,
        
0x7a5847180x7408da170xbc9f9abc0xe94b7d8c0xec7aec3a0xdb851dfa0x630943660xc464c3d2,
        
0xef1c18470x3215d9080xdd433b370x24c2ba160x12a14d430x2a65c4510x509400020x133ae4dd,
        
0x71dff89e0x10314e550x81ac77d60x5f11199b0x043556f10xd7a3c76b0x3c11183b0x5924a509,
        
0xf28fe6ed0x97f1fbfa0x9ebabf2c0x1e153c6e0x86e345700xeae96fb10x860e5e0a0x5a3e2ab3,
        
0x771fe71c0x4e3d06fa0x2965dcb90x99e71d0f0x803e89d60x5266c8250x2e4cc9780x9c10b36a,
        
0xc6150eba0x94e2ea780xa5fc3c530x1e0a2df40xf2f74ea70x361d2b3d0x1939260f0x19c27960,
        
0x5223a7080xf71312b60xebadfe6e0xeac31f660xe3bc45950xa67bc8830xb17f37d10x018cff28,
        
0xc332ddef0xbe6c5aa50x655821850x68ab98020xeecea50f0xdb2f953b0x2aef7dad0x5b6e2f84,
        
0x1521b6280x290761700xecdd47750x619f15100x13cca8300xeb61bd960x0334fe1e0xaa0363cf,
        
0xb5735c900x4c70a2390xd59e9e0b0xcbaade140xeecc86bc0x60622ca70x9cab5cab0xb2f3846e,
        
0x648b1eaf0x19bdf0ca0xa02369b90x655abb500x40685a320x3c2ab4b30x319ee9d50xc021b8f7,
        
0x9b540b190x875fa0990x95f7997e0x623d7da80xf837889a0x97e32d770x11ed935f0x16681281,
        
0x0e3588290xc7e61fd60x96dedfa10x7858ba990x57f584a50x1b2272630x9b83c3ff0x1ac24696,
        
0xcdb30aeb0x532e30540x8fd948e40x6dbc31280x58ebf2ef0x34c6ffea0xfe28ed610xee7c3c73,
        
0x5d4a14d90xe864b7e30x42105d140x203e13e00x45eee2b60xa3aaabea0xdb6c4f150xfacb4fd0,
        
0xc742f4420xef6abbb50x654f3b1d0x41cd21050xd81e799e0x86854dc70xe44b476a0x3d816250,
        
0xcf62a1f20x5b8d26460xfc8883a00xc1c7b6a30x7f1524c30x69cb74920x47848a0b0x5692b285,
        
0x095bbf000xad19489d0x1462b1740x23820e000x58428d2a0x0c55f5ea0x1dadf43e0x233f7061,
        
0x3372f0920x8d937e410xd65fecf10x6c223bdb0x7cde37590xcbee74600x4085f2a70xce77326e,
        
0xa60780840x19f8509e0xe8efd8550x61d997350xa969a7aa0xc50c06c20x5a04abfc0x800bcadc,
        
0x9e447a2e0xc34534840xfdd567050x0e1e9ec90xdb73dbd30x105588cd0x675fda790xe3674340,
        
0xc5c434650x713e38d80x3d28f89e0xf16dff200x153e21e70x8fb03d4a0xe6e39f2b0xdb83adf7
    
);

    
/**
     * S-Box 2
     *
     * @access private
     * @var    array
     */
    
var $sbox2 = array(
        
0xe93d5a680x948140f70xf64c261c0x946929340x411520f70x7602d4f70xbcf46b2e0xd4a20068,
        
0xd40824710x3320f46a0x43b7d4b70x500061af0x1e39f62e0x972445460x14214f740xbf8b8840,
        
0x4d95fc1d0x96b591af0x70f4ddd30x66a02f450xbfbc09ec0x03bd97850x7fac6dd00x31cb8504,
        
0x96eb27b30x55fd39410xda2547e60xabca0a9a0x285078250x530429f40x0a2c86da0xe9b66dfb,
        
0x68dc14620xd74869000x680ec0a40x27a18dee0x4f3ffea20xe887ad8c0xb58ce0060x7af4d6b6,
        
0xaace1e7c0xd3375fec0xce78a3990x406b2a420x20fe9e350xd9f385b90xee39d7ab0x3b124e8b,
        
0x1dc9faf70x4b6d18560x26a366310xeae397b20x3a6efa740xdd5b43320x6841e7f70xca7820fb,
        
0xfb0af54e0xd8feb3970x454056ac0xba4895270x55533a3a0x20838d870xfe6ba9b70xd096954b,
        
0x55a867bc0xa1159a580xcca929630x99e1db330xa62a4a560x3f3125f90x5ef47e1c0x9029317c,
        
0xfdf8e8020x04272f700x80bb155c0x05282ce30x95c115480xe4c66d220x48c1133f0xc70f86dc,
        
0x07f9c9ee0x41041f0f0x404779a40x5d886e170x325f51eb0xd59bc0d10xf2bcc18f0x41113564,
        
0x257b78340x602a9c600xdff8e8a30x1f636c1b0x0e12b4c20x02e1329e0xaf664fd10xcad18115,
        
0x6b2395e00x333e92e10x3b240b620xeebeb9220x85b2a20e0xe6ba0d990xde720c8c0x2da2f728,
        
0xd01278450x95b794fd0x647d08620xe7ccf5f00x5449a36f0x877d48fa0xc39dfd270xf33e8d1e,
        
0x0a4763410x992eff740x3a6f6eab0xf4f8fd370xa812dc600xa1ebddf80x991be14c0xdb6e6b0d,
        
0xc67b55100x6d672c370x2765d43b0xdcd0e8040xf1290dc70xcc00ffa30xb5390f920x690fed0b,
        
0x667b9ffb0xcedb7d9c0xa091cf0b0xd9155ea30xbb132f880x515bad240x7b9479bf0x763bd6eb,
        
0x37392eb30xcc1159790x8026e2970xf42e312d0x6842ada70xc66a2b3b0x12754ccc0x782ef11c,
        
0x6a1242370xb79251e70x06a1bbe60x4bfb63500x1a6b10180x11caedfa0x3d25bdd80xe2e1c3c9,
        
0x444216590x0a1213860xd90cec6e0xd5abea2a0x64af674e0xda86a85f0xbebfe9880x64e4c3fe,
        
0x9dbc80570xf0f7c0860x60787bf80x6003604d0xd1fd83460xf6381fb00x7745ae040xd736fccc,
        
0x83426b330xf01eab710xb08041870x3c005e5f0x77a057be0xbde8ae240x554642990xbf582e61,
        
0x4e58f48f0xf2ddfda20xf474ef380x8789bdc20x5366f9c30xc8b38e740xb475f2550x46fcd9b9,
        
0x7aeb26610x8b1ddf840x846a0e790x915f95e20x466e598e0x20b457700x8cd555910xc902de4c,
        
0xb90bace10xbb8205d00x11a862480x7574a99e0xb77f19b60xe0a9dc090x662d09a10xc4324633,
        
0xe85a1f020x09f0be8c0x4a99a0250x1d6efe100x1ab93d1d0x0ba5a4df0xa186f20f0x2868f169,
        
0xdcb7da830x573906fe0xa1e2ce9b0x4fcd7f520x50115e010xa70683fa0xa002b5c40x0de6d027,
        
0x9af88c270x773f86410xc3604c060x61a806b50xf0177a280xc0f586e00x006058aa0x30dc7d62,
        
0x11e69ed70x2338ea630x53c2dd940xc2c216340xbbcbee560x90bcb6de0xebfc7da10xce591d76,
        
0x6f05e4090x4b7c01880x39720a3d0x7c927c240x86e3725f0x724d9db90x1ac15bb40xd39eb8fc,
        
0xed5455780x08fca5b50xd83d7cd30x4dad0fc40x1e50ef5e0xb161e6f80xa28514d90x6c51133c,
        
0x6fd5c7e70x56e14ec40x362abfce0xddc6c8370xd79a32340x926382120x670efa8e0x406000e0
    
);

    
/**
     * S-Box 3
     *
     * @access private
     * @var    array
     */
    
var $sbox3 = array(
        
0x3a39ce370xd3faf5cf0xabc277370x5ac52d1b0x5cb0679e0x4fa337420xd38227400x99bc9bbe,
        
0xd5118e9d0xbf0f73150xd62d1c7e0xc700c47b0xb78c1b6b0x21a190450xb26eb1be0x6a366eb4,
        
0x5748ab2f0xbc946e790xc6a376d20x6549c2c80x530ff8ee0x468dde7d0xd5730a1d0x4cd04dc6,
        
0x2939bbdb0xa9ba46500xac9526e80xbe5ee3040xa1fad5f00x6a2d519a0x63ef8ce20x9a86ee22,
        
0xc089c2b80x43242ef60xa51e03aa0x9cf2d0a40x83c061ba0x9be96a4d0x8fe515500xba645bd6,
        
0x2826a2f90xa73a3ae10x4ba995860xef5562e90xc72fefd30xf752f7da0x3f046f690x77fa0a59,
        
0x80e4a9150x87b086010x9b09e6ad0x3b3ee5930xe990fd5a0x9e34d7970x2cf0b7d90x022b8b51,
        
0x96d5ac3a0x017da67d0xd1cf3ed60x7c7d2d280x1f9f25cf0xadf2b89b0x5ad6b4720x5a88f54c,
        
0xe029ac710xe019a5e60x47b0acfd0xed93fa9b0xe8d3c48d0x283b57cc0xf8d566290x79132e28,
        
0x785f01910xed7560550xf7960e440xe3d35e8c0x15056dd40x88f46dba0x03a161250x0564f0bd,
        
0xc3eb9e150x3c9057a20x97271aec0xa93a072a0x1b3f6d9b0x1e6321f50xf59c66fb0x26dcf319,
        
0x7533d9280xb155fdf50x035634820x8aba3cbb0x285177110xc20ad9f80xabcc51670xccad925f,
        
0x4de817510x3830dc8e0x379d58620x9320f9910xea7a90c20xfb3e7bce0x5121ce640x774fbe32,
        
0xa8b6e37e0xc3293d460x48de53690x6413e6800xa2ae08100xdd6db2240x69852dfd0x09072166,
        
0xb39a460a0x6445c0dd0x586cdecf0x1c20c8ae0x5bbef7dd0x1b588d400xccd2017f0x6bb4e3bb,
        
0xdda26a7e0x3a59ff450x3e350a440xbcb4cdd50x72eacea80xfa6484bb0x8d6612ae0xbf3c6f47,
        
0xd29be4630x542f5d9e0xaec2771b0xf64e63700x740e0d8d0xe75b13570xf87216710xaf537d5d,
        
0x4040cb080x4eb4e2cc0x34d2466a0x0115af840xe1b004280x95983a1d0x06b89fb40xce6ea048,
        
0x6f3f3b820x3520ab820x011a1d4b0x277227f80x611560b10xe7933fdc0xbb3a792b0x344525bd,
        
0xa08839e10x51ce794b0x2f32c9b70xa01fbac90xe01cc87e0xbcc7d1f60xcf0111c30xa1e8aac7,
        
0x1a9087490xd44fbd9a0xd0dadecb0xd50ada380x0339c32a0xc69136670x8df9317c0xe0b12b4f,
        
0xf79e59b70x43f5bb3a0xf2d519ff0x27d9459c0xbf97222c0x15e6fc2a0x0f91fc710x9b941525,
        
0xfae593610xceb69ceb0xc2a864590x12baa8d10xb6c1075e0xe3056a0c0x10d250650xcb03a442,
        
0xe0ec6e0e0x1698db3b0x4c98a0be0x3278e9640x9f1f95320xe0d392df0xd3a0342b0x8971f21e,
        
0x1b0a74410x4ba3348c0xc5be71200xc37632d80xdf359f8d0x9b992f2e0xe60b6f470x0fe3f11d,
        
0xe54cda540x1edad8910xce6279cf0xcd3e7e6f0x1618b1660xfd2c1d050x848fd2c50xf6fb2299,
        
0xf523f3570xa63276230x93a835310x56cccd020xacf081620x5a75ebb50x6e1636970x88d273cc,
        
0xde9662920x81b949d00x4c50901b0x71c656140xe6c6c7bd0x327a140a0x45e1d0060xc3f27b9a,
        
0xc9aa53fd0x62a80f000xbb25bfe20x35bdd2f60x711269050xb20402220xb6cbcf7c0xcd769c2b,
        
0x53113ec00x1640e3d30x38abbd600x2547adf00xba38209c0xf746ce760x77afa1c50x20756060,
        
0x85cbfe4e0x8ae88dd80x7aaaf9b00x4cf9aa7e0x1948c25c0x02fb8a8c0x01c36ae40xd6ebe1f9,
        
0x90d4f8690xa65cdea00x3f09252d0xc208e69f0xb74e61320xce77e25b0x578fdfe30x3ac372e6
    
);

    
/**
     * P-Array consists of 18 32-bit subkeys
     *
     * @var array
     * @access private
     */
    
var $parray = array(
        
0x243f6a880x85a308d30x13198a2e0x037073440xa40938220x299f31d0,
        
0x082efa980xec4e6c890x452821e60x38d013770xbe5466cf0x34e90c6c,
        
0xc0ac29b70xc97c50dd0x3f84d5b50xb54709170x9216d5d90x8979fb1b
    
);

    
/**
     * The BCTX-working Array
     *
     * Holds the expanded key [p] and the key-depended s-boxes [sb]
     *
     * @var array
     * @access private
     */
    
var $bctx;

    
/**
     * Holds the last used key
     *
     * @var Array
     * @access private
     */
    
var $kl;

    
/**
     * Sets the key.
     *
     * Keys can be of any length.  Blowfish, itself, requires the use of a key between 32 and max. 448-bits long.
     * If the key is less than 32-bits we NOT fill the key to 32bit but let the key as it is to be compatible
     * with mcrypt because mcrypt act this way with blowfish key's < 32 bits.
     *
     * If the key is more than 448-bits, we trim the excess bits.
     *
     * If the key is not explicitly set, or empty, it'll be assumed a 128 bits key to be all null bytes.
     *
     * @access public
     * @see Crypt_Base::setKey()
     * @param String $key
     */
    
function setKey($key)
    {
        
$keylength strlen($key);

        if (!
$keylength) {
            
$key "";
        } elseif (
$keylength 56) {
            
$key substr($key056);
        }

        
parent::setKey($key);
    }

    
/**
     * Test for engine validity
     *
     * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine()
     *
     * @see Crypt_Base::isValidEngine()
     * @param Integer $engine
     * @access public
     * @return Boolean
     */
    
function isValidEngine($engine)
    {
        if (
$engine == CRYPT_ENGINE_OPENSSL) {
            if (
strlen($this->key) != 16) {
                return 
false;
            }
            
$this->cipher_name_openssl_ecb 'bf-ecb';
            
$this->cipher_name_openssl 'bf-' $this->_openssl_translate_mode();
        }

        return 
parent::isValidEngine($engine);
    }

    
/**
     * Setup the key (expansion)
     *
     * @see Crypt_Base::_setupKey()
     * @access private
     */
    
function _setupKey()
    {
        if (isset(
$this->kl['key']) && $this->key === $this->kl['key']) {
            
// already expanded
            
return;
        }
        
$this->kl = array('key' => $this->key);

        
/* key-expanding p[] and S-Box building sb[] */
        
$this->bctx = array(
            
'p'  => array(),
            
'sb' => array(
                
$this->sbox0,
                
$this->sbox1,
                
$this->sbox2,
                
$this->sbox3
            
)
        );

        
// unpack binary string in unsigned chars
        
$key  array_values(unpack('C*'$this->key));
        
$keyl count($key);
        for (
$j 0$i 0$i 18; ++$i) {
            
// xor P1 with the first 32-bits of the key, xor P2 with the second 32-bits ...
            
for ($data 0$k 0$k 4; ++$k) {
                
$data = ($data << 8) | $key[$j];
                if (++
$j >= $keyl) {
                    
$j 0;
                }
            }
            
$this->bctx['p'][] = $this->parray[$i] ^ $data;
        }

        
// encrypt the zero-string, replace P1 and P2 with the encrypted data,
        // encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys
        
$data "";
        for (
$i 0$i 18$i += 2) {
            list(
$l$r) = array_values(unpack('N*'$data $this->_encryptBlock($data)));
            
$this->bctx['p'][$i    ] = $l;
            
$this->bctx['p'][$i 1] = $r;
        }
        for (
$i 0$i 4; ++$i) {
            for (
$j 0$j 256$j += 2) {
                list(
$l$r) = array_values(unpack('N*'$data $this->_encryptBlock($data)));
                
$this->bctx['sb'][$i][$j    ] = $l;
                
$this->bctx['sb'][$i][$j 1] = $r;
            }
        }
    }

    
/**
     * Encrypts a block
     *
     * @access private
     * @param String $in
     * @return String
     */
    
function _encryptBlock($in)
    {
        
$p $this->bctx["p"];
        
// extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower
        
$sb_0 $this->bctx["sb"][0];
        
$sb_1 $this->bctx["sb"][1];
        
$sb_2 $this->bctx["sb"][2];
        
$sb_3 $this->bctx["sb"][3];

        
$in unpack("N*"$in);
        
$l $in[1];
        
$r $in[2];

        for (
$i 0$i 16$i+= 2) {
            
$l^= $p[$i];
            
$r^= ($sb_0[$l >> 24 0xff]  +
                  
$sb_1[$l >> 16 0xff]  ^
                  
$sb_2[$l >>  0xff]) +
                  
$sb_3[$l       0xff];

            
$r^= $p[$i 1];
            
$l^= ($sb_0[$r >> 24 0xff]  +
                  
$sb_1[$r >> 16 0xff]  ^
                  
$sb_2[$r >>  0xff]) +
                  
$sb_3[$r       0xff];
        }
        return 
pack("N*"$r $p[17], $l $p[16]);
    }

    
/**
     * Decrypts a block
     *
     * @access private
     * @param String $in
     * @return String
     */
    
function _decryptBlock($in)
    {
        
$p $this->bctx["p"];
        
$sb_0 $this->bctx["sb"][0];
        
$sb_1 $this->bctx["sb"][1];
        
$sb_2 $this->bctx["sb"][2];
        
$sb_3 $this->bctx["sb"][3];

        
$in unpack("N*"$in);
        
$l $in[1];
        
$r $in[2];

        for (
$i 17$i 2$i-= 2) {
            
$l^= $p[$i];
            
$r^= ($sb_0[$l >> 24 0xff]  +
                  
$sb_1[$l >> 16 0xff]  ^
                  
$sb_2[$l >>  0xff]) +
                  
$sb_3[$l       0xff];

            
$r^= $p[$i 1];
            
$l^= ($sb_0[$r >> 24 0xff]  +
                  
$sb_1[$r >> 16 0xff]  ^
                  
$sb_2[$r >>  0xff]) +
                  
$sb_3[$r       0xff];
        }
        return 
pack("N*"$r $p[0], $l $p[1]);
    }

    
/**
     * Setup the performance-optimized function for de/encrypt()
     *
     * @see Crypt_Base::_setupInlineCrypt()
     * @access private
     */
    
function _setupInlineCrypt()
    {
        
$lambda_functions =& Crypt_Blowfish::_getLambdaFunctions();

        
// We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
        // (Currently, for Crypt_Blowfish, one generated $lambda_function cost on php5.5@32bit ~100kb unfreeable mem and ~180kb on php5.5@64bit)
        // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one.
        
$gen_hi_opt_code = (bool)( count($lambda_functions) < 10 );

        
// Generation of a unique hash for our generated code
        
$code_hash "Crypt_Blowfish, {$this->mode}";
        if (
$gen_hi_opt_code) {
            
$code_hash str_pad($code_hash32) . $this->_hashInlineCryptFunction($this->key);
        }

        if (!isset(
$lambda_functions[$code_hash])) {
            switch (
true) {
                case 
$gen_hi_opt_code:
                    
$p $this->bctx['p'];
                    
$init_crypt '
                        static $sb_0, $sb_1, $sb_2, $sb_3;
                        if (!$sb_0) {
                            $sb_0 = $self->bctx["sb"][0];
                            $sb_1 = $self->bctx["sb"][1];
                            $sb_2 = $self->bctx["sb"][2];
                            $sb_3 = $self->bctx["sb"][3];
                        }
                    '
;
                    break;
                default:
                    
$p   = array();
                    for (
$i 0$i 18; ++$i) {
                        
$p[] = '$p_' $i;
                    }
                    
$init_crypt '
                        list($sb_0, $sb_1, $sb_2, $sb_3) = $self->bctx["sb"];
                        list(' 
implode(','$p) . ') = $self->bctx["p"];

                    '
;
            }

            
// Generating encrypt code:
            
$encrypt_block '
                $in = unpack("N*", $in);
                $l = $in[1];
                $r = $in[2];
            '
;
            for (
$i 0$i 16$i+= 2) {
                
$encrypt_block.= '
                    $l^= ' 
$p[$i] . ';
                    $r^= ($sb_0[$l >> 24 & 0xff]  +
                          $sb_1[$l >> 16 & 0xff]  ^
                          $sb_2[$l >>  8 & 0xff]) +
                          $sb_3[$l       & 0xff];

                    $r^= ' 
$p[$i 1] . ';
                    $l^= ($sb_0[$r >> 24 & 0xff]  +
                          $sb_1[$r >> 16 & 0xff]  ^
                          $sb_2[$r >>  8 & 0xff]) +
                          $sb_3[$r       & 0xff];
                '
;
            }
            
$encrypt_block.= '
                $in = pack("N*",
                    $r ^ ' 
$p[17] . ',
                    $l ^ ' 
$p[16] . '
                );
            '
;

            
// Generating decrypt code:
            
$decrypt_block '
                $in = unpack("N*", $in);
                $l = $in[1];
                $r = $in[2];
            '
;

            for (
$i 17$i 2$i-= 2) {
                
$decrypt_block.= '
                    $l^= ' 
$p[$i] . ';
                    $r^= ($sb_0[$l >> 24 & 0xff]  +
                          $sb_1[$l >> 16 & 0xff]  ^
                          $sb_2[$l >>  8 & 0xff]) +
                          $sb_3[$l       & 0xff];

                    $r^= ' 
$p[$i 1] . ';
                    $l^= ($sb_0[$r >> 24 & 0xff]  +
                          $sb_1[$r >> 16 & 0xff]  ^
                          $sb_2[$r >>  8 & 0xff]) +
                          $sb_3[$r       & 0xff];
                '
;
            }

            
$decrypt_block.= '
                $in = pack("N*",
                    $r ^ ' 
$p[0] . ',
                    $l ^ ' 
$p[1] . '
                );
            '
;

            
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
                array(
                   
'init_crypt'    => $init_crypt,
                   
'init_encrypt'  => '',
                   
'init_decrypt'  => '',
                   
'encrypt_block' => $encrypt_block,
                   
'decrypt_block' => $decrypt_block
                
)
            );
        }
        
$this->inline_crypt $lambda_functions[$code_hash];
    }
}
Онлайн: 2
Реклама