KGA
KGA

Reputation: 1

is it possible to generate RSA privatekey using modulus and exponent like did on Java side?

I am using RSA decryption on an iOS solution. I want to use the same parameters used on Java side to create the privateKey, but i am not able to find how. is there a way to do it or is it possible to export this privateKey using Java and then import it on the iOS solutions ?

byte[] modulusBytes = Base64.decode("base64EncodedString");
byte[] DBytes = Base64.decode("anotherBase64EncodedString");
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, DBytes);

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

My iOS application will decrypt a QRCode using this algorithme. the QRCode is encrypted using Java by a public key like below. to decrypt this code, we use the code above on Java side. so how we can generate the same privatekey that will be able to decrypt ? is it possible to do it without the same modulus and exponent ?

byte[] modulusBytes = Base64.decode("base64EncodedString");

byte[] exponentBytes = Base64.decode("AQAB");
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, exponentBytes);

RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);

Upvotes: 0

Views: 858

Answers (1)

Jonathan Rosenne
Jonathan Rosenne

Reputation: 2227

This is not the way it is supposed to work. You could generate a pair of private and public keys on each side, and exchange the public keys. The each side would encrypt the messages they wish to send (provided they are not too long) with the other side's public key, and decrypt the received messages with their own private key. But the private key is and should remain just that, private.

Upvotes: 1

Related Questions