예제 #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;
    }
예제 #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(
        BCPBEKey pbeKey, AlgorithmParameterSpec spec) {
      if ((spec == null) || !(spec instanceof PBEParameterSpec)) {
        throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
      }

      PBEParameterSpec pbeParam = (PBEParameterSpec) spec;
      PBEParametersGenerator generator = makePBEGenerator(pbeKey.getType(), pbeKey.getDigest());
      byte[] key = pbeKey.getEncoded();
      CipherParameters param;

      if (pbeKey.shouldTryWrongPKCS12()) {
        key = new byte[2];
      }

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

      param = generator.generateDerivedMacParameters(pbeKey.getKeySize());

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

      return param;
    }
예제 #3
0
  static SecretKey generateSecretKeyForPKCS5Scheme2(
      String algorithm, char[] password, byte[] salt, int iterationCount) {
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();

    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);

    return new SecretKeySpec(
        ((KeyParameter) generator.generateDerivedParameters(PEMUtilities.getKeySize(algorithm)))
            .getKey(),
        algorithm);
  }
예제 #4
0
 private static CipherParameters extractPBES2CipherParams(
     char[] password, PBES2Parameters pbeParams) {
   PBKDF2Params pbkdfParams =
       PBKDF2Params.getInstance(pbeParams.getKeyDerivationFunc().getParameters());
   int keySize = 192;
   if (pbkdfParams.getKeyLength() != null) {
     keySize = pbkdfParams.getKeyLength().intValue() * 8;
   }
   int iterationCount = pbkdfParams.getIterationCount().intValue();
   byte[] salt = pbkdfParams.getSalt();
   PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
   generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
   return generator.generateDerivedParameters(keySize);
 }
예제 #5
0
파일: PBE.java 프로젝트: hiro2k/bc-java
    /**
     * 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;
    }
예제 #6
0
    /** construct a key and iv (if necessary) suitable for use with a Cipher. */
    public static CipherParameters makePBEParameters(
        BCPBEKey pbeKey, AlgorithmParameterSpec spec, String targetAlgorithm) {
      if ((spec == null) || !(spec instanceof PBEParameterSpec)) {
        throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
      }

      PBEParameterSpec pbeParam = (PBEParameterSpec) spec;
      PBEParametersGenerator generator = makePBEGenerator(pbeKey.getType(), pbeKey.getDigest());
      byte[] key = pbeKey.getEncoded();
      CipherParameters param;

      if (pbeKey.shouldTryWrongPKCS12()) {
        key = new byte[2];
      }

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

      if (pbeKey.getIvSize() != 0) {
        param = generator.generateDerivedParameters(pbeKey.getKeySize(), pbeKey.getIvSize());
      } else {
        param = generator.generateDerivedParameters(pbeKey.getKeySize());
      }

      if (targetAlgorithm.startsWith("DES")) {
        if (param instanceof ParametersWithIV) {
          KeyParameter kParam = (KeyParameter) ((ParametersWithIV) param).getParameters();

          DESParameters.setOddParity(kParam.getKey());
        } else {
          KeyParameter kParam = (KeyParameter) param;

          DESParameters.setOddParity(kParam.getKey());
        }
      }

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

      return param;
    }