简体   繁体   中英

how to get private key from exponent , modulus and private exponent in PHP?

im new in PHP Crypto and im using openssl to do crypto operations. Openssl Rsa is need PEM format , but i have hexadecimal modulus(n) , private exponent(d) and public exponent (e). How can i generate Private key from this components? And sorry for my english.

RSA private keys usually also have a bunch of other parameters too. The primes that were used to create the modulus (p and q) and other parameters to facilitate the use of the Chinese remainder theorem to speed up decryption. If you don't have that then I'd just create a public key. You might be able to replace PUBLIC with PRIVATE idk.

Anyway, here's some code (uses phpseclib 1.0.3 ):

<?php
include('Crypt/RSA.php');
include('Math/BigInteger.php');

$rsa = new Crypt_RSA();
$rsa->loadKey([
    'e' => new Math_BigInteger('...'), // base-10 by default
    'n' => new Math_BigInteger('...') // base-10 by default
]);

echo $rsa;

I used 15 as both e and n (which isn't actually a valid combo for RSA purposes but for demo purposes it's fine) and got this back:

-----BEGIN PUBLIC KEY-----
MBowDQYJKoZIhvcNAQEBBQADCQAwBgIBDwIBDw==
-----END PUBLIC KEY-----

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM