I was able to generate a public key in my Java desktop environment and I got something like this
Sun RSA public key, 1024 bits modulus: 101700973019391285593457598101942678753508114287289699162184623605939671495532511783006850112834969917970271633181351680298749946797462542179729127916916336425952724141383800466274935950042225686754068132826643586090512962724382324158485291344703936377718522573879330753020035687831145457530843148690890911921 public exponent: 65537
But on Android device, when I get the key as a string and use KeyFactory
to generate the public key, I am getting this:
OpenSSLRSAPublicKey{modulus=90d3b5cefc50dc42828cee8d718876f7573b4c9287dddf808e73cb66266c2004165217f86d0f0192de0bb88b3aac2002303ee8b1c926e9bc54189a5ec5a12bb293df0b3c6ff2458a63098f712f0b72218ce301c38de3971ae8c6c646160a5e2e24dc07679e5a82ada1233ecf5eca3d0d1f483d1c9f059 a23deed537c670b70b1,publicExponent=10001}
There is discrepancy in the keys. I have tried SpongyCastle. I am still getting OpenSSL result on Android.
What am I missing?
RSA is a public-key or asymmetric crypto system. It uses a public key for encryption and a private key for decryption. Anyone can use the public key to encrypt a message but it can be decrypted only by the private key owner.
To obtain the public key from the Android Keystore use java. security. KeyStore#getCertificate(String) and then Certificate#getPublicKey() . To help obtain algorithm-specific public parameters of key pairs stored in the Android Keystore, its private keys implement java.
The first step of encrypting a message with RSA is to generate the keys. To do this, we need two prime numbers (p and q) which are selected with a primality test. A primality test is an algorithm that efficiently finds prime numbers, such as the Rabin-Miller primality test.
There is no discrepancy between the keys. One is printed in base-16 (hexadecimal) whereas the other is printed in decimal. Just try the following code for reassurance:
BigInteger bi = new BigInteger(
"90d3b5cefc50dc42828cee8d718876f7573b4c9287dddf808e73cb66266c2004165217f86d0f0192de0bb88b3aac2002303ee8b1c926e9bc54189a5ec5a12bb293df0b3c6ff2458a63098f712f0b72218ce301c38de3971ae8c6c646160a5e2e24dc07679e5a82ada1233ecf5eca3d0d1f483d1c9f059a23deed537c670b70b1",
16);
System.out.println(bi.toString(10));
The result will be the same as your non OpenSSL key.
What's happening here is that your different platforms have different cryptographic providers that perform the crypto operations. The results will be the same, but there will be small idiosyncrasies that will be noticed, for example, when calling toString()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With