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!

Problems unwrapping a key

843810Apr 5 2002 — edited Jul 18 2002
Any help here is appreciated. I wish to password protect a private key. So I want to wrap it using a PBE ciper then unwrap for use later. The simple test case I write (see below) fails with the following exception:

java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineInit(DashoA6275)
at javax.crypto.Cipher.init(DashoA6275)
at WrapTest.main(WrapTest.java:25)

How is is possible that I can't initialize the cipher for unwrap with THE EXACT SAME key I just used for wrapping?? Another bothersome question - why can I get an instance of the following cipher: PBEWithMD5AndTripleDES, but I can't generate a SecretKey for it? Is Sun messed up here, or am I?

Code follows:
//-------------------------------------------------
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;

public class WrapTest {
public static void main(String args[]) {
Provider sunJce = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJce);
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
KeyPair kp = kpg.generateKeyPair();

PBEKeySpec pass = new PBEKeySpec("mypass".toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey s = skf.generateSecret(pass);

Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
c.init(Cipher.WRAP_MODE, s);
byte[] wrapped = c.wrap(kp.getPrivate());

//re-init for unwrap DIES HERE
c.init(Cipher.UNWRAP_MODE, s);

//should recover my private key
Key myUnwrappedKey = c.unwrap(wrapped, "DSA", Cipher.PRIVATE_KEY);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 15 2002
Added on Apr 5 2002
6 comments
237 views