Example usage for org.bouncycastle.openpgp PGPPublicKey encode

List of usage examples for org.bouncycastle.openpgp PGPPublicKey encode

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPublicKey encode.

Prototype

public void encode(OutputStream outStream) throws IOException 

Source Link

Usage

From source file:com.ginema.crypto.encryption.PGPKeyPairGenerator.java

License:Apache License

private static void exportKeyPair(OutputStream secretOut, OutputStream publicOut, PublicKey publicKey,
        PrivateKey privateKey, String identity, char[] passPhrase, boolean armor)
        throws IOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException {
    if (armor) {//from w  w  w .j  ava2 s  . c o m
        secretOut = new ArmoredOutputStream(secretOut);
    }

    PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PGPPublicKey.RSA_GENERAL,
            publicKey, privateKey, new Date(), identity, PGPEncryptedData.CAST5, passPhrase, null, null,
            new SecureRandom(), "BC");

    secretKey.encode(secretOut);

    secretOut.close();

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

    PGPPublicKey key = secretKey.getPublicKey();

    key.encode(publicOut);

    publicOut.close();
}

From source file:com.google.gerrit.acceptance.api.accounts.AccountIT.java

License:Apache License

private static String armor(PGPPublicKey key) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    try (ArmoredOutputStream aout = new ArmoredOutputStream(out)) {
        key.encode(aout);
    }/*from w ww . j  a  va  2 s  .  c o m*/
    return new String(out.toByteArray(), UTF_8);
}

From source file:com.google.gerrit.gpg.server.GpgKeys.java

License:Apache License

static GpgKeyInfo toJson(PGPPublicKeyRing keyRing) throws IOException {
    PGPPublicKey key = keyRing.getPublicKey();
    GpgKeyInfo info = new GpgKeyInfo();
    info.id = PublicKeyStore.keyIdToString(key.getKeyID());
    info.fingerprint = Fingerprint.toString(key.getFingerprint());
    @SuppressWarnings("unchecked")
    Iterator<String> userIds = key.getUserIDs();
    info.userIds = ImmutableList.copyOf(userIds);
    try (ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
            ArmoredOutputStream aout = new ArmoredOutputStream(out)) {
        // This is not exactly the key stored in the store, but is equivalent. In
        // particular, it will have a Bouncy Castle version string. The armored
        // stream reader in PublicKeyStore doesn't give us an easy way to extract
        // the original ASCII armor.
        key.encode(aout);
        info.key = new String(out.toByteArray(), UTF_8);
    }/*from w  w  w  .j  ava 2s  .  c  om*/
    return info;
}

From source file:google.registry.keyring.api.KeySerializer.java

License:Open Source License

/**
 * Serialize a PGPPublicKey//from   w  w  w. jav a  2 s  .c  om
 *
 * <p>The reason we're not using {@link PGPPublicKey#getEncoded()} is to use {@link
 * ArmoredOutputStream}.
 */
public static byte[] serializePublicKey(PGPPublicKey publicKey) throws IOException {
    try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
        // NOTE: We have to close the ArmoredOutputStream before calling the underlying OutputStream's
        // "toByteArray". Failing to do so would result in a truncated serialization as we took the
        // byte array before the ArmoredOutputStream wrote all the data.
        //
        // Even "flushing" the ArmoredOutputStream isn't enough - as there are parts that are only
        // written by the ArmoredOutputStream when it is closed: the "-----END PGP PRIVATE KEY
        // BLOCK-----" (or similar) footer.
        try (ArmoredOutputStream out = new ArmoredOutputStream(byteStream)) {
            publicKey.encode(out);
        }
        return byteStream.toByteArray();
    }
}

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

License:Open Source License

/**
 * Return the passed publicKey in ASCII armored.
 * //from   www .  jav a 2  s  . co  m
 * @param pubKey
 * @return
 * @throws IOException 
 */
public static String getArmoredPublicKey(PGPPublicKey pubKey) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArmoredOutputStream aos = new ArmoredOutputStream(baos);

    pubKey.encode(aos);

    aos.close();
    baos.close();

    String armoredPubKey = baos.toString();
    return armoredPubKey;
}

From source file:net.staticsnow.nexus.repository.apt.internal.gpg.AptSigningFacet.java

License:Open Source License

public Content getPublicKey() throws IOException, PGPException {
    PGPSecretKey signKey = readSecretKey();
    PGPPublicKey publicKey = signKey.getPublicKey();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try (BCPGOutputStream os = new BCPGOutputStream(new ArmoredOutputStream(buffer))) {
        publicKey.encode(os);
    }//w w  w. ja  v a2s .  c om
    return new Content(new BytesPayload(buffer.toByteArray(), AptMimeTypes.PUBLICKEY));
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.internal.AbstractSecretKeySigningService.java

License:Open Source License

@Override
public void printPublicKey(final OutputStream out) throws IOException {
    final ArmoredOutputStream armoredOutput = new ArmoredOutputStream(out);
    armoredOutput.setHeader("Version", VersionInformation.VERSIONED_PRODUCT);

    final PGPPublicKey pubKey = this.secretKey.getPublicKey();
    pubKey.encode(new BCPGOutputStream(armoredOutput));
    armoredOutput.close();//from w  w  w. j ava2  s .c o  m
}

From source file:org.jivesoftware.smackx.ox.OpenPgpManager.java

License:Apache License

/**
 * Fetch a secret key backup from the server and try to restore a selected secret key from it.
 *
 * @param codeCallback callback for prompting the user to provide the secret backup code.
 * @return fingerprint of the restored secret key
 *
 * @throws InterruptedException if the thread gets interrupted.
 * @throws PubSubException.NotALeafNodeException if the private node is not a {@link LeafNode}.
 * @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
 * @throws SmackException.NotConnectedException if we are not connected.
 * @throws SmackException.NoResponseException if the server doesn't respond.
 * @throws InvalidBackupCodeException if the user-provided backup code is invalid.
 * @throws SmackException.NotLoggedInException if we are not logged in
 * @throws IOException IO is dangerous//from   w  ww .j a  va 2 s  .c  om
 * @throws MissingUserIdOnKeyException if the key that is to be imported is missing a user-id with our jid
 * @throws NoBackupFoundException if no secret key backup has been found
 * @throws PGPException in case the restored secret key is damaged.
 */
public OpenPgpV4Fingerprint restoreSecretKeyServerBackup(AskForBackupCodeCallback codeCallback)
        throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException,
        SmackException.NotConnectedException, SmackException.NoResponseException, InvalidBackupCodeException,
        SmackException.NotLoggedInException, IOException, MissingUserIdOnKeyException, NoBackupFoundException,
        PGPException {
    throwIfNoProviderSet();
    throwIfNotAuthenticated();
    SecretkeyElement backup = OpenPgpPubSubUtil.fetchSecretKey(pepManager);
    if (backup == null) {
        throw new NoBackupFoundException();
    }

    String backupCode = codeCallback.askForBackupCode();

    PGPSecretKeyRing secretKeys = SecretKeyBackupHelper.restoreSecretKeyBackup(backup, backupCode);
    provider.getStore().importSecretKey(getJidOrThrow(), secretKeys);
    provider.getStore().importPublicKey(getJidOrThrow(), BCUtil.publicKeyRingFromSecretKeyRing(secretKeys));

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(2048);
    for (PGPSecretKey sk : secretKeys) {
        PGPPublicKey pk = sk.getPublicKey();
        if (pk != null)
            pk.encode(buffer);
    }
    PGPPublicKeyRing publicKeys = new PGPPublicKeyRing(buffer.toByteArray(), new BcKeyFingerprintCalculator());
    provider.getStore().importPublicKey(getJidOrThrow(), publicKeys);

    return new OpenPgpV4Fingerprint(secretKeys);
}