Example usage for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags AES_128

List of usage examples for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags AES_128

Introduction

In this page you can find the example usage for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags AES_128.

Prototype

int AES_128

To view the source code for org.bouncycastle.bcpg SymmetricKeyAlgorithmTags AES_128.

Click Source Link

Usage

From source file:genkeys.java

License:Open Source License

private static String symmetricCipherName(int algorithm) throws PGPException {
    switch (algorithm) {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default:/*from   w  w w . j  a v a 2 s  . c o m*/
        throw new PGPException("unknown symmetric algorithm: " + algorithm);
    }
}

From source file:com.bekwam.resignator.util.CryptUtils.java

License:Apache License

private byte[] encrypt(byte[] clearData, char[] passPhrase)
        throws IOException, PGPException, NoSuchProviderException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    ///*from   w w w  .  j  ava2s .  c  om*/
    // armor makes the encrypted output more readable (includes header, footer, printable chars)
    //

    OutputStream out = bOut;
    out = new ArmoredOutputStream(out);

    //
    // The standard jre installation limits keysize to 128.  Use the unlimited jars to go higher.
    //
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
            new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_128)
                    .setSecureRandom(new SecureRandom()).setProvider("BC"));

    encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(passPhrase).setProvider("BC"));

    OutputStream encOut = encGen.open(out, clearData.length);

    encOut.write(clearData);
    encOut.close();

    out.close();

    return bOut.toByteArray();
}

From source file:divconq.pgp.PGPUtil.java

License:Open Source License

public static String getSymmetricCipherName(int algorithm) {
    switch (algorithm) {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.CAMELLIA_128:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_192:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_256:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default://w  ww .jav  a 2s .c  o m
        throw new IllegalArgumentException("unknown symmetric algorithm: " + algorithm);
    }
}

From source file:keygenerator.KeyGenerator.java

public final static PGPKeyRingGenerator generateKeyRingGenerator(String id, char[] pass, int s2kcount)
        throws Exception {
    // This object generates individual key-pairs.
    RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();

    // Boilerplate RSA parameters, no need to change anything
    // except for the RSA key-size (2048). You can use whatever
    // key-size makes sense for you -- 4096, etc.
    kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 2048, 12));

    // First create the master (signing) key with the generator.
    PGPKeyPair rsakp_sign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), new Date());
    // Then an encryption subkey.
    PGPKeyPair rsakp_enc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), new Date());

    // Add a self-signature on the id
    PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();

    // Add signed metadata on the signature.
    // 1) Declare its purpose
    signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);
    // 2) Set preferences for secondary crypto algorithms to use
    //    when sending messages to this key.
    signhashgen.setPreferredSymmetricAlgorithms(false, new int[] { SymmetricKeyAlgorithmTags.AES_256,
            SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128 });
    signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1,
            HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224, });
    // 3) Request senders add additional checksums to the
    //    message (useful when verifying unsigned messages.)
    signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);

    // Create a signature on the encryption subkey.
    PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
    // Add metadata to declare its purpose
    enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);

    // Objects used to encrypt the secret key.
    PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
    PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);

    // bcpg 1.48 exposes this API that includes s2kcount. Earlier
    // versions use a default of 0x60.
    PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha256Calc,
            s2kcount)).build(pass);//w w w .  j  av a2s  . co m

    // Finally, create the keyring itself. The constructor
    // takes parameters that allow it to generate the self
    // signature.

    BcPGPContentSignerBuilder signerBuilder = new BcPGPContentSignerBuilder(
            rsakp_sign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1);

    PGPKeyRingGenerator keyRingGen;
    keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign, id, sha1Calc,
            signhashgen.generate(), null, signerBuilder, pske);

    // Add our encryption subkey, together with its signature.
    keyRingGen.addSubKey(rsakp_enc, enchashgen.generate(), null);
    return keyRingGen;
}

From source file:org.apache.camel.converter.crypto.PGPDataFormatDynamicTest.java

License:Apache License

protected Map<String, Object> getHeaders() {
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(PGPDataFormat.KEY_USERID, "sdude@nowhere.net");
    headers.put(PGPDataFormat.KEY_USERIDS, Collections.singletonList("second"));
    headers.put(PGPDataFormat.SIGNATURE_KEY_USERID, "sdude@nowhere.net");
    headers.put(PGPDataFormat.KEY_PASSWORD, "sdude");
    headers.put(PGPDataFormat.SIGNATURE_KEY_PASSWORD, "sdude");
    headers.put(PGPDataFormat.ENCRYPTION_ALGORITHM, SymmetricKeyAlgorithmTags.AES_128);
    headers.put(PGPDataFormat.SIGNATURE_HASH_ALGORITHM, HashAlgorithmTags.SHA512);
    headers.put(PGPDataFormat.COMPRESSION_ALGORITHM, CompressionAlgorithmTags.ZLIB);
    return headers;
}

From source file:org.sufficientlysecure.keychain.operations.BenchmarkOperation.java

License:Open Source License

@NonNull
@Override//from w ww.j  a va  2  s. c  o  m
public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel,
        CryptoInputParcel cryptoInputParcel) {
    OperationLog log = new OperationLog();
    log.add(LogType.MSG_BENCH, 0);

    // random data
    byte[] buf = new byte[1024 * 1024 * 10];
    new Random().nextBytes(buf);

    Passphrase passphrase = new Passphrase("a");

    int numRepeats = 5;
    long totalTime = 0;

    // encrypt
    SignEncryptResult encryptResult;
    int i = 0;
    do {
        SignEncryptOperation op = new SignEncryptOperation(mContext, mProviderHelper,
                new ProgressScaler(mProgressable, i * (50 / numRepeats), (i + 1) * (50 / numRepeats), 100),
                mCancelled);
        PgpSignEncryptData data = new PgpSignEncryptData();
        data.setSymmetricPassphrase(passphrase);
        data.setSymmetricEncryptionAlgorithm(OpenKeychainSymmetricKeyAlgorithmTags.AES_128);
        SignEncryptParcel input = new SignEncryptParcel(data);
        input.setBytes(buf);
        encryptResult = op.execute(input, new CryptoInputParcel());
        log.add(encryptResult, 1);
        log.add(LogType.MSG_BENCH_ENC_TIME, 2,
                String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0));
        totalTime += encryptResult.getResults().get(0).mOperationTime;
    } while (++i < numRepeats);

    long encryptionTime = totalTime / numRepeats;
    totalTime = 0;

    // decrypt
    i = 0;
    do {
        DecryptVerifyResult decryptResult;
        PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mProviderHelper,
                new ProgressScaler(mProgressable, 50 + i * (50 / numRepeats), 50 + (i + 1) * (50 / numRepeats),
                        100));
        PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(encryptResult.getResultBytes());
        input.setAllowSymmetricDecryption(true);
        decryptResult = op.execute(input, new CryptoInputParcel(passphrase));
        log.add(decryptResult, 1);
        log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0));
        totalTime += decryptResult.mOperationTime;
    } while (++i < numRepeats);

    long decryptionTime = totalTime / numRepeats;
    totalTime = 0;

    int iterationsFor100ms;
    try {
        PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder()
                .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build();
        PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder(digestCalcProvider)
                .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());

        byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        int iterations = 0;
        while (iterations < 255 && totalTime < 100) {
            iterations += 1;

            S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations);
            totalTime = System.currentTimeMillis();
            decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k);
            totalTime = System.currentTimeMillis() - totalTime;

            if ((iterations % 10) == 0) {
                log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations),
                        Long.toString(totalTime));
            }

        }
        iterationsFor100ms = iterations;

    } catch (PGPException e) {
        Log.e(Constants.TAG, "internal error during benchmark", e);
        log.add(LogType.MSG_INTERNAL_ERROR, 0);
        return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log);
    }

    log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms));
    log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime / 1000.0));
    log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime / 1000.0));

    log.add(LogType.MSG_BENCH_SUCCESS, 0);
    return new BenchmarkResult(BenchmarkResult.RESULT_OK, log);
}

From source file:org.sufficientlysecure.keychain.support.KeyringBuilder.java

License:Open Source License

private static SignaturePacket createSignaturePacket(BigInteger signature) {
    MPInteger[] signatureArray = new MPInteger[] { new MPInteger(signature) };

    int signatureType = PGPSignature.POSITIVE_CERTIFICATION;
    int keyAlgorithm = SignaturePacket.RSA_GENERAL;
    int hashAlgorithm = HashAlgorithmTags.SHA1;

    SignatureSubpacket[] hashedData = new SignatureSubpacket[] {
            new SignatureCreationTime(false, SIGNATURE_DATE),
            new KeyFlags(false, KeyFlags.CERTIFY_OTHER + KeyFlags.SIGN_DATA),
            new KeyExpirationTime(false, TimeUnit.DAYS.toSeconds(2)),
            new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, false,
                    new int[] { SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192,
                            SymmetricKeyAlgorithmTags.AES_128, SymmetricKeyAlgorithmTags.CAST5,
                            SymmetricKeyAlgorithmTags.TRIPLE_DES }),
            new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_HASH_ALGS, false,
                    new int[] { HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1, HashAlgorithmTags.SHA384,
                            HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224 }),
            new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_COMP_ALGS, false,
                    new int[] { CompressionAlgorithmTags.ZLIB, CompressionAlgorithmTags.BZIP2,
                            CompressionAlgorithmTags.ZIP }),
            new Features(false, Features.FEATURE_MODIFICATION_DETECTION),
            createPreferencesSignatureSubpacket() };
    SignatureSubpacket[] unhashedData = new SignatureSubpacket[] {
            new IssuerKeyID(false, false, KEY_ID.toByteArray()) };
    byte[] fingerPrint = new BigInteger("522c", 16).toByteArray();

    return new SignaturePacket(signatureType, KEY_ID.longValue(), keyAlgorithm, hashAlgorithm, hashedData,
            unhashedData, fingerPrint, signatureArray);
}

From source file:org.tramaci.onionmail.PGPKeyGen.java

License:Open Source License

public static PGPKeyRingGenerator generateKeyRingGenerator(String id, char[] pass, int s2kcount, int nBits,
        int certainty, Date when) throws Exception {

    RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
    RSAKeyGenerationParameters kgp = new RSAKeyGenerationParameters(DEFAULT_PUBEXP, new SecureRandom(), nBits,
            certainty);//from w ww  .  j a  v a2 s . c  om
    kpg.init(kgp);
    PGPKeyPair rsakpSign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), when);
    PGPKeyPair rsakpEnc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), when);
    PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();

    signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);

    signhashgen.setPreferredSymmetricAlgorithms(false,
            new int[] { SymmetricKeyAlgorithmTags.CAST5, SymmetricKeyAlgorithmTags.AES_256,
                    SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.TWOFISH,
                    SymmetricKeyAlgorithmTags.AES_128 });

    signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1,
            HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224 });

    signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
    PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
    enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);

    PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);
    PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);

    PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha256Calc,
            s2kcount)).build(pass);

    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsakpSign, id,
            sha1Calc, signhashgen.generate(), null,
            new BcPGPContentSignerBuilder(rsakpSign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
            pske);

    keyRingGen.addSubKey(rsakpEnc, enchashgen.generate(), null);
    return keyRingGen;
}

From source file:ubicrypt.core.crypto.PGPEC.java

License:Open Source License

private static PGPKeyRingGenerator keyRingGenerator(final PGPKeyPair masterKey,
        final PBESecretKeyEncryptor encryptor) {
    // Add a self-signature on the id
    final PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();

    // Add signed metadata on the signature.
    // 1) Declare its purpose
    signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);
    // 2) Set preferences for secondary crypto algorithms to use
    //    when sending messages to this key.
    signhashgen.setPreferredSymmetricAlgorithms(false, new int[] { SymmetricKeyAlgorithmTags.AES_256,
            SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128 });
    signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA256,
            //                        HashAlgorithmTags.SHA1,
            HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224, });
    // 3) Request senders add additional checksums to the
    //    message (useful when verifying unsigned messages.)
    signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);

    try {/*from   w  w w. j a  v a 2 s.c  o  m*/
        return new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, masterKey, Utils.machineName(),
                new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1), signhashgen.generate(), null,
                new BcPGPContentSignerBuilder(PGPPublicKey.ECDSA, HashAlgorithmTags.SHA256), encryptor);
    } catch (final PGPException e) {
        Throwables.propagate(e);
    }
    return null;
}