This
exception thrown for invalid key specifications.
For
Example in the below program, I tried to generate specification for
DES key, using PBEKeySpec, which is invalid.
import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; public class InvalidKeySpecExceptionEx { public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecretKey key = keyGen.generateKey(); PBEKeySpec spec = new PBEKeySpec(new char[] {'a'}); KeySpec specification = keyFactory.getKeySpec(key, spec.getClass()); } }
When
tries to run the above program, below error thrown.
Exception in thread "main" java.security.spec.InvalidKeySpecException: Inappropriate key specification at com.sun.crypto.provider.DESKeyFactory.engineGetKeySpec(DESKeyFactory.java:113) at javax.crypto.SecretKeyFactory.getKeySpec(SecretKeyFactory.java:380) at InvalidKeySpecExceptionEx.main(InvalidKeySpecExceptionEx.java:19) Java Result: 1
No comments:
Post a Comment