What is the difference between SecretKey vs SecretKeySpec in Java?
The SecretKeySpec documentation says:
it can be used to create SecretKey from an array of bytes
In this code, if I type secretKey.getEncoded() or secret.getEncoded() in hexadecimal, both give the same output. So why do we need SecretKeySpec ?
final String password = "test"; int pswdIterations = 65536 ; int keySize = 256; byte[] ivBytes; byte[] saltBytes = {0,1,2,3,4,5,6}; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec( password.toCharArray(), saltBytes, pswdIterations, keySize ); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(),"AES");
Here is the output of both calls to getEncoded() :
00367171843C185C043DDFB90AA97677F11D02B629DEAFC04F935419D832E697
java cryptography jce secret-key
Iley willow
source share