Esempio n. 1
0
    /** construct a key and iv (if necessary) suitable for use with a Cipher. */
    public static CipherParameters makePBEParameters(
        PBEKeySpec keySpec, int type, int hash, int keySize, int ivSize) {
      PBEParametersGenerator generator = makePBEGenerator(type, hash);
      byte[] key;
      CipherParameters param;

      if (type == PKCS12) {
        key = PBEParametersGenerator.PKCS12PasswordToBytes(keySpec.getPassword());
      } else {
        key = PBEParametersGenerator.PKCS5PasswordToBytes(keySpec.getPassword());
      }

      generator.init(key, keySpec.getSalt(), keySpec.getIterationCount());

      if (ivSize != 0) {
        param = generator.generateDerivedParameters(keySize, ivSize);
      } else {
        param = generator.generateDerivedParameters(keySize);
      }

      for (int i = 0; i != key.length; i++) {
        key[i] = 0;
      }

      return param;
    }
Esempio n. 2
0
    /**
     * generate a PBE based key suitable for a MAC algorithm, the key size is chosen according the
     * MAC size, or the hashing algorithm, whichever is greater.
     */
    public static CipherParameters makePBEMacParameters(
        PBEKeySpec keySpec, int type, int hash, int keySize) {
      PBEParametersGenerator generator = makePBEGenerator(type, hash);
      byte[] key;
      CipherParameters param;

      key = convertPassword(type, keySpec);

      generator.init(key, keySpec.getSalt(), keySpec.getIterationCount());

      param = generator.generateDerivedMacParameters(keySize);

      for (int i = 0; i != key.length; i++) {
        key[i] = 0;
      }

      return param;
    }