2017-08-29 107 views
1

我试图使用Bouncy Castle 1.58(org.bouncycastle:bcprov-jdk15on:1.58)从密码开始加密有效载荷:当尝试初始化Bouncy Castle密码时,密钥长度不是128/192/256位Java中

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: [email protected] 
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 

什么我错过了?

+0

其实是在256位的密钥?请注意,128位密钥与256位密钥一样安全,既不容易受到强力攻击。当然,更大的一把钥匙更像一辆红色汽车。请参阅{为什么大多数人使用256位加密而不是128位?](https://security.stackexchange.com/a/19762/5121)。 – zaph

+0

大小128,192和256都不起作用。 – Pablo

+0

鉴于错误消息:'由于:java.lang.IllegalArgumentException:密钥长度不是128/192/256 bits.'什么是失败的实际密钥? – zaph

回答

1

我发现了问题!在获得密钥工厂时,我没有指定提供者。更换:

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512"); 

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512", "BC"); 

制造这个例子的工作。

+1

如果这是解决方案,那么这个例外没有多大意义。当问题是提供者时,为什么Bouncy Castle会抛出一个无效的关键异常?查看'passwordKey.getEncoded()',一些字符被设置为高位。它们是否合并成一个代码点,使得密钥比显示的小?也许Bouncy Castle更宽容,把它当作一个字节数组而不结合字符? – jww

+0

@jww我相信Pablo做了更多的改变,实际上这个代码本身不应该帮助(它甚至可能会产生另一个异常,因为恕我直言,定义的关键规范不由BC提供) – gusto2

1

对于AES,你需要一个AES密钥,这是基于PBE密钥:

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"); 
相关问题