0

我一直在编写javax.crypto.Cipher代码,并且 StackOverflow 回答了很多问题,所以我很欣赏这个站点中包含的信息。我确实有一个问题,我在这个网站上没有找到答案。

以下代码生成一个 SecretkeySpec:

try {
        keyGen = KeyGenerator.getInstance("AES");
}
catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
}

secureRandomCipher = new SecureRandom();
seed = new Random().nextLong();
secureRandomCipher.setSeed(seed);

try {
    secureRandomIV = SecureRandom.getInstance("SHA1PRNG");
}
catch (NoSuchAlgorithmException noSuchAlgorithm) {
    System.out.println(noSuchAlgorithm.getMessage());
}

keyGen.init(128, secureRandomCipher);
Key encryptionKey = keyGen.generateKey();

encryptionKey 是一个带有“AES”算法的 SecretKeySpec。

我是否应该明确说明“SecretKeySpec”的使用,例如

Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm());

或者我所做的是否足够。从代码长度的角度来看,我可以看到一个优势。

4

0 回答 0