Hi ,
I am trying to use JCE in my application. following are the code that i used for encription
public class EncryptionTest {
/**
* @param args
* @throws InvalidKeyException
*/
public static void main(String[] args) throws InvalidKeyException {
// TODO Auto-generated method stub
Key myKey = null;
try {
myKey = generateKey();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// get cipher object for password-based encryption
Cipher c=null;
try {
c = Cipher.getInstance("PBEWithMD5AndDES" , "SunJCE");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// initialize cipher for encryption, without supplying
// any parameters. Here, "myKey" is assumed to refer
// to an already-generated key.
c.init(Cipher.ENCRYPT_MODE , myKey);
// encrypt some data and store away ciphertext
// for later decryption
try {
byte[] cipherText = c.doFinal("This is just an example".getBytes());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// retrieve parameters generated by underlying cipher
// implementation
AlgorithmParameters algParams = c.getParameters();
// get parameter encoding and store it away
try {
byte[] encodedAlgParams = algParams.getEncoded();
System.out.println("Size Of" +encodedAlgParams.length);
System.out.println("Execution Over !!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
While executing the code its giving the following error
java.security.InvalidKeyException: Missing password
at com.sun.crypto.provider.SunJCE_ab.a(DashoA12275)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineInit(DashoA12275)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineInit(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
Its getting from the following line
c.init(Cipher.ENCRYPT_MODE , myKey);
I am very new to this kind of implementation pls help me to come out of this
Thanks and Regards
Rasa