gpt4 book ai didi

java - 尝试在 Java 中初始化 Bouncy CaSTLe 密码时, key 长度不是 128/192/256 位

转载 作者:行者123 更新时间:2023-11-30 06:47:28 24 4
gpt4 key购买 nike

我正在尝试使用 Bouncy CaSTLe 1.58 (org.bouncycaSTLe:bcprov-jdk15on:1.58) 从密码开始加密负载:

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.SecureRandom;
import java.security.Security;

public class Scratch {
public static void main(String[] args) throws Exception {
int keyLength = 128;

Security.addProvider(new BouncyCastleProvider());

String password = "password";

SecureRandom randomGenerator = new SecureRandom();
byte[] salt = randomGenerator.generateSeed(128 / 8);
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 872791, keyLength);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
SecretKey passwordKey = secretKeyFactory.generateSecret(keySpec);
System.out.println("passwordKey: " + passwordKey);
System.out.println("passwordKey.getEncoded(): " + Arrays.toString(passwordKey.getEncoded()));
System.out.println("passwordKey.getEncoded().length: " + passwordKey.getEncoded().length);
System.out.println("passwordKey.getFormat():" + passwordKey.getFormat());
System.out.println("passwordKey.getAlgorithm(): " + passwordKey.getAlgorithm());

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
PBEParameterSpec parSpec = new PBEParameterSpec(salt, 872791);
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parSpec);
}
}

这是我得到的错误:

Exception in thread "main" org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$InvalidKeyOrParametersException: Key length not 128/192/256 bits.
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown Source)
at javax.crypto.Cipher.init(Cipher.java:1394)
at javax.crypto.Cipher.init(Cipher.java:1327)
at tech.dashman.dashman.Scratch.main(Scratch.java:30)
Caused by: java.lang.IllegalArgumentException: Key length not 128/192/256 bits.
at org.bouncycastle.crypto.engines.AESEngine.generateWorkingKey(Unknown Source)
at org.bouncycastle.crypto.engines.AESEngine.init(Unknown Source)
at org.bouncycastle.crypto.modes.GCMBlockCipher.init(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$AEADGenericBlockCipher.init(Unknown Source)
... 4 more

调试输出如下所示:

passwordKey: com.sun.crypto.provider.PBKDF2KeyImpl@f00a68fe
passwordKey.getEncoded(): [122, -75, -99, 114, -123, 71, 6, 50, 45, 64, -97, 10, -66, 7, 110, 17]
passwordKey.getEncoded().length: 16
passwordKey.getFormat():RAW
passwordKey.getAlgorithm(): PBKDF2WithHmacSHA512

我错过了什么?

最佳答案

对于 AES,您需要一个基于 PBE key 的 AES key :

SecretKeyFactory secKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
KeySpec spec = new PBEKeySpec(password, salt, iterations, 128);
SecretKey pbeSecretKey = secKeyFactory.generateSecret(spec);
SecretKey aesSecret = new SecretKeySpec(pbeSecretKey.getEncoded(), "AES");

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding","BC");

关于java - 尝试在 Java 中初始化 Bouncy CaSTLe 密码时, key 长度不是 128/192/256 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943113/

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