gpt4 book ai didi

java - PBEWithHmacSHA512AndAES_128 和 AES 模式(如 GCM)

转载 作者:行者123 更新时间:2023-11-30 02:35:28 26 4
gpt4 key购买 nike

首先,我已成功使用 Java (JDK 8) 编写了使用 PBEWithHmacSHA512AndAES_128 进行加密和解密的代码。

但我想知道如果是 AES 那么我如何使用 GCM 这样的模式来检查完整性。

另一方面 - 我可以将 AES/GCM/NoPaddingPBKDF2WithHmacSHA256 结合使用。意味着 key 是使用 PBKDF2WithHmacSHA256 生成的,并在 AES/GCM 中使用。

  1. 但我正在努力寻找使用 PBEWithHmacSHA512AndAES_128 生成 key 并使用 AES/GCM 的源或者即使这是可能的或者是否有意义?

  2. 其次,使用 PBEWithHmacSHA512AndAES_128 生成的 key 始终为 9 字节 - 如果是这种情况,那么我想知道 AES 128 需要 16 字节大小的 key ,以及如何将 key 生成为 9 字节?

非常感谢这方面的任何帮助/澄清......

使用PBEWithHmacSHA512AndAES_128编写代码

private byte[] getRandomNumber(final int size) throws NoSuchAlgorithmException {
SecureRandom secureRandom = SecureRandom.getInstanceStrong();
byte[] randomBytes = new byte[size];
secureRandom.nextBytes(randomBytes);
return randomBytes;
}


private SecretKey getPBE_AES_Key(final String password, final byte[] salt) {
try {
char[] passwdData = password.toCharArray();

PBEKeySpec pbeKeySpec = new PBEKeySpec(passwdData, salt, 4096, 128);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
return pbeKey; // <-- size of this byte array is 9 - I thought it should be 16 since its AES
} catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
throw new OperationFailedException(ex.getMessage(), ex);
}
}


public String encrypt_PBE_AES(final String plaintext, final String password) {
try {
byte[] ivBytes = getRandomNumber(16);
byte[] saltBytes = getRandomNumber(16);
byte[] dataToEncrypt = plaintext.getBytes("UTF-8");

Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(saltBytes, 4096, ivParameterSpec);

cipher.init(Cipher.ENCRYPT_MODE, getPBE_AES_Key(password, saltBytes), pbeParameterSpec);
byte[] encryptedData = cipher.doFinal(dataToEncrypt);

byte[] ivWithSalt = ArrayUtils.addAll(ivBytes, saltBytes);
byte[] encryptedDataWithIVAndSalt = ArrayUtils.addAll(ivWithSalt, encryptedData);
String encodedData = Base64.getUrlEncoder().encodeToString(encryptedDataWithIVAndSalt);
return encodedData;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | IOException | InvalidAlgorithmParameterException ex) {
throw new OperationFailedException(ex.getMessage(), ex);
}
}

public String decrypt_PBE_AES(final String ciphertext, final String password) {
try {
byte[] encryptedDataWithIVAndSalt = Base64.getUrlDecoder().decode(ciphertext);
byte[] ivBytes = ArrayUtils.subarray(encryptedDataWithIVAndSalt, 0, 16);
byte[] saltBytes = ArrayUtils.subarray(encryptedDataWithIVAndSalt, 16,
16 + 16);
byte[] dataToDecrypt = ArrayUtils.subarray(encryptedDataWithIVAndSalt,
16 + 16, encryptedDataWithIVAndSalt.length);

Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(saltBytes, 4096, ivParameterSpec);

cipher.init(Cipher.DECRYPT_MODE, getPBE_AES_Key(password, saltBytes), pbeParameterSpec);
byte[] decryptedData = cipher.doFinal(dataToDecrypt);

return new String(decryptedData, "UTF-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException
| IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException ex) {
throw new OperationFailedException(ex.getMessage(), ex);
}
}

如您所见,有 2 个问题...

a) 在我的代码中,我将 IV 和盐与密文一起保存。我想使用 AES/GCM 检查整个 IV + 盐的完整性。

b) 为什么key的byte[]是9个字节? (当我以 Pree@2017 形式输入时,生成的 key 为 9 个字节 - 我检查了 pbeKey.getEncoded() 的长度及其 9。

非常感谢

更新 - 第一个问题的答案如下。然而,第二个问题在 https://crypto.stackexchange.com/questions/46849/pbewithhmacsha512andaes-128-and-aes-modes-like-gcm 得到了解答。

谢谢大家!

最佳答案

On other hand - I am able to use AES/GCM/NoPadding in conjunction with PBKDF2WithHmacSHA256.

完美。您似乎想要默认为 AES-128。如果正确实现,上面的方法几乎没有什么问题,并且更改为 SHA-512 在安全方面不会对您有太大帮助(如果有的话)。

But I am struggling to find sources that generate key using PBEWithHmacSHA512AndAES_128 and use AES/GCM Or even if it's possible or if it makes sense?

AES_128 已表明该模式不使用完整性。这是一种全有或全无的方案,默认采用 CBC。我会保留你所拥有的,如上所述。

Secondly the key generated using PBEWithHmacSHA512AndAES_128 is always 9 bytes - if it's the case then I am wondering AES 128 needs key of size 16 bytes and how key is getting generated as 9 bytes?

这不可能是正确的。毫无疑问, key 是 128 位/16 字节,但您只是得到了错误的信息,例如尝试直接打印出底层字节数组,而不是首先将其转换为十六进制。

关于java - PBEWithHmacSHA512AndAES_128 和 AES 模式(如 GCM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43233296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com