Esempio n. 1
0
 /**
  * @return JoDES
  */
 public static function share()
 {
     if (is_null(self::$_instance)) {
         self::$_instance = new DES();
     }
     return self::$_instance;
 }
Esempio n. 2
0
function testDesParam()
{
    $deskey = "AAacM12+";
    $des = DES::share("android");
    $desp = $des->encode('a=11&b=22&c=33', $deskey);
    my_log($desp);
    $param = PDU($desp, "android", "{$deskey}");
    my_log($param);
}
Esempio n. 3
0
 public function __construct()
 {
     $config = (include "config.php");
     $this->mdkey = $config['mdkey'];
     $this->deskey = $config['deskey'];
     $this->log = new remoteLog();
     $this->curl = new remoteCurl();
     $this->db = new remoteDb();
     $this->des = DES::share();
 }
Esempio n. 4
0
 /**
  * 解析用DES加密的字符串, 返回 key => value 数组。
  * @param type $desparam
  * @param type $key
  * @return type
  */
 static function parse_des_urlparam($desparam, $os, $key)
 {
     $des = DES::share($os);
     $params = $des->decode($desparam, $key);
     $arrParam = array();
     $arrTmp = explode("&", $params);
     foreach ($arrTmp as $p) {
         $item = explode("=", $p);
         if (count($item) == 2) {
             $arrParam[$item[0]] = $item[1];
         }
     }
     $arrParam = Add_S($arrParam);
     return $arrParam;
 }
Esempio n. 5
0
 function getData($param)
 {
     include_once 'module/myaccount/config.php';
     // $wsdlURL = "http://gboss.id5.cn/services/QueryValidatorServices?wsdl";
     /*$partner = "hongzhiniang123";
     		$partnerPW = "hongzhiniang123_4*ds6UOF";
     
     		$Key = "12345678";
     		$iv = "12345678";*/
     $DES = new DES($Key, $iv);
     //$wsdlURL = "http://gboss.id5.cn/services/QueryValidatorServices?wsdl";
     // echo $wsdlURL.'and'.$partner.'and'.$partnerPW;exit;
     try {
         //var_dump(file_get_contents($wsdlURL));exit;
         if (!@file_get_contents($wsdlURL)) {
             throw new SoapFault('Server', 'No WSDL found at ' . $wsdlURL);
         }
         $soap = new SoapClient($wsdlURL);
         //$soap->xml_encoding = 'UTF-8';
         //$client = new SoapClient($ws, array('proxy_host'     => "113.140.8.202",
         //'proxy_port'     => 9682));
         /*} catch ( Exception $e ) {
         		return "Linkerror";*/
     } catch (SoapFault $fault) {
         //return "Fault! code:".",".$fault->faultcode.", string: ".",".$fault->faultstring;exit;
         MooMessage("验证失败,请您重新验证@!", "index.php?n=myaccount&h=smsindex", '01');
     }
     //var_dump ( $soap->__getTypes () );
     //todo 加密数据
     $partner = $DES->encrypt($partner);
     $partnerPW = $DES->encrypt($partnerPW);
     $type = $DES->encrypt($this->type);
     //先将中文转码
     $param = mb_convert_encoding($param, "GBK", "UTF-8");
     $param = $DES->encrypt($param);
     $params = array("userName_" => $partner, "password_" => $partnerPW, "type_" => $type, "param_" => $param);
     //请求查询
     $data = $soap->querySingle($params);
     // todo 解密数据
     $resultXML = $DES->decrypt($data->querySingleReturn);
     $resultXML = mb_convert_encoding($resultXML, "UTF-8", "GBK");
     return $resultXML;
 }
Esempio n. 6
0
function DES($osTag, $debug = false)
{
    if ($debug == true) {
        return DES::share("debug");
    }
    return DES::share($osTag);
}
Esempio n. 7
0
 /**
  * Creates the key schedule
  *
  * @see DES::_setupKey()
  * @see Base::_setupKey()
  * @access private
  */
 function _setupKey()
 {
     switch (true) {
         // if $key <= 64bits we configure our internal pure-php cipher engine
         // to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
         case strlen($this->key) <= 8:
             $this->des_rounds = 1;
             break;
             // otherwise, if $key > 64bits, we configure our engine to work as 3DES.
         // otherwise, if $key > 64bits, we configure our engine to work as 3DES.
         default:
             $this->des_rounds = 3;
             // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
             if ($this->mode_3cbc) {
                 $this->des[0]->_setupKey();
                 $this->des[1]->_setupKey();
                 $this->des[2]->_setupKey();
                 // because $des[0-2] will, now, do all the work we can return here
                 // not need unnecessary stress parent::_setupKey() with our, now unused, $key.
                 return;
             }
     }
     // setup our key
     parent::_setupKey();
 }
Esempio n. 8
0
        }
        /*         * ****************************** 16 iteration **** */
        for ($l = 0; $l < 32; $l++) {
            $right32_bit[$l] = $Data_B[33 + $l];
        }
        self::Vect_Permutation($Data_B, 32, self::$E, 48, 33);
        if ($cryp_decrypt == 0) {
            self::_Xor($Data_B, self::$Ki[16], 48, 33, 1);
        } else {
            self::_Xor($Data_B, self::$Ki[1], 48, 33, 1);
        }
        self::S_Box_Calc($Data_B, 33);
        self::Vect_Permutation($Data_B, 32, self::$PP, 32, 33);
        self::_Xor($Data_B, $Data_B, 32, 1, 33);
        for ($j = 0; $j < 32; $j++) {
            $Data_B[33 + $j] = $right32_bit[$j];
        }
        self::Vect_Permutation($Data_B, 64, self::$IPinv, 64, 1);
        self::Bin_to_Hex($DES_RESULT, $Data_B, 1);
    }
}
$DESKey = "abcdefgh";
$Data = "01234567890";
$Res = array();
DES::function_des(0, $Data, $DESKey, $Res);
$des2 = mcrypt_encrypt(MCRYPT_DES, $DESKey, $Data, MCRYPT_MODE_ECB);
$ascii = array();
for ($i = 0; $i < strlen($des2); $i++) {
    $ascii[] = ord($des2[$i]);
}
var_dump($Res, $ascii);
Esempio n. 9
0
<?php

include 'db_config.php';
require 'anzhi_class_desc.php';
$data = $_POST;
if (!isset($data['data'])) {
    return 'failed';
}
$des = new DES();
$code = $des->decrypt($data['data']);
if (!isset($code) || empty($code)) {
    return 'failed';
}
//$code = 'callback is coming';
//phplog($code);
$response = json_decode($code, true);
if (!isset($response)) {
    return 'failed';
}
$uid = $response['uid'];
$orderId = $response['orderId'];
$orderAmount = $response['orderAmount'];
$orderTime = $response['orderTime'];
$orderAccount = $response['orderAccount'];
$code = $response['code'];
$payAmount = $response['payAmount'];
$cpInfo = $response['cpInfo'];
$notifyTime = $response['notifyTime'];
$memo = $response['memo'];
$mysql_connect = mysql_connect($db_ip, $db_user, $db_pw);
if (!$mysql_connect) {
Esempio n. 10
0
 /**
  * Break a public or private key down into its constituant components
  *
  * @access private
  * @see _convertPublicKey()
  * @see _convertPrivateKey()
  * @param String $key
  * @param Integer $type
  * @return Array
  */
 function _parseKey($key, $type)
 {
     if ($type != self::PUBLIC_FORMAT_RAW && !is_string($key)) {
         return false;
     }
     switch ($type) {
         case self::PUBLIC_FORMAT_RAW:
             if (!is_array($key)) {
                 return false;
             }
             $components = array();
             switch (true) {
                 case isset($key['e']):
                     $components['publicExponent'] = $key['e']->copy();
                     break;
                 case isset($key['exponent']):
                     $components['publicExponent'] = $key['exponent']->copy();
                     break;
                 case isset($key['publicExponent']):
                     $components['publicExponent'] = $key['publicExponent']->copy();
                     break;
                 case isset($key[0]):
                     $components['publicExponent'] = $key[0]->copy();
             }
             switch (true) {
                 case isset($key['n']):
                     $components['modulus'] = $key['n']->copy();
                     break;
                 case isset($key['modulo']):
                     $components['modulus'] = $key['modulo']->copy();
                     break;
                 case isset($key['modulus']):
                     $components['modulus'] = $key['modulus']->copy();
                     break;
                 case isset($key[1]):
                     $components['modulus'] = $key[1]->copy();
             }
             return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false;
         case self::PRIVATE_FORMAT_PKCS1:
         case self::PRIVATE_FORMAT_PKCS8:
         case self::PUBLIC_FORMAT_PKCS1:
             /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is
                "outside the scope" of PKCS#1.  PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to
                protect private keys, however, that's not what OpenSSL* does.  OpenSSL protects private keys by adding
                two new "fields" to the key - DEK-Info and Proc-Type.  These fields are discussed here:
                http://tools.ietf.org/html/rfc1421#section-4.6.1.1
                http://tools.ietf.org/html/rfc1421#section-4.6.1.3
                DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell.
                DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation
                function.  As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's
                own implementation.  ie. the implementation *is* the standard and any bugs that may exist in that
                implementation are part of the standard, as well.
                * OpenSSL is the de facto standard.  It's utilized by OpenSSH and other projects */
             if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
                 $iv = pack('H*', trim($matches[2]));
                 $symkey = pack('H*', md5($this->password . substr($iv, 0, 8)));
                 // symkey is short for symmetric key
                 $symkey .= pack('H*', md5($symkey . $this->password . substr($iv, 0, 8)));
                 // remove the Proc-Type / DEK-Info sections as they're no longer needed
                 $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
                 $ciphertext = $this->_extractBER($key);
                 if ($ciphertext === false) {
                     $ciphertext = $key;
                 }
                 switch ($matches[1]) {
                     case 'AES-256-CBC':
                         $crypto = new AES();
                         break;
                     case 'AES-128-CBC':
                         $symkey = substr($symkey, 0, 16);
                         $crypto = new AES();
                         break;
                     case 'DES-EDE3-CFB':
                         $crypto = new TripleDES(Base::MODE_CFB);
                         break;
                     case 'DES-EDE3-CBC':
                         $symkey = substr($symkey, 0, 24);
                         $crypto = new TripleDES();
                         break;
                     case 'DES-CBC':
                         $crypto = new DES();
                         break;
                     default:
                         return false;
                 }
                 $crypto->setKey($symkey);
                 $crypto->setIV($iv);
                 $decoded = $crypto->decrypt($ciphertext);
             } else {
                 $decoded = $this->_extractBER($key);
             }
             if ($decoded !== false) {
                 $key = $decoded;
             }
             $components = array();
             if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
                 return false;
             }
             if ($this->_decodeLength($key) != strlen($key)) {
                 return false;
             }
             $tag = ord($this->_string_shift($key));
             /* intended for keys for which OpenSSL's asn1parse returns the following:
                 0:d=0  hl=4 l= 631 cons: SEQUENCE
                 4:d=1  hl=2 l=   1 prim:  INTEGER           :00
                 7:d=1  hl=2 l=  13 cons:  SEQUENCE
                 9:d=2  hl=2 l=   9 prim:   OBJECT            :rsaEncryption
                20:d=2  hl=2 l=   0 prim:   NULL
                22:d=1  hl=4 l= 609 prim:  OCTET STRING
                ie. PKCS8 keys*/
             if ($tag == self::ASN1_INTEGER && substr($key, 0, 3) == "0") {
                 $this->_string_shift($key, 3);
                 $tag = self::ASN1_SEQUENCE;
             }
             if ($tag == self::ASN1_SEQUENCE) {
                 $temp = $this->_string_shift($key, $this->_decodeLength($key));
                 if (ord($this->_string_shift($temp)) != self::ASN1_OBJECT) {
                     return false;
                 }
                 $length = $this->_decodeLength($temp);
                 switch ($this->_string_shift($temp, $length)) {
                     case "*†H†÷\r":
                         // rsaEncryption
                         break;
                     case "*†H†÷\r":
                         // pbeWithMD5AndDES-CBC
                         /*
                            PBEParameter ::= SEQUENCE {
                                salt OCTET STRING (SIZE(8)),
                                iterationCount INTEGER }
                         */
                         if (ord($this->_string_shift($temp)) != self::ASN1_SEQUENCE) {
                             return false;
                         }
                         if ($this->_decodeLength($temp) != strlen($temp)) {
                             return false;
                         }
                         $this->_string_shift($temp);
                         // assume it's an octet string
                         $salt = $this->_string_shift($temp, $this->_decodeLength($temp));
                         if (ord($this->_string_shift($temp)) != self::ASN1_INTEGER) {
                             return false;
                         }
                         $this->_decodeLength($temp);
                         list(, $iterationCount) = unpack('N', str_pad($temp, 4, chr(0), STR_PAD_LEFT));
                         $this->_string_shift($key);
                         // assume it's an octet string
                         $length = $this->_decodeLength($key);
                         if (strlen($key) != $length) {
                             return false;
                         }
                         $crypto = new DES();
                         $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
                         $key = $crypto->decrypt($key);
                         if ($key === false) {
                             return false;
                         }
                         return $this->_parseKey($key, self::PRIVATE_FORMAT_PKCS1);
                     default:
                         return false;
                 }
                 /* intended for keys for which OpenSSL's asn1parse returns the following:
                     0:d=0  hl=4 l= 290 cons: SEQUENCE
                     4:d=1  hl=2 l=  13 cons:  SEQUENCE
                     6:d=2  hl=2 l=   9 prim:   OBJECT            :rsaEncryption
                    17:d=2  hl=2 l=   0 prim:   NULL
                    19:d=1  hl=4 l= 271 prim:  BIT STRING */
                 $tag = ord($this->_string_shift($key));
                 // skip over the BIT STRING / OCTET STRING tag
                 $this->_decodeLength($key);
                 // skip over the BIT STRING / OCTET STRING length
                 // "The initial octet shall encode, as an unsigned binary integer wtih bit 1 as the least significant bit, the number of
                 //  unused bits in the final subsequent octet. The number shall be in the range zero to seven."
                 //  -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf (section 8.6.2.2)
                 if ($tag == self::ASN1_BITSTRING) {
                     $this->_string_shift($key);
                 }
                 if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
                     return false;
                 }
                 if ($this->_decodeLength($key) != strlen($key)) {
                     return false;
                 }
                 $tag = ord($this->_string_shift($key));
             }
             if ($tag != self::ASN1_INTEGER) {
                 return false;
             }
             $length = $this->_decodeLength($key);
             $temp = $this->_string_shift($key, $length);
             if (strlen($temp) != 1 || ord($temp) > 2) {
                 $components['modulus'] = new BigInteger($temp, 256);
                 $this->_string_shift($key);
                 // skip over self::ASN1_INTEGER
                 $length = $this->_decodeLength($key);
                 $components[$type == self::PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
                 return $components;
             }
             if (ord($this->_string_shift($key)) != self::ASN1_INTEGER) {
                 return false;
             }
             $length = $this->_decodeLength($key);
             $components['modulus'] = new BigInteger($this->_string_shift($key, $length), 256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['publicExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['privateExponent'] = new BigInteger($this->_string_shift($key, $length), 256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['primes'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['exponents'] = array(1 => new BigInteger($this->_string_shift($key, $length), 256));
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($key, $length), 256));
             if (!empty($key)) {
                 if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
                     return false;
                 }
                 $this->_decodeLength($key);
                 while (!empty($key)) {
                     if (ord($this->_string_shift($key)) != self::ASN1_SEQUENCE) {
                         return false;
                     }
                     $this->_decodeLength($key);
                     $key = substr($key, 1);
                     $length = $this->_decodeLength($key);
                     $components['primes'][] = new BigInteger($this->_string_shift($key, $length), 256);
                     $this->_string_shift($key);
                     $length = $this->_decodeLength($key);
                     $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), 256);
                     $this->_string_shift($key);
                     $length = $this->_decodeLength($key);
                     $components['coefficients'][] = new BigInteger($this->_string_shift($key, $length), 256);
                 }
             }
             return $components;
         case self::PUBLIC_FORMAT_OPENSSH:
             $parts = explode(' ', $key, 3);
             $key = isset($parts[1]) ? base64_decode($parts[1]) : false;
             if ($key === false) {
                 return false;
             }
             $comment = isset($parts[2]) ? $parts[2] : false;
             $cleanup = substr($key, 0, 11) == "ssh-rsa";
             if (strlen($key) <= 4) {
                 return false;
             }
             extract(unpack('Nlength', $this->_string_shift($key, 4)));
             $publicExponent = new BigInteger($this->_string_shift($key, $length), -256);
             if (strlen($key) <= 4) {
                 return false;
             }
             extract(unpack('Nlength', $this->_string_shift($key, 4)));
             $modulus = new BigInteger($this->_string_shift($key, $length), -256);
             if ($cleanup && strlen($key)) {
                 if (strlen($key) <= 4) {
                     return false;
                 }
                 extract(unpack('Nlength', $this->_string_shift($key, 4)));
                 $realModulus = new BigInteger($this->_string_shift($key, $length), -256);
                 return strlen($key) ? false : array('modulus' => $realModulus, 'publicExponent' => $modulus, 'comment' => $comment);
             } else {
                 return strlen($key) ? false : array('modulus' => $modulus, 'publicExponent' => $publicExponent, 'comment' => $comment);
             }
             // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
             // http://en.wikipedia.org/wiki/XML_Signature
         // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
         // http://en.wikipedia.org/wiki/XML_Signature
         case self::PRIVATE_FORMAT_XML:
         case self::PUBLIC_FORMAT_XML:
             $this->components = array();
             $xml = xml_parser_create('UTF-8');
             xml_set_object($xml, $this);
             xml_set_element_handler($xml, '_start_element_handler', '_stop_element_handler');
             xml_set_character_data_handler($xml, '_data_handler');
             // add <xml></xml> to account for "dangling" tags like <BitStrength>...</BitStrength> that are sometimes added
             if (!xml_parse($xml, '<xml>' . $key . '</xml>')) {
                 return false;
             }
             return isset($this->components['modulus']) && isset($this->components['publicExponent']) ? $this->components : false;
             // from PuTTY's SSHPUBK.C
         // from PuTTY's SSHPUBK.C
         case self::PRIVATE_FORMAT_PUTTY:
             $components = array();
             $key = preg_split('#\\r\\n|\\r|\\n#', $key);
             $type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0]));
             if ($type != 'ssh-rsa') {
                 return false;
             }
             $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1]));
             $comment = trim(preg_replace('#Comment: (.+)#', '$1', $key[2]));
             $publicLength = trim(preg_replace('#Public-Lines: (\\d+)#', '$1', $key[3]));
             $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
             $public = substr($public, 11);
             extract(unpack('Nlength', $this->_string_shift($public, 4)));
             $components['publicExponent'] = new BigInteger($this->_string_shift($public, $length), -256);
             extract(unpack('Nlength', $this->_string_shift($public, 4)));
             $components['modulus'] = new BigInteger($this->_string_shift($public, $length), -256);
             $privateLength = trim(preg_replace('#Private-Lines: (\\d+)#', '$1', $key[$publicLength + 4]));
             $private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
             switch ($encryption) {
                 case 'aes256-cbc':
                     $symkey = '';
                     $sequence = 0;
                     while (strlen($symkey) < 32) {
                         $temp = pack('Na*', $sequence++, $this->password);
                         $symkey .= pack('H*', sha1($temp));
                     }
                     $symkey = substr($symkey, 0, 32);
                     $crypto = new AES();
             }
             if ($encryption != 'none') {
                 $crypto->setKey($symkey);
                 $crypto->disablePadding();
                 $private = $crypto->decrypt($private);
                 if ($private === false) {
                     return false;
                 }
             }
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             if (strlen($private) < $length) {
                 return false;
             }
             $components['privateExponent'] = new BigInteger($this->_string_shift($private, $length), -256);
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             if (strlen($private) < $length) {
                 return false;
             }
             $components['primes'] = array(1 => new BigInteger($this->_string_shift($private, $length), -256));
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             if (strlen($private) < $length) {
                 return false;
             }
             $components['primes'][] = new BigInteger($this->_string_shift($private, $length), -256);
             $temp = $components['primes'][1]->subtract($this->one);
             $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp));
             $temp = $components['primes'][2]->subtract($this->one);
             $components['exponents'][] = $components['publicExponent']->modInverse($temp);
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             if (strlen($private) < $length) {
                 return false;
             }
             $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($private, $length), -256));
             return $components;
     }
 }
Esempio n. 11
0
 /**
  * Sets the internal crypt engine
  *
  * @see \phpseclib\Crypt\Base::__construct()
  * @see \phpseclib\Crypt\Base::setPreferredEngine()
  * @param int $engine
  * @access public
  * @return int
  */
 function setPreferredEngine($engine)
 {
     if ($this->mode_3cbc) {
         $this->des[0]->setPreferredEngine($engine);
         $this->des[1]->setPreferredEngine($engine);
         $this->des[2]->setPreferredEngine($engine);
     }
     return parent::setPreferredEngine($engine);
 }
Esempio n. 12
0
 /**
  * Encrypt a block of data using the supplied string key
  *
  * Note that the supplied data should be the same size as the block size of
  * the cipher being used.
  *
  * @param string $data The data to encrypt
  *
  * @return string The result encrypted data
  */
 protected function encryptBlockData($data)
 {
     $key = $this->key;
     $this->key = substr($key, 0, 8);
     $this->initialize();
     $data = parent::encryptBlockData($data);
     $this->key = substr($key, 8, 8);
     $this->initialize();
     $data = parent::decryptBlockData($data);
     $this->key = substr($key, 16, 8);
     $this->initialize();
     $data = parent::encryptBlockData($data);
     $this->key = $key;
     return $data;
 }
Esempio n. 13
0
    }
}
function getSignature($params, $secret)
{
    $str = '';
    ksort($params);
    foreach ($params as $k => $v) {
        if ($k == 'sign') {
            continue;
        }
        $str .= "{$k}={$v}";
    }
    $str .= $secret;
    return md5($str);
}
function basic_en($arr)
{
    return base64_encode(json_encode($arr));
}
function basic_de($str)
{
    return json_decode(base64_decode($str), true);
}
$des_key = 'YdqJnu+wrl0vDeb++OzZMpcWq9l1qEPN';
$desObj = new DES($des_key);
$data = array("name" => 'zhenwei', "age" => '23', "sex" => 'mail');
echo "DES_EN:" . ($en = $desObj->encrypt(basic_en($data)));
echo PHP_EOL;
$des_data = basic_de($desObj->decrypt($en));
echo "DES_DE:" . PHP_EOL;
print_r($des_data);
Esempio n. 14
0
 /**
  * Break a public or private key down into its constituant components
  *
  * @access private
  * @see _convertPublicKey()
  * @see _convertPrivateKey()
  * @param String $key
  * @param Integer $type
  * @return Array
  */
 function _parseKey($key, $type)
 {
     switch ($type) {
         case RSA_PUBLIC_FORMAT_RAW:
             if (!is_array($key)) {
                 return false;
             }
             $components = array();
             switch (true) {
                 case isset($key['e']):
                     $components['publicExponent'] = $key['e']->copy();
                     break;
                 case isset($key['exponent']):
                     $components['publicExponent'] = $key['exponent']->copy();
                     break;
                 case isset($key['publicExponent']):
                     $components['publicExponent'] = $key['publicExponent']->copy();
                     break;
                 case isset($key[0]):
                     $components['publicExponent'] = $key[0]->copy();
             }
             switch (true) {
                 case isset($key['n']):
                     $components['modulus'] = $key['n']->copy();
                     break;
                 case isset($key['modulo']):
                     $components['modulus'] = $key['modulo']->copy();
                     break;
                 case isset($key['modulus']):
                     $components['modulus'] = $key['modulus']->copy();
                     break;
                 case isset($key[1]):
                     $components['modulus'] = $key[1]->copy();
             }
             return $components;
         case RSA_PRIVATE_FORMAT_PKCS1:
         case RSA_PUBLIC_FORMAT_PKCS1:
             /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is
                                "outside the scope" of PKCS#1.  PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to
                                protect private keys, however, that's not what OpenSSL* does.  OpenSSL protects private keys by adding
                                two new "fields" to the key - DEK-Info and Proc-Type.  These fields are discussed here:
             
                                http://tools.ietf.org/html/rfc1421#section-4.6.1.1
                                http://tools.ietf.org/html/rfc1421#section-4.6.1.3
             
                                DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell.
                                DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation
                                function.  As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's
                                own implementation.  ie. the implementation *is* the standard and any bugs that may exist in that 
                                implementation are part of the standard, as well.
             
                                * OpenSSL is the de facto standard.  It's utilized by OpenSSH and other projects */
             if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
                 $iv = pack('H*', trim($matches[2]));
                 $symkey = pack('H*', md5($this->password . substr($iv, 0, 8)));
                 // symkey is short for symmetric key
                 $symkey .= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
                 $ciphertext = preg_replace('#.+(\\r|\\n|\\r\\n)\\1|[\\r\\n]|-.+-#s', '', $key);
                 $ciphertext = preg_match('#^[a-zA-Z\\d/+]*={0,2}$#', $ciphertext) ? base64_decode($ciphertext) : false;
                 if ($ciphertext === false) {
                     $ciphertext = $key;
                 }
                 switch ($matches[1]) {
                     case 'AES-128-CBC':
                         if (!class_exists('AES')) {
                             require_once 'Crypt/AES.php';
                         }
                         $symkey = substr($symkey, 0, 16);
                         $crypto = new AES();
                         break;
                     case 'DES-EDE3-CFB':
                         if (!class_exists('TripleDES')) {
                             require_once 'Crypt/TripleDES.php';
                         }
                         $crypto = new TripleDES(DES_MODE_CFB);
                         break;
                     case 'DES-EDE3-CBC':
                         if (!class_exists('TripleDES')) {
                             require_once 'Crypt/TripleDES.php';
                         }
                         $crypto = new TripleDES();
                         break;
                     case 'DES-CBC':
                         if (!class_exists('DES')) {
                             require_once 'Crypt/DES.php';
                         }
                         $crypto = new DES();
                         break;
                     default:
                         return false;
                 }
                 $crypto->setKey($symkey);
                 $crypto->setIV($iv);
                 $decoded = $crypto->decrypt($ciphertext);
             } else {
                 $decoded = preg_replace('#-.+-|[\\r\\n]#', '', $key);
                 $decoded = preg_match('#^[a-zA-Z\\d/+]*={0,2}$#', $decoded) ? base64_decode($decoded) : false;
             }
             if ($decoded !== false) {
                 $key = $decoded;
             }
             $components = array();
             if (ord($this->_string_shift($key)) != RSA_ASN1_SEQUENCE) {
                 return false;
             }
             if ($this->_decodeLength($key) != strlen($key)) {
                 return false;
             }
             $tag = ord($this->_string_shift($key));
             if ($tag == RSA_ASN1_SEQUENCE) {
                 /* intended for keys for which OpenSSL's asn1parse returns the following:
                 
                                         0:d=0  hl=4 l= 290 cons: SEQUENCE
                                         4:d=1  hl=2 l=  13 cons:  SEQUENCE
                                         6:d=2  hl=2 l=   9 prim:   OBJECT            :rsaEncryption
                                        17:d=2  hl=2 l=   0 prim:   NULL
                                        19:d=1  hl=4 l= 271 prim:  BIT STRING */
                 $this->_string_shift($key, $this->_decodeLength($key));
                 $this->_string_shift($key);
                 // skip over the BIT STRING tag
                 $this->_decodeLength($key);
                 // skip over the BIT STRING length
                 // "The initial octet shall encode, as an unsigned binary integer wtih bit 1 as the least significant bit, the number of
                 //  unused bits in teh final subsequent octet. The number shall be in the range zero to seven."
                 //  -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf (section 8.6.2.2)
                 $this->_string_shift($key);
                 if (ord($this->_string_shift($key)) != RSA_ASN1_SEQUENCE) {
                     return false;
                 }
                 if ($this->_decodeLength($key) != strlen($key)) {
                     return false;
                 }
                 $tag = ord($this->_string_shift($key));
             }
             if ($tag != RSA_ASN1_INTEGER) {
                 return false;
             }
             $length = $this->_decodeLength($key);
             $temp = $this->_string_shift($key, $length);
             if (strlen($temp) != 1 || ord($temp) > 2) {
                 $components['modulus'] = new BigInteger($temp, -256);
                 $this->_string_shift($key);
                 // skip over RSA_ASN1_INTEGER
                 $length = $this->_decodeLength($key);
                 $components[$type == RSA_PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new BigInteger($this->_string_shift($key, $length), -256);
                 return $components;
             }
             if (ord($this->_string_shift($key)) != RSA_ASN1_INTEGER) {
                 return false;
             }
             $length = $this->_decodeLength($key);
             $components['modulus'] = new BigInteger($this->_string_shift($key, $length), -256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['publicExponent'] = new BigInteger($this->_string_shift($key, $length), -256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['privateExponent'] = new BigInteger($this->_string_shift($key, $length), -256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['primes'] = array(1 => new BigInteger($this->_string_shift($key, $length), -256));
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['primes'][] = new BigInteger($this->_string_shift($key, $length), -256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['exponents'] = array(1 => new BigInteger($this->_string_shift($key, $length), -256));
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), -256);
             $this->_string_shift($key);
             $length = $this->_decodeLength($key);
             $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($key, $length), -256));
             if (!empty($key)) {
                 if (ord($this->_string_shift($key)) != RSA_ASN1_SEQUENCE) {
                     return false;
                 }
                 $this->_decodeLength($key);
                 while (!empty($key)) {
                     if (ord($this->_string_shift($key)) != RSA_ASN1_SEQUENCE) {
                         return false;
                     }
                     $this->_decodeLength($key);
                     $key = substr($key, 1);
                     $length = $this->_decodeLength($key);
                     $components['primes'][] = new BigInteger($this->_string_shift($key, $length), -256);
                     $this->_string_shift($key);
                     $length = $this->_decodeLength($key);
                     $components['exponents'][] = new BigInteger($this->_string_shift($key, $length), -256);
                     $this->_string_shift($key);
                     $length = $this->_decodeLength($key);
                     $components['coefficients'][] = new BigInteger($this->_string_shift($key, $length), -256);
                 }
             }
             return $components;
         case RSA_PUBLIC_FORMAT_OPENSSH:
             $key = base64_decode(preg_replace('#^ssh-rsa | .+$#', '', $key));
             if ($key === false) {
                 return false;
             }
             $cleanup = substr($key, 0, 11) == "ssh-rsa";
             extract(unpack('Nlength', $this->_string_shift($key, 4)));
             $publicExponent = new BigInteger($this->_string_shift($key, $length), -256);
             extract(unpack('Nlength', $this->_string_shift($key, 4)));
             $modulus = new BigInteger($this->_string_shift($key, $length), -256);
             if ($cleanup && strlen($key)) {
                 extract(unpack('Nlength', $this->_string_shift($key, 4)));
                 return array('modulus' => new BigInteger($this->_string_shift($key, $length), -256), 'publicExponent' => $modulus);
             } else {
                 return array('modulus' => $modulus, 'publicExponent' => $publicExponent);
             }
             // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
             // http://en.wikipedia.org/wiki/XML_Signature
         // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
         // http://en.wikipedia.org/wiki/XML_Signature
         case RSA_PRIVATE_FORMAT_XML:
         case RSA_PUBLIC_FORMAT_XML:
             $this->components = array();
             $xml = xml_parser_create('UTF-8');
             xml_set_object($xml, $this);
             xml_set_element_handler($xml, '_start_element_handler', '_stop_element_handler');
             xml_set_character_data_handler($xml, '_data_handler');
             if (!xml_parse($xml, $key)) {
                 return false;
             }
             return $this->components;
             // from PuTTY's SSHPUBK.C
         // from PuTTY's SSHPUBK.C
         case RSA_PRIVATE_FORMAT_PUTTY:
             $components = array();
             $key = preg_split('#\\r\\n|\\r|\\n#', $key);
             $type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0]));
             if ($type != 'ssh-rsa') {
                 return false;
             }
             $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1]));
             $publicLength = trim(preg_replace('#Public-Lines: (\\d+)#', '$1', $key[3]));
             $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
             $public = substr($public, 11);
             extract(unpack('Nlength', $this->_string_shift($public, 4)));
             $components['publicExponent'] = new BigInteger($this->_string_shift($public, $length), -256);
             extract(unpack('Nlength', $this->_string_shift($public, 4)));
             $components['modulus'] = new BigInteger($this->_string_shift($public, $length), -256);
             $privateLength = trim(preg_replace('#Private-Lines: (\\d+)#', '$1', $key[$publicLength + 4]));
             $private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
             switch ($encryption) {
                 case 'aes256-cbc':
                     if (!class_exists('AES')) {
                         require_once 'Crypt/AES.php';
                     }
                     $symkey = '';
                     $sequence = 0;
                     while (strlen($symkey) < 32) {
                         $temp = pack('Na*', $sequence++, $this->password);
                         $symkey .= pack('H*', sha1($temp));
                     }
                     $symkey = substr($symkey, 0, 32);
                     $crypto = new AES();
             }
             if ($encryption != 'none') {
                 $crypto->setKey($symkey);
                 $crypto->disablePadding();
                 $private = $crypto->decrypt($private);
                 if ($private === false) {
                     return false;
                 }
             }
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             $components['privateExponent'] = new BigInteger($this->_string_shift($private, $length), -256);
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             $components['primes'] = array(1 => new BigInteger($this->_string_shift($private, $length), -256));
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             $components['primes'][] = new BigInteger($this->_string_shift($private, $length), -256);
             $temp = $components['primes'][1]->subtract($this->one);
             $components['exponents'] = array(1 => $components['publicExponent']->modInverse($temp));
             $temp = $components['primes'][2]->subtract($this->one);
             $components['exponents'][] = $components['publicExponent']->modInverse($temp);
             extract(unpack('Nlength', $this->_string_shift($private, 4)));
             $components['coefficients'] = array(2 => new BigInteger($this->_string_shift($private, $length), -256));
             return $components;
     }
 }
Esempio n. 15
0
 private function getQueryString($method, $data = "")
 {
     //参数加密
     $crypt = new DES($this->dataKey);
     $data = urlencode($crypt->encrypt($data));
     $str = 'cId=' . $this->cId . '&authCode=' . $this->getAuthCode($method) . '&method=' . $method . '&time=' . $this->time . '&data=' . $data;
     return $str;
 }
Esempio n. 16
0
 /**
  * Setup the performance-optimized function for de/encrypt()
  *
  * @see Base::_setupInlineCrypt()
  * @access private
  */
 function _setupInlineCrypt()
 {
     $lambda_functions =& DES::_getLambdaFunctions();
     // Engine configuration for:
     // -  DES ($des_rounds == 1) or
     // - 3DES ($des_rounds == 3)
     $des_rounds = $this->des_rounds;
     // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
     // 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 uniqe hash for our generated code
     switch (true) {
         case $gen_hi_opt_code:
             // For hi-optimized code, we create for each combination of
             // $mode, $des_rounds and $this->key its own encrypt/decrypt function.
             $code_hash = md5(str_pad("DES, {$des_rounds}, {$this->mode}, ", 32, "") . $this->key);
             break;
         default:
             // After max 10 hi-optimized functions, we create generic
             // (still very fast.. but not ultra) functions for each $mode/$des_rounds
             // Currently 2 * 5 generic functions will be then max. possible.
             $code_hash = "DES, {$des_rounds}, {$this->mode}";
     }
     // Is there a re-usable $lambda_functions in there? If not, we have to create it.
     if (!isset($lambda_functions[$code_hash])) {
         // Init code for both, encrypt and decrypt.
         $init_crypt = 'static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip;
             if (!$sbox1) {
                 $sbox1 = array_map("intval", $self->sbox1);
                 $sbox2 = array_map("intval", $self->sbox2);
                 $sbox3 = array_map("intval", $self->sbox3);
                 $sbox4 = array_map("intval", $self->sbox4);
                 $sbox5 = array_map("intval", $self->sbox5);
                 $sbox6 = array_map("intval", $self->sbox6);
                 $sbox7 = array_map("intval", $self->sbox7);
                 $sbox8 = array_map("intval", $self->sbox8);' . '
                 for ($i = 0; $i < 256; ++$i) {
                     $shuffleip[]    =  $self->shuffle[$self->ipmap[$i]];
                     $shuffleinvip[] =  $self->shuffle[$self->invipmap[$i]];
                 }
             }
         ';
         switch (true) {
             case $gen_hi_opt_code:
                 // In Hi-optimized code mode, we use our [3]DES key schedule as hardcoded integers.
                 // No futher initialisation of the $keys schedule is necessary.
                 // That is the extra performance boost.
                 $k = array(CRYPT_DES_ENCRYPT => $this->keys[CRYPT_DES_ENCRYPT], CRYPT_DES_DECRYPT => $this->keys[CRYPT_DES_DECRYPT]);
                 $init_encrypt = '';
                 $init_decrypt = '';
                 break;
             default:
                 // In generic optimized code mode, we have to use, as the best compromise [currently],
                 // our key schedule as $ke/$kd arrays. (with hardcoded indexes...)
                 $k = array(CRYPT_DES_ENCRYPT => array(), CRYPT_DES_DECRYPT => array());
                 for ($i = 0, $c = count($this->keys[CRYPT_DES_ENCRYPT]); $i < $c; ++$i) {
                     $k[CRYPT_DES_ENCRYPT][$i] = '$ke[' . $i . ']';
                     $k[CRYPT_DES_DECRYPT][$i] = '$kd[' . $i . ']';
                 }
                 $init_encrypt = '$ke = $self->keys[CRYPT_DES_ENCRYPT];';
                 $init_decrypt = '$kd = $self->keys[CRYPT_DES_DECRYPT];';
                 break;
         }
         // Creating code for en- and decryption.
         $crypt_block = array();
         foreach (array(CRYPT_DES_ENCRYPT, CRYPT_DES_DECRYPT) as $c) {
             /* Do the initial IP permutation. */
             $crypt_block[$c] = '
                 $in = unpack("N*", $in);
                 $l  = $in[1];
                 $r  = $in[2];
                 $in = unpack("N*",
                     ($shuffleip[ $r        & 0xFF] & "\\x80\\x80\\x80\\x80\\x80\\x80\\x80\\x80") |
                     ($shuffleip[($r >>  8) & 0xFF] & "\\x40\\x40\\x40\\x40\\x40\\x40\\x40\\x40") |
                     ($shuffleip[($r >> 16) & 0xFF] & "\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20") |
                     ($shuffleip[($r >> 24) & 0xFF] & "\\x10\\x10\\x10\\x10\\x10\\x10\\x10\\x10") |
                     ($shuffleip[ $l        & 0xFF] & "\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08") |
                     ($shuffleip[($l >>  8) & 0xFF] & "\\x04\\x04\\x04\\x04\\x04\\x04\\x04\\x04") |
                     ($shuffleip[($l >> 16) & 0xFF] & "\\x02\\x02\\x02\\x02\\x02\\x02\\x02\\x02") |
                     ($shuffleip[($l >> 24) & 0xFF] & "\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01")
                 );
                 ' . '
                 $l = $in[1];
                 $r = $in[2];
             ';
             $l = '$l';
             $r = '$r';
             // Perform DES or 3DES.
             for ($ki = -1, $des_round = 0; $des_round < $des_rounds; ++$des_round) {
                 // Perform the 16 steps.
                 for ($i = 0; $i < 16; ++$i) {
                     // 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.
                     $crypt_block[$c] .= '
                         $b1 = ((' . $r . ' >>  3) & 0x1FFFFFFF)  ^ (' . $r . ' << 29) ^ ' . $k[$c][++$ki] . ';
                         $b2 = ((' . $r . ' >> 31) & 0x00000001)  ^ (' . $r . ' <<  1) ^ ' . $k[$c][++$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 . ';
                     ';
                     // end of "the Feistel (F) function"
                     // swap L & R
                     list($l, $r) = array($r, $l);
                 }
                 list($l, $r) = array($r, $l);
             }
             // Perform the inverse IP permutation.
             $crypt_block[$c] .= '$in =
                 ($shuffleinvip[($l >> 24) & 0xFF] & "\\x80\\x80\\x80\\x80\\x80\\x80\\x80\\x80") |
                 ($shuffleinvip[($r >> 24) & 0xFF] & "\\x40\\x40\\x40\\x40\\x40\\x40\\x40\\x40") |
                 ($shuffleinvip[($l >> 16) & 0xFF] & "\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20") |
                 ($shuffleinvip[($r >> 16) & 0xFF] & "\\x10\\x10\\x10\\x10\\x10\\x10\\x10\\x10") |
                 ($shuffleinvip[($l >>  8) & 0xFF] & "\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08") |
                 ($shuffleinvip[($r >>  8) & 0xFF] & "\\x04\\x04\\x04\\x04\\x04\\x04\\x04\\x04") |
                 ($shuffleinvip[ $l        & 0xFF] & "\\x02\\x02\\x02\\x02\\x02\\x02\\x02\\x02") |
                 ($shuffleinvip[ $r        & 0xFF] & "\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x01");
             ';
         }
         // Creates the inline-crypt function
         $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(array('init_crypt' => $init_crypt, 'init_encrypt' => $init_encrypt, 'init_decrypt' => $init_decrypt, 'encrypt_block' => $crypt_block[CRYPT_DES_ENCRYPT], 'decrypt_block' => $crypt_block[CRYPT_DES_DECRYPT]));
     }
     // Set the inline-crypt function as callback in: $this->inline_crypt
     $this->inline_crypt = $lambda_functions[$code_hash];
 }