gt:             if (iCount <= 0) {
context: /*
   * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Oracle in the LICENSE file that accompanied this code.
   *
   * This code is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   * version 2 for more details (a copy is included in the LICENSE file that
   * accompanied this code).
   *
   * You should have received a copy of the GNU General Public License version
   * 2 along with this work; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  
  package com.sun.crypto.provider;
  
  import java.security.*;
  import java.security.spec.*;
  import javax.crypto.*;
  import javax.crypto.spec.*;
  
  /**
   * This class represents password-based encryption as defined by the PKCS #5
   * standard.
   *
   * @author Jan Luehe
   *
   *
   * @see javax.crypto.Cipher
   */
  final class PBES1Core {
  
      // the encapsulated DES cipher
      private CipherCore cipher;
      private MessageDigest md;
      private int blkSize;
      private String algo = null;
      private byte[] salt = null;
      private int iCount = 10;
  
      /**
       * Creates an instance of PBE Cipher using the specified CipherSpi
       * instance.
       *
       */
      PBES1Core(String cipherAlg) throws NoSuchAlgorithmException,
          NoSuchPaddingException {
          algo = cipherAlg;
          if (algo.equals("DES")) {
              cipher = new CipherCore(new DESCrypt(),
                                      DESConstants.DES_BLOCK_SIZE);
          } else if (algo.equals("DESede")) {
  
              cipher = new CipherCore(new DESedeCrypt(),
                                      DESConstants.DES_BLOCK_SIZE);
          } else {
              throw new NoSuchAlgorithmException("No Cipher implementation " +
                                                 "for PBEWithMD5And" + algo);
          }
          cipher.setMode("CBC");
          cipher.setPadding("PKCS5Padding");
          // get instance of MD5
          md = MessageDigest.getInstance("MD5");
      }
  
      /**
       * Sets the mode of this cipher. This algorithm can only be run in CBC
       * mode.
       *
       * @param mode the cipher mode
       *
       * @exception NoSuchAlgorithmException if the requested cipher mode is
       * invalid
       */
      void setMode(String mode) throws NoSuchAlgorithmException {
          cipher.setMode(mode);
      }
  
       /**
       * Sets the padding mechanism of this cipher. This algorithm only uses
       * PKCS #5 padding.
       *
       * @param padding the padding mechanism
       *
       * @exception NoSuchPaddingException if the requested padding mechanism
       * is invalid
       */
      void setPadding(String paddingScheme) throws NoSuchPaddingException {
          cipher.setPadding(paddingScheme);
      }
  
      /**
       * Returns the block size (in bytes).
       *
       * @return the block size (in bytes)
       */
      int getBlockSize() {
          return DESConstants.DES_BLOCK_SIZE;
      }
  
      /**
       * Returns the length in bytes that an output buffer would need to be in
       * order to hold the result of the next <code>update</code> or
       * <code>doFinal</code> operation, given the input length
       * <code>inputLen</code> (in bytes).
       *
       * <p>This call takes into account any unprocessed (buffered) data from a
       * previous <code>update</code> call, and padding.
       *
       * <p>The actual output length of the next <code>update</code> or
       * <code>doFinal</code> call may be smaller than the length returned by
       * this method.
       *
       * @param inputLen the input length (in bytes)
       *
       * @return the required output buffer size (in bytes)
       *
       */
      int getOutputSize(int inputLen) {
          return cipher.getOutputSize(inputLen);
      }
  
      /**
       * Returns the initialization vector (IV) in a new buffer.
       *
       * <p> This is useful in the case where a random IV has been created
       * (see <a href = "#init">init</a>),
       * or in the context of password-based encryption or
       * decryption, where the IV is derived from a user-supplied password.
       *
       * @return the initialization vector in a new buffer, or null if the
       * underlying algorithm does not use an IV, or if the IV has not yet
       * been set.
       */
      byte[] getIV() {
          return cipher.getIV();
      }
  
      /**
       * Returns the parameters used with this cipher.
       *
       * <p>The returned parameters may be the same that were used to initialize
       * this cipher, or may contain the default set of parameters or a set of
       * randomly generated parameters used by the underlying cipher
       * implementation (provided that the underlying cipher implementation
       * uses a default set of parameters or creates new parameters if it needs
       * parameters but was not initialized with any).
       *
       * @return the parameters used with this cipher, or null if this cipher
       * does not use any parameters.
       */
      AlgorithmParameters getParameters() {
          AlgorithmParameters params = null;
          if (salt == null) {
              salt = new byte[8];
              SunJCE.getRandom().nextBytes(salt);
          }
          PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
          try {
              params = AlgorithmParameters.getInstance("PBEWithMD5And" +
                  (algo.equalsIgnoreCase("DES")? "DES":"TripleDES"), "SunJCE");
          } catch (NoSuchAlgorithmException nsae) {
              // should never happen
              throw new RuntimeException("SunJCE called, but not configured");
          } catch (NoSuchProviderException nspe) {
              // should never happen
              throw new RuntimeException("SunJCE called, but not configured");
          }
          try {
              params.init(pbeSpec);
          } catch (InvalidParameterSpecException ipse) {
              // should never happen
              throw new RuntimeException("PBEParameterSpec not supported");
          }
          return params;
      }
  
      /**
       * Initializes this cipher with a key, a set of
       * algorithm parameters, and a source of randomness.
       * The cipher is initialized for one of the following four operations:
       * encryption, decryption, key wrapping or key unwrapping, depending on
       * the value of <code>opmode</code>.
       *
       * <p>If this cipher (including its underlying feedback or padding scheme)
       * requires any random bytes, it will get them from <code>random</code>.
       *
       * @param opmode the operation mode of this cipher (this is one of
       * the following:
       * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
       * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
       * @param key the encryption key
       * @param params the algorithm parameters
       * @param random the source of randomness
       *
       * @exception InvalidKeyException if the given key is inappropriate for
       * initializing this cipher
       * @exception InvalidAlgorithmParameterException if the given algorithm
       * parameters are inappropriate for this cipher
       */
      void init(int opmode, Key key, AlgorithmParameterSpec params,
                SecureRandom random)
          throws InvalidKeyException, InvalidAlgorithmParameterException {
          if (((opmode == Cipher.DECRYPT_MODE) ||
               (opmode == Cipher.UNWRAP_MODE)) && (params == null)) {
              throw new InvalidAlgorithmParameterException("Parameters "
                                                           + "missing");
          }
          if ((key == null) ||
              (key.getEncoded() == null) ||
              !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
              throw new InvalidKeyException("Missing password");
          }
  
          if (params == null) {
              // create random salt and use default iteration count
              salt = new byte[8];
              random.nextBytes(salt);
          } else {
              if (!(params instanceof PBEParameterSpec)) {
                  throw new InvalidAlgorithmParameterException
                      ("Wrong parameter type: PBE expected");
              }
              salt = ((PBEParameterSpec) params).getSalt();
              // salt must be 8 bytes long (by definition)
              if (salt.length != 8) {
                  throw new InvalidAlgorithmParameterException
                      ("Salt must be 8 bytes long");
              }
              iCount = ((PBEParameterSpec) params).getIterationCount();
  