当前位置: 首页>>代码示例>>PHP>>正文


PHP BigInteger::toBytes方法代码示例

本文整理汇总了PHP中phpseclib\Math\BigInteger::toBytes方法的典型用法代码示例。如果您正苦于以下问题:PHP BigInteger::toBytes方法的具体用法?PHP BigInteger::toBytes怎么用?PHP BigInteger::toBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在phpseclib\Math\BigInteger的用法示例。


在下文中一共展示了BigInteger::toBytes方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: randomNumber

 /**
  * Generate a number that lies between 0 and q-1.
  *
  * @param \phpseclib\Math\BigInteger $q  Max number.
  *
  * @return \phpseclib\Math\BigInteger  Generated number.
  */
 public static function randomNumber($q)
 {
     $bytes = strlen($q->toBytes()) + 8;
     $ints = $bytes + 1 >> 2;
     $cstring = Crypt\Random::String($ints);
     $random = '';
     for ($i = 0; $i < $ints; ++$i) {
         $random .= pack('N', $cstring[$i]);
     }
     $c = new BigInteger(substr($random, 0, $bytes), 256);
     $one = new BigInteger(1);
     $result_base = $c->divide($q->subtract($one));
     return $result_base[1]->add($one);
 }
开发者ID:horde,项目名称:horde,代码行数:21,代码来源:DSA.php

示例2: decrypt

 /**
  * Descrypts encrypted text
  *
  * @param string $ciphertext Text to decrypt
  * @return string Decrypted text or DECRYPTION_FAILED in case of failure
  */
 public static function decrypt($ciphertext)
 {
     $rsa = new RSA();
     $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
     $rsa->loadKey(static::getPrivateKey());
     $s = new BigInteger($ciphertext, 16);
     // prevent library error output appearing in the dashboard
     set_error_handler(function () {
         /* ignore errors */
     });
     $cleartext = $rsa->decrypt($s->toBytes());
     restore_error_handler();
     return $cleartext;
 }
开发者ID:Joey3000,项目名称:piwik-LoginEncrypted,代码行数:20,代码来源:Crypto.php

示例3: array


//.........这里部分代码省略.........
                 if ($temp === false) {
                     return false;
                 }
                 // An empty child encoding means it has been optimized out.
                 // Else we should have at least one tag byte.
                 if ($temp === '') {
                     continue;
                 }
                 $tag = ord($temp[0]);
                 // if isset($child['constant']) is true then isset($child['optional']) should be true as well
                 if (isset($child['constant'])) {
                     if (isset($child['explicit']) || $child['type'] == self::TYPE_CHOICE) {
                         $subtag = chr(self::CLASS_CONTEXT_SPECIFIC << 6 | 0x20 | $child['constant']);
                         $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp;
                     } else {
                         $subtag = chr(self::CLASS_CONTEXT_SPECIFIC << 6 | ord($temp[0]) & 0x20 | $child['constant']);
                         $temp = $subtag . substr($temp, 1);
                     }
                 }
             }
             if (isset($idx)) {
                 array_pop($this->location);
             }
             if ($temp && isset($mapping['cast'])) {
                 $temp[0] = chr($mapping['class'] << 6 | $tag & 0x20 | $mapping['cast']);
             }
             return $temp;
         case self::TYPE_INTEGER:
         case self::TYPE_ENUMERATED:
             if (!isset($mapping['mapping'])) {
                 if (is_numeric($source)) {
                     $source = new BigInteger($source);
                 }
                 $value = $source->toBytes(true);
             } else {
                 $value = array_search($source, $mapping['mapping']);
                 if ($value === false) {
                     return false;
                 }
                 $value = new BigInteger($value);
                 $value = $value->toBytes(true);
             }
             if (!strlen($value)) {
                 $value = chr(0);
             }
             break;
         case self::TYPE_UTC_TIME:
         case self::TYPE_GENERALIZED_TIME:
             $format = $mapping['type'] == self::TYPE_UTC_TIME ? 'y' : 'Y';
             $format .= 'mdHis';
             $value = @gmdate($format, strtotime($source)) . 'Z';
             break;
         case self::TYPE_BIT_STRING:
             if (isset($mapping['mapping'])) {
                 $bits = array_fill(0, count($mapping['mapping']), 0);
                 $size = 0;
                 for ($i = 0; $i < count($mapping['mapping']); $i++) {
                     if (in_array($mapping['mapping'][$i], $source)) {
                         $bits[$i] = 1;
                         $size = $i;
                     }
                 }
                 if (isset($mapping['min']) && $mapping['min'] >= 1 && $size < $mapping['min']) {
                     $size = $mapping['min'] - 1;
                 }
                 $offset = 8 - ($size + 1 & 7);
开发者ID:BozzaCoon,项目名称:SPHERE-Framework,代码行数:67,代码来源:ASN1.php

示例4: switch

 /**
  * Logical Exclusive-Or
  *
  * @param \phpseclib\Math\BigInteger $x
  * @access public
  * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  * @return \phpseclib\Math\BigInteger
  */
 function bitwise_xor($x)
 {
     switch (MATH_BIGINTEGER_MODE) {
         case self::MODE_GMP:
             $temp = new static();
             $temp->value = gmp_xor($this->value, $x->value);
             return $this->_normalize($temp);
         case self::MODE_BCMATH:
             $left = $this->toBytes();
             $right = $x->toBytes();
             $length = max(strlen($left), strlen($right));
             $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
             $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
             return $this->_normalize(new static($left ^ $right, 256));
     }
     $length = max(count($this->value), count($x->value));
     $result = $this->copy();
     $result->value = array_pad($result->value, $length, 0);
     $x->value = array_pad($x->value, $length, 0);
     for ($i = 0; $i < $length; ++$i) {
         $result->value[$i] ^= $x->value[$i];
     }
     return $this->_normalize($result);
 }
开发者ID:msulistijo,项目名称:PhpseclibBundle,代码行数:32,代码来源:BigInteger.php

示例5: savePrivateKey

 /**
  * Convert a private key to the appropriate format.
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @param \phpseclib\Math\BigInteger $d
  * @param array $primes
  * @param array $exponents
  * @param array $coefficients
  * @param string $password optional
  * @return string
  */
 static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, $primes, $exponents, $coefficients, $password = '')
 {
     if (count($primes) != 2) {
         return false;
     }
     $raw = array('modulus' => $n->toBytes(true), 'publicExponent' => $e->toBytes(true), 'privateExponent' => $d->toBytes(true), 'prime1' => $primes[1]->toBytes(true), 'prime2' => $primes[2]->toBytes(true), 'exponent1' => $exponents[1]->toBytes(true), 'exponent2' => $exponents[2]->toBytes(true), 'coefficient' => $coefficients[2]->toBytes(true));
     $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
     $encryption = !empty($password) || is_string($password) ? 'aes256-cbc' : 'none';
     $key .= $encryption;
     $key .= "\r\nComment: " . self::$comment . "\r\n";
     $public = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus']);
     $source = pack('Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($encryption), $encryption, strlen(self::$comment), self::$comment, strlen($public), $public);
     $public = Base64::encode($public);
     $key .= "Public-Lines: " . (strlen($public) + 63 >> 6) . "\r\n";
     $key .= chunk_split($public, 64);
     $private = pack('Na*Na*Na*Na*', strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'], strlen($raw['coefficient']), $raw['coefficient']);
     if (empty($password) && !is_string($password)) {
         $source .= pack('Na*', strlen($private), $private);
         $hashkey = 'putty-private-key-file-mac-key';
     } else {
         $private .= Random::string(16 - (strlen($private) & 15));
         $source .= pack('Na*', strlen($private), $private);
         $crypto = new AES();
         $crypto->setKey(static::generateSymmetricKey($password, 32));
         $crypto->setIV(str_repeat("", $crypto->getBlockLength() >> 3));
         $crypto->disablePadding();
         $private = $crypto->encrypt($private);
         $hashkey = 'putty-private-key-file-mac-key' . $password;
     }
     $private = Base64::encode($private);
     $key .= 'Private-Lines: ' . (strlen($private) + 63 >> 6) . "\r\n";
     $key .= chunk_split($private, 64);
     $hash = new Hash('sha1');
     $hash->setKey(sha1($hashkey, true));
     $key .= 'Private-MAC: ' . Hex::encode($hash->hash($source)) . "\r\n";
     return $key;
 }
开发者ID:phpseclib,项目名称:phpseclib,代码行数:50,代码来源:PuTTY.php

示例6: fgets

 /**
  * Connect to an SSHv1 server
  *
  * @return Boolean
  * @access private
  */
 function _connect()
 {
     $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->connectionTimeout);
     if (!$this->fsock) {
         user_error(rtrim("Cannot connect to {$this->host}:{$this->port}. Error {$errno}. {$errstr}"));
         return false;
     }
     $this->server_identification = $init_line = fgets($this->fsock, 255);
     if (defined('NET_SSH1_LOGGING')) {
         $this->_append_log('<-', $this->server_identification);
         $this->_append_log('->', $this->identifier . "\r\n");
     }
     if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
         user_error('Can only connect to SSH servers');
         return false;
     }
     if ($parts[1][0] != 1) {
         user_error("Cannot connect to SSH {$parts['1']} servers");
         return false;
     }
     fputs($this->fsock, $this->identifier . "\r\n");
     $response = $this->_get_binary_packet();
     if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
         user_error('Expected SSH_SMSG_PUBLIC_KEY');
         return false;
     }
     $anti_spoofing_cookie = $this->_string_shift($response[self::RESPONSE_DATA], 8);
     $this->_string_shift($response[self::RESPONSE_DATA], 4);
     $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2));
     $server_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->server_key_public_exponent = $server_key_public_exponent;
     $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2));
     $server_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->server_key_public_modulus = $server_key_public_modulus;
     $this->_string_shift($response[self::RESPONSE_DATA], 4);
     $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2));
     $host_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->host_key_public_exponent = $host_key_public_exponent;
     $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2));
     $host_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->host_key_public_modulus = $host_key_public_modulus;
     $this->_string_shift($response[self::RESPONSE_DATA], 4);
     // get a list of the supported ciphers
     extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4)));
     foreach ($this->supported_ciphers as $mask => $name) {
         if (($supported_ciphers_mask & 1 << $mask) == 0) {
             unset($this->supported_ciphers[$mask]);
         }
     }
     // get a list of the supported authentications
     extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4)));
     foreach ($this->supported_authentications as $mask => $name) {
         if (($supported_authentications_mask & 1 << $mask) == 0) {
             unset($this->supported_authentications[$mask]);
         }
     }
     $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
     $session_key = Random::string(32);
     $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
     if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
     } else {
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
     }
     $cipher = isset($this->supported_ciphers[$this->cipher]) ? $this->cipher : self::CIPHER_3DES;
     $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
     if (!$this->_send_binary_packet($data)) {
         user_error('Error sending SSH_CMSG_SESSION_KEY');
         return false;
     }
     switch ($cipher) {
         //case self::CIPHER_NONE:
         //    $this->crypto = new \phpseclib\Crypt\Null();
         //    break;
         case self::CIPHER_DES:
             $this->crypto = new DES();
             $this->crypto->disablePadding();
             $this->crypto->enableContinuousBuffer();
             $this->crypto->setKey(substr($session_key, 0, 8));
             break;
         case self::CIPHER_3DES:
             $this->crypto = new TripleDES(TripleDES::MODE_3CBC);
             $this->crypto->disablePadding();
             $this->crypto->enableContinuousBuffer();
             $this->crypto->setKey(substr($session_key, 0, 24));
             break;
             //case self::CIPHER_RC4:
             //    $this->crypto = new RC4();
             //    $this->crypto->enableContinuousBuffer();
             //    $this->crypto->setKey(substr($session_key, 0,  16));
             //    break;
     }
//.........这里部分代码省略.........
开发者ID:zeus911,项目名称:phpseclib,代码行数:101,代码来源:SSH1.php

示例7: loadKey

 /**
  * Loads a public or private key
  *
  * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
  *
  * @access public
  * @param String $key        	
  * @param Integer $type
  *        	optional
  */
 function loadKey($key, $type = false)
 {
     if (is_object($key) && strtolower(get_class($key)) == 'crypt_rsa') {
         $this->privateKeyFormat = $key->privateKeyFormat;
         $this->publicKeyFormat = $key->publicKeyFormat;
         $this->k = $key->k;
         $this->hLen = $key->hLen;
         $this->sLen = $key->sLen;
         $this->mgfHLen = $key->mgfHLen;
         $this->encryptionMode = $key->encryptionMode;
         $this->signatureMode = $key->signatureMode;
         $this->password = $key->password;
         $this->configFile = $key->configFile;
         $this->comment = $key->comment;
         if (is_object($key->hash)) {
             $this->hash = new Hash($key->hash->getHash());
         }
         if (is_object($key->mgfHash)) {
             $this->mgfHash = new Hash($key->mgfHash->getHash());
         }
         if (is_object($key->modulus)) {
             $this->modulus = $key->modulus->copy();
         }
         if (is_object($key->exponent)) {
             $this->exponent = $key->exponent->copy();
         }
         if (is_object($key->publicExponent)) {
             $this->publicExponent = $key->publicExponent->copy();
         }
         $this->primes = array();
         $this->exponents = array();
         $this->coefficients = array();
         foreach ($this->primes as $prime) {
             $this->primes[] = $prime->copy();
         }
         foreach ($this->exponents as $exponent) {
             $this->exponents[] = $exponent->copy();
         }
         foreach ($this->coefficients as $coefficient) {
             $this->coefficients[] = $coefficient->copy();
         }
         return true;
     }
     if ($type === false) {
         $types = array(CRYPT_RSA_PUBLIC_FORMAT_RAW, CRYPT_RSA_PRIVATE_FORMAT_PKCS1, CRYPT_RSA_PRIVATE_FORMAT_XML, CRYPT_RSA_PRIVATE_FORMAT_PUTTY, CRYPT_RSA_PUBLIC_FORMAT_OPENSSH);
         foreach ($types as $type) {
             $components = $this->_parseKey($key, $type);
             if ($components !== false) {
                 break;
             }
         }
     } else {
         $components = $this->_parseKey($key, $type);
     }
     if ($components === false) {
         return false;
     }
     if (isset($components['comment']) && $components['comment'] !== false) {
         $this->comment = $components['comment'];
     }
     $this->modulus = $components['modulus'];
     $this->k = strlen($this->modulus->toBytes());
     $this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent'];
     if (isset($components['primes'])) {
         $this->primes = $components['primes'];
         $this->exponents = $components['exponents'];
         $this->coefficients = $components['coefficients'];
         $this->publicExponent = $components['publicExponent'];
     } else {
         $this->primes = array();
         $this->exponents = array();
         $this->coefficients = array();
         $this->publicExponent = false;
     }
     return true;
 }
开发者ID:HerO-0110,项目名称:EmailAuth,代码行数:86,代码来源:RSA.php

示例8: savePublicKey

 /**
  * Convert a public key to the appropriate format
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @return string
  */
 static function savePublicKey(BigInteger $n, BigInteger $e)
 {
     $modulus = $n->toBytes(true);
     $publicExponent = $e->toBytes(true);
     // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.1>:
     // RSAPublicKey ::= SEQUENCE {
     //     modulus           INTEGER,  -- n
     //     publicExponent    INTEGER   -- e
     // }
     $components = array('modulus' => pack('Ca*a*', self::ASN1_INTEGER, self::_encodeLength(strlen($modulus)), $modulus), 'publicExponent' => pack('Ca*a*', self::ASN1_INTEGER, self::_encodeLength(strlen($publicExponent)), $publicExponent));
     $RSAPublicKey = pack('Ca*a*a*', self::ASN1_SEQUENCE, self::_encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent']);
     $RSAPublicKey = "-----BEGIN RSA PUBLIC KEY-----\r\n" . chunk_split(base64_encode($RSAPublicKey), 64) . '-----END RSA PUBLIC KEY-----';
     return $RSAPublicKey;
 }
开发者ID:bengitiger,项目名称:phpseclib,代码行数:22,代码来源:PKCS1.php

示例9: savePrivateKey

 /**
  * Convert a private key to the appropriate format.
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @param \phpseclib\Math\BigInteger $d
  * @param array $primes
  * @param array $exponents
  * @param array $coefficients
  * @param string $password optional
  * @return string
  */
 static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, $primes, $exponents, $coefficients, $password = '')
 {
     $n = strrev($n->toBytes());
     $e = str_pad(strrev($e->toBytes()), 4, "");
     $key = pack('aavV', chr(self::PRIVATEKEYBLOB), chr(2), 0, self::CALG_RSA_KEYX);
     $key .= pack('VVa*', self::RSA2, 8 * strlen($n), $e);
     $key .= $n;
     $key .= strrev($primes[1]->toBytes());
     $key .= strrev($primes[2]->toBytes());
     $key .= strrev($exponents[1]->toBytes());
     $key .= strrev($exponents[2]->toBytes());
     $key .= strrev($coefficients[1]->toBytes());
     $key .= strrev($d->toBytes());
     return base64_encode($key);
 }
开发者ID:bengitiger,项目名称:phpseclib,代码行数:28,代码来源:MSBLOB.php

示例10: savePublicKey

 /**
  * Convert a public key to the appropriate format
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @return string
  */
 static function savePublicKey(BigInteger $n, BigInteger $e)
 {
     return "<RSAKeyValue>\r\n" . '  <Modulus>' . Base64::encode($n->toBytes()) . "</Modulus>\r\n" . '  <Exponent>' . Base64::encode($e->toBytes()) . "</Exponent>\r\n" . '</RSAKeyValue>';
 }
开发者ID:paragonie-scott,项目名称:phpseclib,代码行数:12,代码来源:XML.php

示例11: getPublicKeyFingerprint

    /**
     * Returns the public key's fingerprint
     *
     * The public key's fingerprint is returned, which is equivalent to running `ssh-keygen -lf rsa.pub`. If there is
     * no public key currently loaded, false is returned.
     * Example output (md5): "c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87" (as specified by RFC 4716)
     *
     * @access public
     * @param string $algorithm The hashing algorithm to be used. Valid options are 'md5' and 'sha256'. False is returned
     * for invalid values.
     * @return mixed
     */
    function getPublicKeyFingerprint($algorithm = 'md5')
    {
        if (empty($this->modulus) || empty($this->publicExponent)) {
            return false;
        }

        $modulus = $this->modulus->toBytes(true);
        $publicExponent = $this->publicExponent->toBytes(true);

        $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);

        switch ($algorithm) {
            case 'sha256':
                $hash = new Hash('sha256');
                $base = base64_encode($hash->hash($RSAPublicKey));
                return substr($base, 0, strlen($base) - 1);
            case 'md5':
                return substr(chunk_split(md5($RSAPublicKey), 2, ':'), 0, -1);
            default:
                return false;
        }
    }
开发者ID:juggernautsei,项目名称:openemr,代码行数:34,代码来源:RSA.php

示例12: loadKey

 /**
  * Loads a public or private key
  *
  * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
  *
  * @access public
  * @param String $key
  * @param Integer $type optional
  */
 function loadKey($key, $type = false)
 {
     if ($key instanceof RSA) {
         $this->privateKeyFormat = $key->privateKeyFormat;
         $this->publicKeyFormat = $key->publicKeyFormat;
         $this->k = $key->k;
         $this->hLen = $key->hLen;
         $this->sLen = $key->sLen;
         $this->mgfHLen = $key->mgfHLen;
         $this->encryptionMode = $key->encryptionMode;
         $this->signatureMode = $key->signatureMode;
         $this->password = $key->password;
         $this->configFile = $key->configFile;
         $this->comment = $key->comment;
         if (is_object($key->hash)) {
             $this->hash = new Hash($key->hash->getHash());
         }
         if (is_object($key->mgfHash)) {
             $this->mgfHash = new Hash($key->mgfHash->getHash());
         }
         if (is_object($key->modulus)) {
             $this->modulus = $key->modulus->copy();
         }
         if (is_object($key->exponent)) {
             $this->exponent = $key->exponent->copy();
         }
         if (is_object($key->publicExponent)) {
             $this->publicExponent = $key->publicExponent->copy();
         }
         $this->primes = array();
         $this->exponents = array();
         $this->coefficients = array();
         foreach ($this->primes as $prime) {
             $this->primes[] = $prime->copy();
         }
         foreach ($this->exponents as $exponent) {
             $this->exponents[] = $exponent->copy();
         }
         foreach ($this->coefficients as $coefficient) {
             $this->coefficients[] = $coefficient->copy();
         }
         return true;
     }
     if ($type === false) {
         $types = array(self::PUBLIC_FORMAT_RAW, self::PRIVATE_FORMAT_PKCS1, self::PRIVATE_FORMAT_XML, self::PRIVATE_FORMAT_PUTTY, self::PUBLIC_FORMAT_OPENSSH);
         foreach ($types as $type) {
             $components = $this->_parseKey($key, $type);
             if ($components !== false) {
                 break;
             }
         }
     } else {
         $components = $this->_parseKey($key, $type);
     }
     if ($components === false) {
         return false;
     }
     if (isset($components['comment']) && $components['comment'] !== false) {
         $this->comment = $components['comment'];
     }
     $this->modulus = $components['modulus'];
     $this->k = strlen($this->modulus->toBytes());
     $this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent'];
     if (isset($components['primes'])) {
         $this->primes = $components['primes'];
         $this->exponents = $components['exponents'];
         $this->coefficients = $components['coefficients'];
         $this->publicExponent = $components['publicExponent'];
     } else {
         $this->primes = array();
         $this->exponents = array();
         $this->coefficients = array();
         $this->publicExponent = false;
     }
     switch ($type) {
         case self::PUBLIC_FORMAT_OPENSSH:
         case self::PUBLIC_FORMAT_RAW:
             $this->setPublicKey();
             break;
         case self::PRIVATE_FORMAT_PKCS1:
             switch (true) {
                 case strpos($key, '-BEGIN PUBLIC KEY-') !== false:
                 case strpos($key, '-BEGIN RSA PUBLIC KEY-') !== false:
                     $this->setPublicKey();
             }
     }
     return true;
 }
开发者ID:sksree,项目名称:Jorani_new,代码行数:97,代码来源:RSA.php

示例13: savePublicKey

 /**
  * Convert a public key to the appropriate format
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @return string
  */
 static function savePublicKey(BigInteger $n, BigInteger $e)
 {
     $modulus = $n->toBytes(true);
     $publicExponent = $e->toBytes(true);
     // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.1>:
     // RSAPublicKey ::= SEQUENCE {
     //     modulus           INTEGER,  -- n
     //     publicExponent    INTEGER   -- e
     // }
     $components = array('modulus' => pack('Ca*a*', self::ASN1_INTEGER, ASN1::encodeLength(strlen($modulus)), $modulus), 'publicExponent' => pack('Ca*a*', self::ASN1_INTEGER, ASN1::encodeLength(strlen($publicExponent)), $publicExponent));
     $RSAPublicKey = pack('Ca*a*a*', self::ASN1_SEQUENCE, ASN1::encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent']);
     // sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption.
     $rsaOID = "0\r\t*†H†÷\r";
     // hex version of MA0GCSqGSIb3DQEBAQUA
     $RSAPublicKey = chr(0) . $RSAPublicKey;
     $RSAPublicKey = chr(3) . ASN1::encodeLength(strlen($RSAPublicKey)) . $RSAPublicKey;
     $RSAPublicKey = pack('Ca*a*', self::ASN1_SEQUENCE, ASN1::encodeLength(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey);
     $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split(Base64::encode($RSAPublicKey), 64) . '-----END PUBLIC KEY-----';
     return $RSAPublicKey;
 }
开发者ID:andreybolonin,项目名称:phpseclib,代码行数:28,代码来源:PKCS8.php

示例14: savePublicKey

 /**
  * Convert a public key to the appropriate format
  *
  * @access public
  * @param \phpseclib\Math\BigInteger $n
  * @param \phpseclib\Math\BigInteger $e
  * @return string
  */
 static function savePublicKey(BigInteger $n, BigInteger $e)
 {
     $publicExponent = $e->toBytes(true);
     $modulus = $n->toBytes(true);
     // from <http://tools.ietf.org/html/rfc4253#page-15>:
     // string    "ssh-rsa"
     // mpint     e
     // mpint     n
     $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
     $RSAPublicKey = 'ssh-rsa ' . Base64::encode($RSAPublicKey) . ' ' . self::$comment;
     return $RSAPublicKey;
 }
开发者ID:phpseclib,项目名称:phpseclib,代码行数:20,代码来源:OpenSSH.php


注:本文中的phpseclib\Math\BigInteger::toBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。