當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Crypt_DES::get_lambda_functions方法代碼示例

本文整理匯總了PHP中Crypt_DES::get_lambda_functions方法的典型用法代碼示例。如果您正苦於以下問題:PHP Crypt_DES::get_lambda_functions方法的具體用法?PHP Crypt_DES::get_lambda_functions怎麽用?PHP Crypt_DES::get_lambda_functions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Crypt_DES的用法示例。


在下文中一共展示了Crypt_DES::get_lambda_functions方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: inline_crypt_setup

    /**
     * Creates performance-optimized function for de/encrypt(), storing it in $this->inline_crypt
     *
     * @param optional Integer $des_rounds (1 = DES[default], 3 = TribleDES)
     * @access private
     */
    function inline_crypt_setup($des_rounds = 1)
    {
        $lambda_functions =& Crypt_DES::get_lambda_functions();
        $block_size = 8;
        $mode = $this->mode;
        $code_hash = "{$mode},{$des_rounds}";
        if (!isset($lambda_functions[$code_hash])) {
            // Generating encrypt code:
            $ki = -1;
            $init_cryptBlock = '
                $shuffle  = $self->shuffle;
                $invipmap = $self->invipmap;
                $ipmap = $self->ipmap;
                $sbox1 = $self->sbox1;
                $sbox2 = $self->sbox2;
                $sbox3 = $self->sbox3;
                $sbox4 = $self->sbox4;
                $sbox5 = $self->sbox5;
                $sbox6 = $self->sbox6;
                $sbox7 = $self->sbox7;
                $sbox8 = $self->sbox8;
            ';
            $_cryptBlock = '$in = unpack("N*", $in);' . "\n";
            // Do the initial IP permutation.
            $_cryptBlock .= '
                $l  = $in[1];
                $r  = $in[2];
                $in = unpack("N*",
                    ($shuffle[$ipmap[ $r        & 0xFF]] & "\\x80\\x80\\x80\\x80\\x80\\x80\\x80\\x80") |
                    ($shuffle[$ipmap[($r >>  8) & 0xFF]] & "\\x40\\x40\\x40\\x40\\x40\\x40\\x40\\x40") |
                    ($shuffle[$ipmap[($r >> 16) & 0xFF]] & "\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20") |
                    ($shuffle[$ipmap[($r >> 24) & 0xFF]] & "\\x10\\x10\\x10\\x10\\x10\\x10\\x10\\x10") |
                    ($shuffle[$ipmap[ $l        & 0xFF]] & "\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08") |
                    ($shuffle[$ipmap[($l >>  8) & 0xFF]] & "\\x04\\x04\\x04\\x04\\x04\\x04\\x04\\x04") |
                    ($shuffle[$ipmap[($l >> 16) & 0xFF]] & "\\x02\\x02\\x02\\x02\\x02\\x02\\x02\\x02") |
                    ($shuffle[$ipmap[($l >> 24) & 0xFF]] & "\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01")
                );

                ' . '' . '
                $l = $in[1];
                $r = $in[2];
            ';
            $l = 'l';
            $r = 'r';
            for ($des_round = 0; $des_round < $des_rounds; ++$des_round) {
                // Perform the 16 steps.
                // start of "the Feistel (F) function" - see the following URL:
                // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
                // Merge key schedule.
                for ($i = 0; $i < 8; ++$i) {
                    $_cryptBlock .= '
                        $b1 = (($' . $r . ' >>  3) & 0x1FFFFFFF)  ^ ($' . $r . ' << 29) ^ $k_' . ++$ki . ';
                        $b2 = (($' . $r . ' >> 31) & 0x00000001)  ^ ($' . $r . ' <<  1) ^ $k_' . ++$ki . ';
                        $' . $l . '  = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
                              $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
                              $sbox5[($b1 >>  8) & 0x3F] ^ $sbox6[($b2 >>  8) & 0x3F] ^
                              $sbox7[ $b1        & 0x3F] ^ $sbox8[ $b2        & 0x3F] ^ $' . $l . ';

                        $b1 = (($' . $l . ' >>  3) & 0x1FFFFFFF)  ^ ($' . $l . ' << 29) ^ $k_' . ++$ki . ';
                        $b2 = (($' . $l . ' >> 31) & 0x00000001)  ^ ($' . $l . ' <<  1) ^ $k_' . ++$ki . ';
                        $' . $r . '  = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
                              $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
                              $sbox5[($b1 >>  8) & 0x3F] ^ $sbox6[($b2 >>  8) & 0x3F] ^
                              $sbox7[ $b1        & 0x3F] ^ $sbox8[ $b2        & 0x3F] ^ $' . $r . ';
                    ';
                }
                // Last step should not permute L & R.
                $t = $l;
                $l = $r;
                $r = $t;
            }
            // Perform the inverse IP permutation.
            $_cryptBlock .= '$in = (
                    ($shuffle[$invipmap[($' . $r . ' >> 24) & 0xFF]] & "\\x80\\x80\\x80\\x80\\x80\\x80\\x80\\x80") |
                    ($shuffle[$invipmap[($' . $l . ' >> 24) & 0xFF]] & "\\x40\\x40\\x40\\x40\\x40\\x40\\x40\\x40") |
                    ($shuffle[$invipmap[($' . $r . ' >> 16) & 0xFF]] & "\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20") |
                    ($shuffle[$invipmap[($' . $l . ' >> 16) & 0xFF]] & "\\x10\\x10\\x10\\x10\\x10\\x10\\x10\\x10") |
                    ($shuffle[$invipmap[($' . $r . ' >>  8) & 0xFF]] & "\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08") |
                    ($shuffle[$invipmap[($' . $l . ' >>  8) & 0xFF]] & "\\x04\\x04\\x04\\x04\\x04\\x04\\x04\\x04") |
                    ($shuffle[$invipmap[ $' . $r . '        & 0xFF]] & "\\x02\\x02\\x02\\x02\\x02\\x02\\x02\\x02") |
                    ($shuffle[$invipmap[ $' . $l . '        & 0xFF]] & "\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01")
                );
            ';
            // Generating mode of operation code:
            switch ($mode) {
                case CRYPT_DES_MODE_ECB:
                    $encrypt = $init_cryptBlock . '
                        extract($self->keys[CRYPT_DES_ENCRYPT_1DIM],  EXTR_PREFIX_ALL, "k");
                        $ciphertext = "";
                        $plaintext_len = strlen($text);

                        for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                            $in = substr($text, $i, ' . $block_size . ');
                            ' . $_cryptBlock . '
//.........這裏部分代碼省略.........
開發者ID:sspaeti,項目名稱:ImpressPages,代碼行數:101,代碼來源:DES.php

示例2: inline_crypt_setup

    function inline_crypt_setup($des_rounds = 1)
    {
        $lambda_functions =& Crypt_DES::get_lambda_functions();
        $block_size = 8;
        $mode = $this->mode;

        $code_hash = "$mode,$des_rounds";

        if (!isset($lambda_functions[$code_hash])) {
                        $ki = -1;
            $init_cryptBlock = '
                $shuffle  = $self->shuffle;
                $invipmap = $self->invipmap;
                $ipmap = $self->ipmap;
                $sbox1 = $self->sbox1;
                $sbox2 = $self->sbox2;
                $sbox3 = $self->sbox3;
                $sbox4 = $self->sbox4;
                $sbox5 = $self->sbox5;
                $sbox6 = $self->sbox6;
                $sbox7 = $self->sbox7;
                $sbox8 = $self->sbox8;
            ';

            $_cryptBlock = '$in = unpack("N*", $in);'."\n";
                        $_cryptBlock .= '
                $l  = $in[1];
                $r  = $in[2];
                $in = unpack("N*",
                    ($shuffle[$ipmap[ $r        & 0xFF]] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
                    ($shuffle[$ipmap[($r >>  8) & 0xFF]] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
                    ($shuffle[$ipmap[($r >> 16) & 0xFF]] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
                    ($shuffle[$ipmap[($r >> 24) & 0xFF]] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
                    ($shuffle[$ipmap[ $l        & 0xFF]] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
                    ($shuffle[$ipmap[($l >>  8) & 0xFF]] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
                    ($shuffle[$ipmap[($l >> 16) & 0xFF]] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
                    ($shuffle[$ipmap[($l >> 24) & 0xFF]] & "\x01\x01\x01\x01\x01\x01\x01\x01")
                );

                '.''  .'
                $l = $in[1];
                $r = $in[2];
            ';

            $l = 'l';
            $r = 'r';
            for ($des_round = 0; $des_round < $des_rounds; ++$des_round) {
                                                                                for ($i = 0; $i < 8; ++$i) {
                    $_cryptBlock .= '
                        $b1 = (($' . $r . ' >>  3) & 0x1FFFFFFF)  ^ ($' . $r . ' << 29) ^ $k_'.(++$ki).';
                        $b2 = (($' . $r . ' >> 31) & 0x00000001)  ^ ($' . $r . ' <<  1) ^ $k_'.(++$ki).';
                        $' . $l . '  = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
                              $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
                              $sbox5[($b1 >>  8) & 0x3F] ^ $sbox6[($b2 >>  8) & 0x3F] ^
                              $sbox7[ $b1        & 0x3F] ^ $sbox8[ $b2        & 0x3F] ^ $' . $l . ';

                        $b1 = (($' . $l . ' >>  3) & 0x1FFFFFFF)  ^ ($' . $l . ' << 29) ^ $k_'.(++$ki).';
                        $b2 = (($' . $l . ' >> 31) & 0x00000001)  ^ ($' . $l . ' <<  1) ^ $k_'.(++$ki).';
                        $' . $r . '  = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
                              $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
                              $sbox5[($b1 >>  8) & 0x3F] ^ $sbox6[($b2 >>  8) & 0x3F] ^
                              $sbox7[ $b1        & 0x3F] ^ $sbox8[ $b2        & 0x3F] ^ $' . $r . ';
                    ';
                }

                                $t = $l;
                $l = $r;
                $r = $t;
            }

                        $_cryptBlock .= '$in = (
                    ($shuffle[$invipmap[($' . $r . ' >> 24) & 0xFF]] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
                    ($shuffle[$invipmap[($' . $l . ' >> 24) & 0xFF]] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
                    ($shuffle[$invipmap[($' . $r . ' >> 16) & 0xFF]] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
                    ($shuffle[$invipmap[($' . $l . ' >> 16) & 0xFF]] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
                    ($shuffle[$invipmap[($' . $r . ' >>  8) & 0xFF]] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
                    ($shuffle[$invipmap[($' . $l . ' >>  8) & 0xFF]] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
                    ($shuffle[$invipmap[ $' . $r . '        & 0xFF]] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
                    ($shuffle[$invipmap[ $' . $l . '        & 0xFF]] & "\x01\x01\x01\x01\x01\x01\x01\x01")
                );
            ';

                        switch ($mode) {
                case CRYPT_DES_MODE_ECB:
                    $encrypt = $init_cryptBlock . '
                        extract($self->keys[CRYPT_DES_ENCRYPT_1DIM],  EXTR_PREFIX_ALL, "k");
                        $ciphertext = "";
                        $plaintext_len = strlen($text);

                        for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
                            $in = substr($text, $i, '.$block_size.');
                            '.$_cryptBlock.'
                            $ciphertext.= $in;
                        }

                        return $ciphertext;
                        ';

                    $decrypt = $init_cryptBlock . '
                        extract($self->keys[CRYPT_DES_DECRYPT_1DIM],  EXTR_PREFIX_ALL, "k");
//.........這裏部分代碼省略.........
開發者ID:pf5512,項目名稱:phpstudy,代碼行數:101,代碼來源:Crypt_DES.php


注:本文中的Crypt_DES::get_lambda_functions方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。