Example usage for org.bouncycastle.openpgp.bc BcPGPPublicKeyRing BcPGPPublicKeyRing

List of usage examples for org.bouncycastle.openpgp.bc BcPGPPublicKeyRing BcPGPPublicKeyRing

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.bc BcPGPPublicKeyRing BcPGPPublicKeyRing.

Prototype

public BcPGPPublicKeyRing(InputStream in) throws IOException 

Source Link

Usage

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

License:Open Source License

/** Deserialize a PGPPublicKey */
public static PGPPublicKey deserializePublicKey(byte[] serialized) throws IOException {
    return new BcPGPPublicKeyRing(PGPUtil.getDecoderStream(new ByteArrayInputStream(serialized)))
            .getPublicKey();// w  w w . j a v  a 2 s  .c  om
}

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

License:Open Source License

/** Returns raw key bytes as a Bouncy Castle PGP public key. */
public static PGPPublicKey loadPublicKeyBytes(byte[] data) {
    try {/*w  ww.ja v  a  2 s. c  o m*/
        return lookupPublicSubkey(new BcPGPPublicKeyRing(data), KeyRequirement.ENCRYPT).get();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:google.registry.keyring.kms.KmsKeyring.java

License:Open Source License

private PGPKeyPair getKeyPair(String publicKeyName, String privateKeyName) {
    try {//www.  j  av a 2s  . c o m
        PGPPublicKey publicKey = new BcPGPPublicKeyRing(getPgpInputStream(publicKeyName)).getPublicKey();
        return new PGPKeyPair(publicKey, getPrivateKey(privateKeyName));
    } catch (IOException e) {
        throw new KeyringException(String.format("Could not parse public key %s and private key %s",
                publicKeyName, privateKeyName), e);
    }
}

From source file:google.registry.keyring.kms.KmsKeyring.java

License:Open Source License

private PGPPublicKey getPublicKeyForEncrypting(String publicKeyName) {
    try {//  w  w  w .ja  va  2s .c o  m
        return PgpHelper.lookupPublicSubkey(new BcPGPPublicKeyRing(getPgpInputStream(publicKeyName)), ENCRYPT)
                .get();
    } catch (IOException e) {
        throw new KeyringException(String.format("Could not parse public key %s", publicKeyName), e);
    }
}

From source file:google.registry.keyring.kms.KmsTestHelper.java

License:Open Source License

static BcPGPPublicKeyRing getPublicKeyring() throws Exception {
    return new BcPGPPublicKeyRing(PGPUtil.getDecoderStream(PGP_PUBLIC_KEYRING.openStream()));
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testSignVerify_Detached() throws Exception {
    // Load the keys.
    PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
    PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();
    PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());

    // Sign the data and write signature data to "signatureFile".
    // Note: RSA_GENERAL will encrypt AND sign. RSA_SIGN and RSA_ENCRYPT are deprecated.
    PGPSignatureGenerator signer = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
    signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
    addUserInfoToSignature(publicKey, signer);
    signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    signer.generate().encode(output);/*w  w w  .  ja v  a 2s  .c o m*/
    byte[] signatureFileData = output.toByteArray();
    logger.info(".sig file data: " + dumpHex(signatureFileData));

    // Load algorithm information and signature data from "signatureFileData".
    PGPSignature sig;
    try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
        assertThat(sigList.size()).isEqualTo(1);
        sig = sigList.get(0);
    }

    // Use "onePass" and "sig" to verify "publicKey" signed the text.
    sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    sig.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    assertThat(sig.verify()).isTrue();

    // Verify that they DIDN'T sign the text "hello monster".
    sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    sig.update("hello monster".getBytes(UTF_8));
    assertThat(sig.verify()).isFalse();
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testSignVerify_OnePass() throws Exception {
    // Load the keys.
    PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
    PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();
    PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());

    // Sign the data and write signature data to "signatureFile".
    PGPSignatureGenerator signer = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
    signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
    addUserInfoToSignature(publicKey, signer);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    signer.generateOnePassVersion(false).encode(output);
    signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    signer.generate().encode(output);//from   ww  w  .j  a va 2  s.  c o  m
    byte[] signatureFileData = output.toByteArray();
    logger.info(".sig file data: " + dumpHex(signatureFileData));

    // Load algorithm information and signature data from "signatureFileData".
    PGPSignature sig;
    PGPOnePassSignature onePass;
    try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPOnePassSignatureList onePassList = (PGPOnePassSignatureList) pgpFact.nextObject();
        PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
        assertThat(onePassList.size()).isEqualTo(1);
        assertThat(sigList.size()).isEqualTo(1);
        onePass = onePassList.get(0);
        sig = sigList.get(0);
    }

    // Use "onePass" and "sig" to verify "publicKey" signed the text.
    onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    onePass.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
    assertThat(onePass.verify(sig)).isTrue();

    // Verify that they DIDN'T sign the text "hello monster".
    onePass.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    onePass.update("hello monster".getBytes(UTF_8));
    assertThat(onePass.verify(sig)).isFalse();
}

From source file:google.registry.rde.BouncyCastleTest.java

License:Open Source License

@Test
public void testEncryptDecrypt_ExplicitStyle() throws Exception {
    int bufferSize = 64 * 1024;

    // Alice loads Bob's "publicKey" into memory.
    PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
    PGPPublicKey publicKey = publicKeyRing.getPublicKey();

    // Alice encrypts the secret message for Bob using his "publicKey".
    PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(AES_128));
    encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
    byte[] encryptedData;
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        try (OutputStream output2 = encryptor.open(output, new byte[bufferSize])) {
            output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
        }/*from   ww w. j  a v  a2  s  .  c  om*/
        encryptedData = output.toByteArray();
    }
    logger.info("Encrypted data: " + dumpHex(encryptedData));

    // Bob loads his "privateKey" into memory.
    PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
    PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());

    // Bob decrypt's the OpenPGP message (w/ ciphertext) using his "privateKey".
    try (ByteArrayInputStream input = new ByteArrayInputStream(encryptedData)) {
        PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
        PGPEncryptedDataList encDataList = (PGPEncryptedDataList) pgpFact.nextObject();
        assertThat(encDataList.size()).isEqualTo(1);
        PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) encDataList.get(0);
        assertThat(encData.getKeyID()).isEqualTo(publicKey.getKeyID());
        assertThat(encData.getKeyID()).isEqualTo(privateKey.getKeyID());
        try (InputStream original = encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
            assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8)))
                    .isEqualTo(FALL_OF_HYPERION_A_DREAM);
        }
    }
}

From source file:google.registry.tmch.TmchData.java

License:Open Source License

@SuppressWarnings("deprecation")
static PGPPublicKey loadPublicKey(ByteSource pgpPublicKeyFile) {
    try (InputStream input = pgpPublicKeyFile.openStream();
            InputStream decoder = PGPUtil.getDecoderStream(input)) {
        return new BcPGPPublicKeyRing(decoder).getPublicKey();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }//from   w  w  w  .java  2  s .c  o m
}

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

License:Open Source License

public static PGPPublicKey decodePK(final InputStream pk) {
    final PGPObjectFactory pgpFact = new PGPObjectFactory(pk, new JcaKeyFingerprintCalculator());

    try {/* w  w w. jav a2 s  .  co  m*/
        return new BcPGPPublicKeyRing(pk).getPublicKey();
    } catch (final IOException e) {
        Throwables.propagate(e);
    }

    return null;
}