2017-08-29 330 views
0

我試圖用充氣城堡1.58加密的密碼開始有效載荷來初始化充氣城堡密碼時,非法密鑰大小(org.bouncycastle:bcprov-jdk15on :1.58):密鑰長度不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 { 

     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) 

缺少什麼我在這裏?什麼尺寸應該是關鍵?

+0

問題必須是'passwordKey' – Jens

+0

@jen爲什麼呢?以什麼方式? – Pablo

+0

可以請你分享[MCVE] – Jens

回答

3

您需要安裝無限擴展加密如果要使用AES密鑰大小> 128位。唯一的例外實際上是告訴你:

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) 

它的失敗對加密權限檢查上線1039 Cipher.java

嘗試將密鑰長度設置爲128位或安裝無限密鑰強度政策,您可以下載here

+1

哦!我知道,我忘記了無限關鍵力量政策。現在,我添加了它,我停止獲取非法密鑰大小,但我仍然遇到了另一個錯誤。我要接受這個答案,並創建另一個問題來保持獨立。 – Pablo

相關問題