public_exponent = $publicexponent; //$this->num_bits = $numbits; $this->private_key = $this->generateNewPrivateKey($numbits, $publicexponent); list($this->modulus, $this->public_key) = $this->getModulusAndPublicKeyFromPrivateKey($this->private_key); } // get modulus and public key from privatekey via openssl function getModulusAndPublicKeyFromPrivateKey($privkey) { if (preg_match('/^-----BEGIN RSA PRIVATE KEY-----[^-]+-----END RSA PRIVATE KEY-----$/s', $privkey)) { $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w") // stdout is a pipe that the child will write to //2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to ); $process = @proc_open("$this->opensslLocation rsa -pubout -modulus", $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $privkey); fclose($pipes[0]); //$output = stream_get_contents($pipes[1]); // PHP5 only :( while (!feof($pipes[1])) $output .= fgets($pipes[1]); $output = trim($output); fclose($pipes[1]); @proc_close($process); if (preg_match('/^Modulus=(.+)(-----BEGIN PUBLIC KEY-----[^-]+-----END PUBLIC KEY-----)$/s', $output, $matches)) { return array( trim($matches[1]), trim($matches[2]) ); } } } return array('', ''); } // generates private key via openssl function generateNewPrivateKey($numbits = 1024, $publicexponent = 10001) { if ($publicexponent == 10001) { $e = '-f4'; $this->public_exponent = 10001; } else { $e = '-3'; $this->public_exponent = 3; } $numbits = (int)$numbits; switch($numbits) { case 512: case 1024: case 2048: case 3072: $nbits = $numbits; break; default: $nbits = 1024; break; } $this->num_bits = $nbits; $privkey = @shell_exec("$this->opensslLocation genrsa $e $nbits"); return trim($privkey); } } } ?>