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.
Asked
Active
Viewed 2,177 times
0
-
1You have some links to information that you have considered useful / checked already? Please add them to your question. – Ryan Vincent Sep 22 '16 at 19:06
-
http://nmichaels.org/rsa.py Example , above site is getting n , e , d components and doing rsa crypt operation. Im in curious , how is he/she get private key for decrypt from this 3 components ? – Muhammed N. Kartal Sep 28 '16 at 08:52
-
@MuhammedN.Kartal - you *can* do decryption with just n and d but it's not going to be as efficient as it could be, hence why *real-world* implementations use the extra CRT parameters. Real-world RSA implementations and textbook-RSA implementations do different things. Another thing textbook-RSA implementations don't do: padding. That textbook-RSA implementations are subject to known plaintext attacks (among other things) isn't really an issue in the classroom as the goal in the classroom is to give you the /general/ idea. – neubert Sep 28 '16 at 11:32
-
Thank you very very much @neubert , now i can exactly understand some part of this topic. And i will continue research. – Muhammed N. Kartal Sep 28 '16 at 11:42
1 Answers
0
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-----

neubert
- 15,947
- 24
- 120
- 212
-
Thanks a lot for your answer. I got this code before , i was thinking same like you , (http://nmichaels.org/rsa.py) but above site doing rsa enc/dec with just 3 components. Somehow he/she get a private key from hexadecimal n,e,d components and decrypt data with them. – Muhammed N. Kartal Sep 28 '16 at 08:59
-
@MuhammedN.Kartal - Well like I said you can just rename BEGIN PUBLIC KEY to BEGIN PRIVATE KEY. http://stackoverflow.com/a/21289989/569976 discusses the RSA private key format. Note that the primes and CRT parameters are not optional. What is optional is otherPrimeInfos, which is for RSA private keys with more than two primes. – neubert Sep 28 '16 at 11:27
-
I'll read this discuss about private key format. Thanks a lot. – Muhammed N. Kartal Sep 28 '16 at 11:48