示例#1
0
 public KeyPairGenerator createKeyPairGenerator(String algorithm) throws GeneralSecurityException {
   try {
     return helper.createKeyPairGenerator(algorithm);
   } catch (NoSuchAlgorithmException e) {
     throw new GeneralSecurityException("cannot find algorithm: " + e.getMessage());
   } catch (NoSuchProviderException e) {
     throw new GeneralSecurityException("cannot find provider: " + e.getMessage());
   }
 }
示例#2
0
 private Signature createSignature(String cipherName) throws PGPException {
   try {
     return helper.createSignature(cipherName);
   } catch (NoSuchAlgorithmException e) {
     throw new PGPException("cannot create signature: " + e.getMessage(), e);
   } catch (NoSuchProviderException e) {
     throw new PGPException("cannot create signature: " + e.getMessage(), e);
   }
 }
示例#3
0
 KeyFactory createKeyFactory(String algorithm) throws GeneralSecurityException, PGPException {
   try {
     return helper.createKeyFactory(algorithm);
   } catch (NoSuchAlgorithmException e) {
     throw new PGPException("cannot find algorithm: " + e.getMessage(), e);
   } catch (NoSuchProviderException e) {
     throw new PGPException("cannot find provider: " + e.getMessage(), e);
   }
 }
示例#4
0
 Cipher createCipher(String cipherName) throws PGPException {
   try {
     return helper.createCipher(cipherName);
   } catch (NoSuchAlgorithmException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchPaddingException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchProviderException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   }
 }
示例#5
0
  MessageDigest createDigest(int algorithm) throws GeneralSecurityException, PGPException {
    MessageDigest dig;

    try {
      dig = helper.createDigest(PGPUtil.getDigestName(algorithm));
    } catch (NoSuchAlgorithmException e) {
      throw new PGPException("cannot find algorithm: " + e.getMessage(), e);
    } catch (NoSuchProviderException e) {
      throw new PGPException("cannot find provider: " + e.getMessage(), e);
    }

    return dig;
  }
示例#6
0
 Cipher createKeyWrapper(int encAlgorithm) throws PGPException {
   try {
     switch (encAlgorithm) {
       case SymmetricKeyAlgorithmTags.AES_128:
       case SymmetricKeyAlgorithmTags.AES_192:
       case SymmetricKeyAlgorithmTags.AES_256:
         return helper.createCipher("AESWrap");
       case SymmetricKeyAlgorithmTags.CAMELLIA_128:
       case SymmetricKeyAlgorithmTags.CAMELLIA_192:
       case SymmetricKeyAlgorithmTags.CAMELLIA_256:
         return helper.createCipher("CamelliaWrap");
       default:
         throw new PGPException("unknown wrap algorithm: " + encAlgorithm);
     }
   } catch (NoSuchAlgorithmException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchPaddingException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   } catch (NoSuchProviderException e) {
     throw new PGPException("cannot create cipher: " + e.getMessage(), e);
   }
 }
示例#7
0
 public AlgorithmParameters createAlgorithmParameters(String algorithm)
     throws NoSuchProviderException, NoSuchAlgorithmException {
   return helper.createAlgorithmParameters(algorithm);
 }
  public OutputEncryptor build(final char[] password) throws OperatorCreationException {
    final Cipher cipher;
    SecretKey key;

    if (random == null) {
      random = new SecureRandom();
    }

    final AlgorithmIdentifier encryptionAlg;
    final byte[] salt = new byte[20];
    final int iterationCount = 1024;

    random.nextBytes(salt);

    try {
      if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
        PBEKeySpec pbeSpec = new PBEKeySpec(password);

        SecretKeyFactory keyFact = helper.createSecretKeyFactory(algorithm.getId());

        PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);

        key = keyFact.generateSecret(pbeSpec);

        cipher = helper.createCipher(algorithm.getId());

        cipher.init(Cipher.ENCRYPT_MODE, key, defParams);

        encryptionAlg =
            new AlgorithmIdentifier(algorithm, new PKCS12PBEParams(salt, iterationCount));
      } else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
        SecretKeyFactory keyFact =
            helper.createSecretKeyFactory(PKCSObjectIdentifiers.id_PBKDF2.getId());

        key =
            keyFact.generateSecret(
                new PBEKeySpec(
                    password,
                    salt,
                    iterationCount,
                    keySizeProvider.getKeySize(new AlgorithmIdentifier(keyEncAlgorithm))));

        cipher = helper.createCipher(keyEncAlgorithm.getId());

        cipher.init(Cipher.ENCRYPT_MODE, key, random);

        PBES2Parameters algParams =
            new PBES2Parameters(
                new KeyDerivationFunc(
                    PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount)),
                new EncryptionScheme(
                    keyEncAlgorithm,
                    ASN1Primitive.fromByteArray(cipher.getParameters().getEncoded())));

        encryptionAlg = new AlgorithmIdentifier(algorithm, algParams);
      } else {
        throw new OperatorCreationException("unrecognised algorithm");
      }

      return new OutputEncryptor() {
        public AlgorithmIdentifier getAlgorithmIdentifier() {
          return encryptionAlg;
        }

        public OutputStream getOutputStream(OutputStream out) {
          return new CipherOutputStream(out, cipher);
        }

        public GenericKey getKey() {
          if (isPKCS12(encryptionAlg.getAlgorithm())) {
            return new GenericKey(
                encryptionAlg, PBEParametersGenerator.PKCS5PasswordToBytes(password));
          } else {
            return new GenericKey(
                encryptionAlg, PBEParametersGenerator.PKCS12PasswordToBytes(password));
          }
        }
      };
    } catch (Exception e) {
      throw new OperatorCreationException("unable to create OutputEncryptor: " + e.getMessage(), e);
    }
  }