Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Encrypt/Decrypt String with RSA and X509Certificate

Reply
Thread Tools

Encrypt/Decrypt String with RSA and X509Certificate

 
 
b.croissant@gmail.com
Guest
Posts: n/a
 
      06-22-2005
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,rsaPrivateK ey);

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.

 
Reply With Quote
 
 
 
 
iksrazal@terra.com.br
Guest
Posts: n/a
 
      06-22-2005


http://www.velocityreviews.com/forums/(E-Mail Removed) escreveu:
> 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.


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:
> --------------------------------------------------------------------
> [...]
>
> 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,rsaPrivateK ey);
>
> 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.


 
Reply With Quote
 
 
 
 
=?iso-8859-1?B?QmVub+50?=
Guest
Posts: n/a
 
      06-23-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass X509Certificate as String? Ian Pilcher Java 10 02-17-2013 09:16 PM
Verifying X509Certificate signature Peter Ritchie [C# MVP] ASP .Net Security 8 08-05-2008 02:44 PM
Implementing .net RSA on the server *and* JavaScript RSA on the client gg ASP .Net 0 11-18-2004 10:29 PM
X509Certificate The remote server returned an error: (403) Forbidd JN ASP .Net Security 0 07-19-2004 10:23 AM
byte[] to X509Certificate help needed iksrazal Java 0 02-29-2004 12:58 PM



Advertisments