randomNumber() public static method

Generate a number that lies between 0 and q-1.
public static randomNumber ( phpseclib\Math\BigInteger $q ) : phpseclib\Math\BigInteger
$q phpseclib\Math\BigInteger Max number.
return phpseclib\Math\BigInteger Generated number.
Ejemplo n.º 1
0
 /**
  * Encrypt data.
  *
  * @param string $text  Plaintext.
  *
  * @return array  Array of MPI values (c1, c2).
  */
 public function encrypt($text)
 {
     $p_len = strlen($this->_key->key['p']);
     $length = $p_len - 11;
     if ($length <= 0) {
         return false;
     }
     $g = new Math_BigInteger($this->_key->key['g'], 256);
     $p = new Math_BigInteger($this->_key->key['p'], 256);
     $y = new Math_BigInteger($this->_key->key['y'], 256);
     $out = array();
     foreach (str_split($text, $length) as $m) {
         // EME-PKCS1-v1_5 encoding
         $psLen = $p_len - strlen($m) - 3;
         $ps = '';
         while (($psLen2 = strlen($ps)) != $psLen) {
             $tmp = crypt_random_string($psLen - $psLen2);
             $ps .= str_replace("", '', $tmp);
         }
         $em = new Math_BigInteger(chr(0) . chr(2) . $ps . chr(0) . $m, 256);
         // End EME-PKCS1-v1_5 encoding
         $k = Horde_Pgp_Crypt_DSA::randomNumber($p);
         $c1 = $g->modPow($k, $p);
         $c2_base = $y->modPow($k, $p)->multiply($em)->divide($p);
         $c2 = $c2_base[1];
         $out[] = str_pad($c1->toBytes(), $p_len, chr(0), STR_PAD_LEFT);
         $out[] = str_pad($c2->toBytes(), $p_len, chr(0), STR_PAD_LEFT);
     }
     return $out;
 }