密钥长度不是 128/192/256 位,或者在 Java 中尝试初始化 Bouncy Castle 密码时密钥大小非法

Key length not 128/192/256 bits, or Illegal key size when trying to initialize Bouncy Castle cipher in Java

我正在尝试使用 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 {

        Security.addProvider(new BouncyCastleProvider());

        String password = "password";

        SecureRandom randomGenerator = new SecureRandom();
        byte[] salt = randomGenerator.generateSeed(256);
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 32);
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey passwordKey = f.generateSecret(keySpec);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
        PBEParameterSpec parSpec = new PBEParameterSpec(salt, 65536);
        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

如果我将 PBKeySpec 调用中的密钥长度更改为 256,如下所示:

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 {

        Security.addProvider(new BouncyCastleProvider());

        String password = "password";

        SecureRandom randomGenerator = new SecureRandom();
        byte[] salt = randomGenerator.generateSeed(256);
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey passwordKey = f.generateSecret(keySpec);

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

然后我得到这个错误:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
    at javax.crypto.Cipher.init(Cipher.java:1393)
    at javax.crypto.Cipher.init(Cipher.java:1327)
    at tech.dashman.dashman.Scratch.main(Scratch.java:29)

我在这里错过了什么?什么尺寸应该是关键?

如果您想使用密钥大小大于 128 位的 AES,则需要安装无限加密扩展。异常实际上是在告诉你:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
    at javax.crypto.Cipher.init(Cipher.java:1393)
    at javax.crypto.Cipher.init(Cipher.java:1327)
    at tech.dashman.dashman.Scratch.main(Scratch.java:29)

Cipher.java

第 1039 行的加密权限检查失败

尝试将密钥长度设置为 128 位或安装您可以下载的无限制密钥强度策略here