Meaningless Drivel is fun!
The moose likes Security and the fly likes asymmetric ecryption / decryption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "asymmetric ecryption / decryption" Watch "asymmetric ecryption / decryption" New topic

asymmetric ecryption / decryption

Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hi there !
I'm sorry I have to occupy your attention, again.
But I didn't find any propper example anywhere.
Here's my problem:
I've got a private and a public key (RSA 1024)
and I want to encrypt a String with the public key
and decrypt it with the private key.
Do you maybe have a code snipped which shows me howto?
I would be very happy to hear from you ...
Thanx, Thomas.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
This is not normally done, as asymmetric encryption is very slow. The usual procedure is to generate a random (symmetric) session key, encrypt this using RSA or whatever you're using, and then encrypt the actual String with the session key.
One of the reasons that you can't find any examples is probably that the Sun JCE provider doesn't support any asymmetric encryption algorithms; all it has is Diffie-Hellman key exchange (and the JCE documentation does have an example of that). To get RSA support you need a third-party JCE provider such as Cryptix.
Once installed, writing code to use asymmetric encryption is not all that different from symmetric encryption. There are plenty of examples of the latter both in the JCE documentation and places like the Java Almanac.
- Peter
[ November 10, 2003: Message edited by: Peter den Haan ]
Pankaj Kr
Ranch Hand

Joined: Sep 09, 2003
Posts: 80
As Peter mentioned, you will need a JCE Provider that supports asymmetric encryption -- something that Sun doesn't include in J2SE SDK. However, the BouncyCastle JCE includes one.
I have a sample program using BouncyCastle in my book. Thesource file is available online. Look for class AsymmetricCipherTest.

Pankaj Kumar
Home - WebLog - J2EE Security
I agree. Here's the link:
subject: asymmetric ecryption / decryption
It's not a secret anymore!