为什么在 Java 中从密码派生密钥时需要 SecretKeySpec?
2022-09-03 04:06:09
Java中的类与类有什么区别?SecretKey
SecretKeySpec
的文档说:SecretKeySpec
它可以用来从字节数组构造密钥
在这段代码中,如果我在十六进制中打印或,那么两者都给出相同的输出。那么为什么我们需要 ?secretKey.getEncoded()
secret.getEncoded()
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");
下面是两个调用的输出:getEncoded()
00367171843C185C043DDFB90AA97677F11D02B629DEAFC04F935419D832E697