Example usage for org.bouncycastle.openpgp PGPKeyRingGenerator generatePublicKeyRing

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

Introduction

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

Prototype

public PGPPublicKeyRing generatePublicKeyRing() 

Source Link

Document

Return the public key ring that corresponds to 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);
    }//from   w ww  . j  a va 2 s .  c o m

    //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 exportPublicKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor)
        throws IOException {
    PGPPublicKeyRing pgpPubKeyRing = pgpKeyRingGen.generatePublicKeyRing();

    if (asciiArmor) {
        ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
        pgpPubKeyRing.encode(aos);/*  w  w  w. j  av  a  2s. c om*/
        aos.close();
    } else {
        FileOutputStream fos = new FileOutputStream(keyFile);
        pgpPubKeyRing.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);/*  ww w.ja v  a  2 s.co  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 {/*from w w  w  . j  a  v  a2s . c  o m*/
        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   ww w.j  a v a2  s.com
 * @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 ww  w . j  a v a  2  s  .  c om
            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 www .jav a  2s.  com
    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);/*www.j a  v  a 2  s.  com*/
    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 exportPublicKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor)
        throws IOException {
    PGPPublicKeyRing pgpPubKeyRing = pgpKeyRingGen.generatePublicKeyRing();
    if (asciiArmor) {
        ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile));
        pgpPubKeyRing.encode(aos);/* w  w  w.ja  v a  2  s .  c  om*/
        aos.close();
    } else {
        FileOutputStream fos = new FileOutputStream(keyFile);
        pgpPubKeyRing.encode(fos);
        fos.close();
    }
}

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

License:Open Source License

@Test
public void signPK() throws Exception {
    final char[] password = "ciao".toCharArray();
    final PGPSecretKeyRing skr = PGPEC.createSecretKeyRing(password);
    final byte[] encSkr = skr.getEncoded();
    final PGPKeyPair keyPair = PGPEC.extractEncryptKeyPair(PGPEC.readSK(new ByteArrayInputStream(encSkr)),
            "ciao".toCharArray());
    final PGPKeyRingGenerator pkgen = PGPEC.keyRingGenerator();

    final PGPSecretKeyRing targetSecRing = PGPEC.createSecretKeyRing("g".toCharArray());
    final PGPPrivateKey priv = PGPEC.extractSignKey(targetSecRing, "g".toCharArray());
    final PGPPublicKeyRing pkr = PGPPublicKeyRing.insertPublicKey(pkgen.generatePublicKeyRing(),
            PGPEC.signPK(keyPair.getPublicKey(), priv));

    final byte[] pkis = pkr.getEncoded();

    final List<PGPPublicKey> loadRing = PGPEC.readPKring(new ByteArrayInputStream(pkis));
    assertThat(loadRing).hasSize(1);//from w w w  .java  2s .  co  m
    assertThat(Utils.toStream(loadRing.get(0).getKeySignatures())
            .filter(sig -> ((PGPSignature) sig).getKeyID() == priv.getKeyID()).findFirst()).isPresent();
}