title: Key length not 128/192/256 bits when trying to initialize Bouncy Castle cipher in Java
body: <p>I'm trying to encrypt a payload starting from a password using Bouncy Castle 1.58 (org.bouncycastle:bcprov-jdk15on:1.58):</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>and this is the error I get:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>and the debugging output looks like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What am I missing?</p>