Je suis en train de chiffrer une charge utile à partir d'un mot de passe avec Bouncy Castle 1,58 (org.bouncycastle: bcprov-jdk15on: 1,58):longueur de clé non 128/192/256 bits lors de la tentative d'initialisation de chiffrement Bouncy Castle en 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);
}
}
et ceci est l'erreur que je reçois:
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
et la sortie de débogage ressemble à ceci:
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
Quelle Je manque?
est la clé en fait 256 bits? Notez qu'une clé de 128 bits est aussi sécurisée qu'une clé de 256 bits, c'est-à-dire qu'aucune n'est susceptible d'attaques par force brute. Bien sûr, une clé plus grande est plus viril de la même manière qu'une voiture rouge. Voir {Pourquoi la plupart des gens utilisent un cryptage 256 bits au lieu de 128 bits?] (Https://security.stackexchange.com/a/19762/5121). – zaph
Ni la taille, 128, 192 ni 256 fonctionnent. – Pablo
Étant donné le message d'erreur: 'Causé par: java.lang.IllegalArgumentException: Longueur de la clé pas 128/192/256 bits.' Quelle est la clé réelle qui échoue? – zaph