2

I am new to Cryptography. I was provided with a RSA public key in base64 format. Is there any way to encrypt some text using just the public key. Do I need modulus/exponent too?

Any pointers would be very helpful. Thanks

AbbIbb
  • 41
  • 1
  • 4
  • I am not able to generate the RSA public key object. From what I see it needs modulus and exponent. RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent). I just have the public key file. Is there any way to create RSAPublicKeySpec using just key file. I looked at Bouncy Castle library but could not find anything helpful. – AbbIbb Oct 18 '11 at 15:36

3 Answers3

0

I was interested as well, I went "Google-ing" and found this interesting page. It is a lot of information, but very interesting in my opinion.

http://www.di-mgt.com.au/rsa_alg.html

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

Saying it is in base64 format is not enough information. There are at least two common ways that an RSA public key may be formatted, and either one may be base64 encoded. If you are lucky, the key is an X509EncodedKeySpec. If so, you need to base64 decode it, create a KeyFactory, then use the KeyFactory to generate the public key. Here some untested, not even compiled, code that hopefully shows these steps.

// Example using the base64 class from http://iharder.sourceforge.net/current/java/base64/

byte [] x509Key = Base64.decode(base64Key);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey rsaPub = (RSAPublicKey) kf.generatePublic();

If there are -----BEGIN and ----END lines surrounding your base64 key just delete them.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
-1

The public key is used to decrypt ciphertext which has been encrypted by the private key.

Each user that want to communicate has a pair of cryptographic keys—a public encryption key and a private decryption key.

You can encrypt using the public key becuse the keys are derived mathematically, but parameters are chosen so that determining the private key from the public key is prohibitively expensive.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Manish D.
  • 65
  • 1
  • 7
  • Let say user A generated keys (public/private) and supplies user B the public key. I thought user B encrypts using the public key. User A can then decrypt on his end using the private/public keys. – AbbIbb Oct 18 '11 at 16:36