gpt4 book ai didi

java - 如何正确使用 "PBEWithHmacSHA512AndAES_256"算法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:10 36 4
gpt4 key购买 nike

我正在做一些 Java 加密,但无法找到正确使用 PBEWithHmacSHA512AndAES_256 算法的方法。

加密似乎工作正常,但我无法正确初始化解密密码。

下面是一个演示该问题的简短程序。特别是,请参阅“问题”评论。

注:我看过this very helpful answer ,我可以使用该方案让事情正常进行,但我很想知道我在这里做错了什么。

import java.nio.charset.StandardCharsets;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public final class CryptQuestion {

private static final String ALGORITHM = "PBEWithHmacSHA512AndAES_256";
private static final int ITERATIONS = 1000; // Aside: not sure what is a good number, here.

public static void main(final String[] args) throws Exception {
final String message = "This is the secret message... BOO!";
System.out.println("Original : " + message);
final byte[] messageBytes = message.getBytes(StandardCharsets.US_ASCII);

final String password = "some password";
final byte[] salt = "would be random".getBytes(StandardCharsets.US_ASCII);

// Create the Key
final SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS);
SecretKey key = factory.generateSecret(keySpec);

// Build the encryption cipher.
final Cipher cipherEncrypt = Cipher.getInstance(ALGORITHM);
cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);

// Encrypt!
final byte[] ciphertext = cipherEncrypt.doFinal(messageBytes);
final byte[] iv = cipherEncrypt.getIV();

// Now for decryption... The receiving end will have as input:
// * ciphertext
// * IV
// * password
// * salt

// We just re-use 'key' from above, since it will be identical.

final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);
final IvParameterSpec ivParamSpec = new IvParameterSpec(iv);

// Build the decryption cipher.
final Cipher cipherDecrypt = Cipher.getInstance(ALGORITHM);
// PROBLEM: If I pass "ivParamSpec", I get "java.security.InvalidAlgorithmParameterException: Wrong parameter type: PBE expected"
// Whereas if I pass pbeParamSpec, I get "java.security.InvalidAlgorithmParameterException: Missing parameter type: IV expected"
// What to do?
cipherDecrypt.init(
Cipher.DECRYPT_MODE,
key,
ivParamSpec
//pbeParamSpec
);

final String decrypted = new String(
cipherDecrypt.doFinal(ciphertext),
StandardCharsets.US_ASCII);
System.out.println("Decrypted: " + decrypted);
}
}

最佳答案

// PROBLEM: If I pass "ivParamSpec", I get "java.security.InvalidAlgorithmParameterException: Wrong parameter type: PBE expected"
// Whereas if I pass pbeParamSpec, I get "java.security.InvalidAlgorithmParameterException: Missing parameter type: IV expected"
// What to do?
cipherDecrypt.init(
Cipher.DECRYPT_MODE,
key,
ivParamSpec
//pbeParamSpec
);

使用来自加密CipherAlgorithmParameters:

cipherDecrypt.init(
Cipher.DECRYPT_MODE,
key,
cipherEncrypt.getParameters()
);

如果您想要一种更简洁的方式,在解密站点不涉及cipherEncrypt,请将算法参数保存为一个字节,并将它们与 key 数据一起传输:

byte[]  algorithmParametersEncoded = cipherEncrypt.getParameters().getEncoded();

然后在解密站点重建它们:

AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(ALGORITHM);
algorithmParameters.init(algorithmParametersEncoded);

并使用 algorithmParameters 作为 Cipher.init()parameters 参数。

关于java - 如何正确使用 "PBEWithHmacSHA512AndAES_256"算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215274/

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