Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Encrypt/Decrypt String with RSA and X509Certificate

Thread view: 
b.croissant@gmail.com - 22 Jun 2005 09:24 GMT
Hi,
I've 2 files : CA.cert (X509 certificate) and CA.key (contains private
key)
I want to encrypt a string "secret message" with the public key of the
certificate and decrypt this string with the private key.
I think encryption is ok, but I can't import the private key from the
file.
Here is my code:
--------------------------------------------------------------------
[...]

InputStream inStream = new FileInputStream("./CA.crt"); //The X509
certificate
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert =
(X509Certificate)cf.generateCertificate(inStream);
        inStream.close();

RSAPublicKey rsaPublicKey = (RSAPublicKey)cert.getPublicKey();
BouncyCastleProvider bcp = new BouncyCastleProvider();
Security.addProvider(bcp);
Cipher encryptCipher = Cipher.getInstance("RSA", bcp);
encryptCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);

String message = "secret message";
byte[] messageACrypter = message.getBytes();
byte[] messageCrypte = encryptCipher.doFinal(messageACrypter);

System.out.println("\nSource : "+message);
System.out.println("Source crypted: "+new String(messageCrypte)+"\n");

File keyFile = new File("./CA.key");
DataInputStream in = new DataInputStream(new FileInputStream(keyFile));
byte [] fileBytes = new byte[(int) keyFile.length()];
in.readFully(fileBytes);
in.close();
KeyFactory kf = KeyFactory.getInstance("RSA");
KeySpec ks = new X509EncodedKeySpec(fileBytes);
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)kf.generatePrivate(ks);

Cipher decryptCipher = Cipher.getInstance("RSA", bcp);
decryptCipher.init(Cipher.DECRYPT_MODE,rsaPrivateKey);

byte[] messageDecrypte = decryptCipher.doFinal(messageCrypte);
        System.out.println("Source decrypted: "+new
String(messageDecrypte)+"\n");
[...]
-------------------------------------------------------------
I've an error :

java.security.spec.InvalidKeySpecException: Key spec not RSA.

How to correct this?
Thanks a lot.
iksrazal@terra.com.br - 22 Jun 2005 14:02 GMT
> Hi,
> I've 2 files : CA.cert (X509 certificate) and CA.key (contains private
[quoted text clipped - 3 lines]
> I think encryption is ok, but I can't import the private key from the
> file.

OK. I store my Certs in LDAP and the private key as a serialized object
in LDAP. When I create my certs/private key and store them in LDAP - I
use the keystore file created by the java tool keygen. So this may not
help you - but the general idea is:

pk = (PrivateKey)ks.getKey(privateKeyAlias,
privateKeyPass.toCharArray());

I can show you the rest if you decide you want to go that route.

HTH,
iksrazal
http://www.braziloutsource.com/

> Here is my code:
> --------------------------------------------------------------------
[quoted text clipped - 43 lines]
> How to correct this?
> Thanks a lot.
Benoît - 23 Jun 2005 08:40 GMT
Thanks.
I use keystore generated with keytool.
Your solution with LDAP is interresting but a little complex in my
case.
I keep your email if I'me interrested later

Bye


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.