예제 #1
0
 /**
 * 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 ($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;
     }
     $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;
 }
예제 #2
0
 /**
  * Generate modulated number
  *
  * Generates a number that lies between 0 and q-1
  *
  * @access public
  * @static
  * @staticvar MathBigInteger $one Constant one
  * @param MathBigInteger $q Modulation
  * @return MathBigInteger Generated number
  */
 public static function randomNumberMod($q)
 {
     // do a few more bits than q so we can wrap around with not too much bias
     // wow, turns out this was actually not far off from FIPS186-3, who knew?
     // FIPS186-3 says to generate 64 more bits than needed into "c", then to do:
     // result = (c mod (q-1)) + 1
     static $one;
     if (!isset($one)) {
         $one = new MathBigInteger(1);
     }
     $c = self::_os2ip(self::_random(strlen($q->toBytes()) + 8));
     $result_base = $c->divide($q->subtract($one));
     return $result_base[1]->add($one);
 }