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.
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 ]
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.