code: protected static PKCS8EncodedKeySpec generateKeySpec(final char[] password, final byte[] key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidKeyException, InvalidAlgorithmParameterException {
  
      if (password == null || password.length == 0) {
        return new PKCS8EncodedKeySpec(key);
      }
  
      EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
      SecretKeyFactory keyFactory = SecretKeyFactory
          .getInstance(encryptedPrivateKeyInfo.getAlgName());
      PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
      SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
  
      Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
      cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
  
      return encryptedPrivateKeyInfo.getKeySpec(cipher);
    }
docs: Generates a key specification for an (encrypted) private key.
  
  @param password characters, if {@code null} or empty an unencrypted key is assumed
  @param key bytes of the DER encoded private key
  
  @return a key specification
  
  @throws IOException if parsing {@code key} fails
  @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
  @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is
  unkown
  @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be
  generated
  @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to
  decrypt
  {@code key}
  @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow
  faulty