Example usage for org.bouncycastle.openpgp PGPKeyRingGenerator generateSecretKeyRing

List of usage examples for org.bouncycastle.openpgp PGPKeyRingGenerator generateSecretKeyRing

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPKeyRingGenerator generateSecretKeyRing.

Prototype

public PGPSecretKeyRing generateSecretKeyRing() 

Source Link

Document

Return the secret key ring.

Usage

From source file:SELSKeyGen.java

License:Open Source License

private static void exportKeyPair(OutputStream secretOut, OutputStream publicOut, KeyPair dsaKp, KeyPair elgKp,
        String identity, char[] passPhrase, boolean armor, int exptimesec)
        throws IOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException {
    if ((armor) && (secretOut != null)) {
        secretOut = new ArmoredOutputStream(secretOut);
    }// w  w  w .j  a v  a2 s  .  com

    //Create subpacket vector for expiration time

    PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator();
    int secondsToExpire = exptimesec;
    subpacketGenerator.setKeyExpirationTime(false, secondsToExpire);
    subpacketGenerator.setExportable(true, true);
    PGPSignatureSubpacketVector subpacketVector = subpacketGenerator.generate();

    PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date(), "BC");
    PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date(), "BC");

    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
            identity, PGPEncryptedData.AES_256, passPhrase, subpacketVector, null, new SecureRandom(), "BC");

    keyRingGen.addSubKey(elgKeyPair);

    if (secretOut != null) {
        keyRingGen.generateSecretKeyRing().encode(secretOut);
        secretOut.close();
    }

    if (armor) {
        publicOut = new ArmoredOutputStream(publicOut);
    }

    keyRingGen.generatePublicKeyRing().encode(publicOut);
    publicOut.close();
}

From source file:com.fuzion.tools.pgp.BCPGPKeyGenTools.java

License:Open Source License

public static final void exportSecretKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor)
        throws IOException {
    PGPSecretKeyRing pgpSecKeyRing = pgpKeyRingGen.generateSecretKeyRing();

    if (asciiArmor) {
        ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
        pgpSecKeyRing.encode(aos);/*from  w ww.ja va2s .  co m*/
        aos.close();
    } else {
        FileOutputStream fos = new FileOutputStream(keyFile);
        pgpSecKeyRing.encode(fos);
        fos.close();
    }
}

From source file:com.zwitserloot.ivyplusplus.mavencentral.CreateSigningKey_.java

License:Open Source License

void export(OutputStream privOut, OutputStream pubOut, KeyPair privPair_, KeyPair signPair_, String identity,
        String passphrase) throws PGPException, NoSuchProviderException, IOException {
    PGPKeyPair privPair = new PGPKeyPair(PGPPublicKey.DSA, privPair_, new Date());
    PGPKeyPair signPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, signPair_, new Date());

    PGPKeyRingGenerator ringGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, privPair,
            identity, PGPEncryptedData.AES_256, passphrase.toCharArray(), true, null, null, new SecureRandom(),
            "BC");
    ringGen.addSubKey(signPair);/*from   w ww.  ja va2 s .c o m*/
    ringGen.generateSecretKeyRing().encode(privOut);
    privOut.close();

    ringGen.generatePublicKeyRing().encode(pubOut);
    pubOut.close();
}

From source file:keygenerator.KeyGenerator.java

public static void main(String[] args) {
    char pass[] = { 'h', 'e', 'l', 'l', 'o' };
    try {/*  w  ww. j  ava  2  s.  c om*/
        PGPKeyRingGenerator krgen = generateKeyRingGenerator("alice@example.com", pass);

        // Generate public key ring, dump to file.
        PGPPublicKeyRing pkr = krgen.generatePublicKeyRing();
        BufferedOutputStream pubout = new BufferedOutputStream(new FileOutputStream("dummy.pkr"));
        pkr.encode(pubout);
        pubout.close();

        // Generate private key, dump to file.
        PGPSecretKeyRing skr = krgen.generateSecretKeyRing();
        BufferedOutputStream secout = new BufferedOutputStream(new FileOutputStream("dummy.skr"));
        skr.encode(secout);
        secout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.pgp2p.cryptoservice.PGPManager.java

License:Open Source License

/**
 * If the pubring and secring doesn't exist in the informed directory, 
 * they can be created with this method.
 * //from w ww .jav  a  2s .c om
 * @param String identity - the identification of the keypair (Ex.: "John Doe <john.doe@example.com>")
 * @param char[] password - the password that will protect the secring.
 */
private void createPGPKeyring(String identity, char[] password) {

    PGPKeyPair keyPair = createPGPKeyPair();

    PGPKeyRingGenerator keyRingGenerator = null;
    try {
        keyRingGenerator = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, keyPair, identity,
                PGPEncryptedData.AES_256, password, true, null, null, new SecureRandom(), "BC");
    } catch (NoSuchProviderException nspe) {
        nspe.printStackTrace();
    } catch (PGPException pe) {
        pe.printStackTrace();
    }

    try {
        OutputStream secring = new FileOutputStream(
                this.keyRingPath + System.getProperty("file.separator") + SECRING_FILE);
        //secring = new ArmoredOutputStream(secring);

        OutputStream pubring = new FileOutputStream(
                this.keyRingPath + System.getProperty("file.separator") + PUBRING_FILE);
        //pubring = new ArmoredOutputStream(pubring);

        keyRingGenerator.generateSecretKeyRing().encode(secring);
        keyRingGenerator.generatePublicKeyRing().encode(pubring);
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:org.kontalk.certgen.PGP.java

License:Open Source License

/** Creates public and secret keyring for a given keypair. */
public static PGPKeyPairRing store(PGPDecryptedKeyPairRing pair, String id, String passphrase)
        throws PGPException {

    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);
    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, pair.signKey,
            id, sha1Calc, null, null,//from  w  w  w.j a  v a  2  s.co m
            new JcaPGPContentSignerBuilder(pair.signKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1),
            new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider(PROVIDER)
                    .build(passphrase.toCharArray()));

    keyRingGen.addSubKey(pair.encryptKey);

    PGPSecretKeyRing secRing = keyRingGen.generateSecretKeyRing();
    PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();

    return new PGPKeyPairRing(pubRing, secRing);
}

From source file:org.pgptool.gui.encryption.implpgp.KeyGeneratorServicePgpImpl.java

License:Open Source License

@SuppressWarnings("deprecation")
private Key buildKey(PGPKeyRingGenerator keyRingGen) throws PGPException {
    Key ret = new Key();
    KeyDataPgp keyData = new KeyDataPgp();
    keyData.setPublicKeyRing(keyRingGen.generatePublicKeyRing());
    keyData.setSecretKeyRing(keyRingGen.generateSecretKeyRing());
    ret.setKeyData(keyData);//from   w  w  w  .j av  a 2 s  .c o  m
    ret.setKeyInfo(KeyFilesOperationsPgpImpl.buildKeyInfoFromSecret(keyData.getSecretKeyRing()));
    return ret;
}

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

License:Open Source License

public static String KeyGen(Date when, String ID, OutputStream Public, OutputStream Private) throws Exception {

    String pwl = J.GenPassword(DEFAULT_PASS_SIZE, DEFAULT_PASS_STRANGE);
    PGPKeyRingGenerator kg = generateKeyRingGenerator(ID, pwl.toCharArray(), when);

    PGPPublicKeyRing pkr = kg.generatePublicKeyRing();
    ArmoredOutputStream outStream = new ArmoredOutputStream(Public);
    pkr.encode(outStream);/*from  w  ww.  j  a va 2 s  .c  o m*/
    outStream.close();

    PGPSecretKeyRing skr = kg.generateSecretKeyRing();
    outStream = new ArmoredOutputStream(Private);
    skr.encode(outStream);
    outStream.close();

    return pwl;
}

From source file:portablepgp.core.PGPTools.java

License:Open Source License

public static final void exportSecretKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor)
        throws IOException {
    PGPSecretKeyRing pgpSecKeyRing = pgpKeyRingGen.generateSecretKeyRing();
    if (asciiArmor) {
        ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
        pgpSecKeyRing.encode(aos);/*w  w  w.  j  av  a  2s .  c om*/
        aos.close();
    } else {
        FileOutputStream fos = new FileOutputStream(keyFile);
        pgpSecKeyRing.encode(fos);
        fos.close();
    }
}

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

License:Open Source License

public static PGPSecretKeyRing createSecretKeyRing(final char[] passPhrase) {
    final PGPKeyRingGenerator gen = keyRingGenerator(masterKey(), passPhrase);
    try {/*from w  w  w .j a  v  a 2  s. c om*/
        gen.addSubKey(encryptionKey());
    } catch (final PGPException e) {
        Throwables.propagate(e);
    }
    return gen.generateSecretKeyRing();
}