Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Initializing Cipher with Certificate instead of SecretKey?

843810May 6 2003 — edited May 16 2003
Hi there folks!

Been struggling a while with this one. I need to initialize my Cipher with a certificate instead of a SecretKey object. I use JCE 1.2.2 and JDK1.3.1. I created my X.509 certificate with the keytool, selecting default values for it, and then exported it to a .CER file. I can read the certificate in, but when I try to initialize the Cipher, it throws the following exception:

java.security.InvalidKeyException: Wrong algorithm: DES required
at com.sun.crypto.provider.SunJCE_ad.a(DashoA6275)
at com.sun.crypto.provider.SunJCE_ab.a(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineInit(DashoA6275)
at javax.crypto.Cipher.init(DashoA6275)
at javax.crypto.Cipher.init(DashoA6275)
at coza.arivia.encryption.EncryptWithX509.main(EncryptWithX509.java:40)

I surmise that the error lies in my choice of transformation, namely "DES", at the line:
Cipher desCipher = Cipher.getInstance("DES");

But if not DES, then what else should I use?!

Many thanks for ANY help forthcoming! Here's the code:




public static void main(String args[]) {
try {
java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());

//My X.509 certificate
InputStream inStream = new FileInputStream("C:/DevRoot/EncryptionTest/mycert_DER_Encoded.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
inStream.close();

// Initialize the cipher for encryption
Cipher desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, cert);

// The cleartext
byte[] cleartext = "Forgo the Inevitable Cornflakes!".getBytes();

// Encrypt the cleartext
byte[] ciphertext = desCipher.doFinal(cleartext);

// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, cert);

// Decrypt the ciphertext
byte[] decryptedText = desCipher.doFinal(ciphertext);

System.out.println("Clear text = " + new String(cleartext));
System.out.println("Cipher text = " + new String(ciphertext));
System.out.println("Decrypted text = " + new String(decryptedText));
}
catch (java.security.cert.CertificateException e) {
e.printStackTrace();
}
catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (java.security.InvalidKeyException e) {
e.printStackTrace();
}
catch (javax.crypto.IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e) {
e.printStackTrace();
}
catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2003
Added on May 6 2003
7 comments
1,906 views