Java Code Examples for org.bouncycastle.openpgp.PGPPublicKeyRing#getPublicKey()
The following examples show how to use
org.bouncycastle.openpgp.PGPPublicKeyRing#getPublicKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 6 votes |
public static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom, String id ) throws PGPException { try { PGPPublicKey oldKey = keyToRemoveFrom.getPublicKey(); PGPPublicKey newKey = PGPPublicKey.removeCertification( oldKey, id ); PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( keyToRemoveFrom, oldKey ); return PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, newKey ); } catch ( Exception e ) { //throw custom exception throw new PGPException( "Error removing signature", e ); } }
Example 2
Source File: SecurityManagerRestImpl.java From peer-os with Apache License 2.0 | 6 votes |
@Override public Response getPublicKeyId( final String identityId ) { try { PGPPublicKeyRing pubRing = securityManager.getKeyManager().getPublicKeyRing( identityId ); PGPPublicKey key = pubRing.getPublicKey(); if ( key == null ) { logger.info( " ************* Public Key not found with id:" + identityId ); return Response.status( Response.Status.NOT_FOUND ).entity( "Object Not found" ).build(); } else { return Response.ok( PGPKeyUtil.encodeNumericKeyId( key.getKeyID() ) ).build(); } } catch ( Exception ex ) { logger.info( " ************* Error ! Public Key not found with id:" + identityId, ex ); return Response.status( Response.Status.NOT_FOUND ).entity( "Object Not found" ).build(); } }
Example 3
Source File: KeyFilesOperationsPgpImpl.java From pgptool with GNU General Public License v3.0 | 5 votes |
private KeyInfo buildKeyInfoFromPublic(PGPPublicKeyRing publicKeyRing) throws PGPException { KeyInfo ret = new KeyInfo(); ret.setKeyType(KeyTypeEnum.Public); PGPPublicKey key = publicKeyRing.getPublicKey(); ret.setUser(buildUser(key.getUserIDs())); ret.setKeyId(KeyDataPgp.buildKeyIdStr(key.getKeyID())); fillDates(ret, key); fillAlgorithmName(ret, key); return ret; }
Example 4
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 5 votes |
/** * Signs a public key * * @param publicKeyRing a public key ring containing the single public key to sign * @param id the id we are certifying against the public key * @param secretKey the signing key * @param secretKeyPassword the signing key password * * @return a public key ring with the signed public key */ public static PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey, String secretKeyPassword ) throws PGPException { try { PGPPublicKey oldKey = publicKeyRing.getPublicKey(); PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider( provider ) .build( secretKeyPassword.toCharArray() ) ); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) ); signatureGenerator.init( PGPSignature.DEFAULT_CERTIFICATION, pgpPrivKey ); PGPSignature signature = signatureGenerator.generateCertification( id, oldKey ); PGPPublicKey newKey = PGPPublicKey.addCertification( oldKey, signature ); PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( publicKeyRing, oldKey ); return PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, newKey ); } catch ( Exception e ) { //throw custom exception throw new PGPException( "Error signing public key", e ); } }
Example 5
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 5 votes |
public static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom, PGPPublicKey keySignatureToRemove ) throws PGPException { try { PGPPublicKey oldKey = keyToRemoveFrom.getPublicKey(); PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( keyToRemoveFrom, oldKey ); Iterator<PGPSignature> signIterator = oldKey.getSignatures(); while ( signIterator.hasNext() ) { PGPSignature signature = signIterator.next(); signature.init( new JcaPGPContentVerifierBuilderProvider().setProvider( provider ), keySignatureToRemove ); String sigId = PGPKeyUtil.encodeNumericKeyId( oldKey.getKeyID() ); if ( signature.verifyCertification( sigId, oldKey ) ) { PGPPublicKey updatedKey = PGPPublicKey.removeCertification( oldKey, signature ); keyToRemoveFrom = PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, updatedKey ); } } return keyToRemoveFrom; } catch ( Exception e ) { //throw custom exception throw new PGPException( "Error removing signature", e ); } }
Example 6
Source File: KeyManagerImpl.java From peer-os with Apache License 2.0 | 5 votes |
@Override public boolean verifySignature( PGPPublicKeyRing sourcePubRing, PGPPublicKeyRing targetPubRing ) { PGPPublicKey keyToVerifyWith = sourcePubRing.getPublicKey(); PGPPublicKey keyToVerify = targetPubRing.getPublicKey(); String sigId = PGPKeyUtil.encodeNumericKeyId( keyToVerify.getKeyID() ); return encryptionTool.verifyPublicKey( keyToVerify, sigId, keyToVerifyWith ); }
Example 7
Source File: KeyManagerImpl.java From peer-os with Apache License 2.0 | 5 votes |
@Override public PGPPublicKeyRing removeSignature( String sourceFingerprint, String targetFingerprint ) { PGPPublicKeyRing targetPubRing = getPublicKeyRingByFingerprint( targetFingerprint ); PGPPublicKeyRing sourcePubRing = getPublicKeyRingByFingerprint( sourceFingerprint ); PGPPublicKey sourcePublicKey = sourcePubRing.getPublicKey(); return removeSignature( sourcePublicKey, targetPubRing ); }
Example 8
Source File: BouncyCastleTest.java From nomulus with Apache License 2.0 | 5 votes |
@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); byte[] signatureFileData = output.toByteArray(); logger.atInfo().log(".sig file data: %s", 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(); }
Example 9
Source File: BouncyCastleTest.java From nomulus with Apache License 2.0 | 5 votes |
@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)); } encryptedData = output.toByteArray(); } logger.atInfo().log("Encrypted data: %s", 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); } } }
Example 10
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 4 votes |
public static boolean verifyClearSign( byte[] message, PGPPublicKeyRing pgpRings ) throws IOException, PGPException, SignatureException { ArmoredInputStream aIn = new ArmoredInputStream( new ByteArrayInputStream( message ) ); ByteArrayOutputStream bout = new ByteArrayOutputStream(); // // write out signed section using the local line separator. // note: trailing white space needs to be removed from the end of // each line RFC 4880 Section 7.1 // ByteArrayOutputStream lineOut = new ByteArrayOutputStream(); boolean isFirstLineClearText = aIn.isClearText(); int lookAhead = readInputLine( lineOut, aIn ); if ( lookAhead != -1 && isFirstLineClearText ) { bout.write( lineOut.toByteArray() ); while ( lookAhead != -1 && aIn.isClearText() ) { lookAhead = readInputLine( lineOut, lookAhead, aIn ); bout.write( lineOut.toByteArray() ); } } JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory( aIn ); PGPSignatureList p3 = ( PGPSignatureList ) pgpFact.nextObject(); PGPSignature sig = p3.get( 0 ); PGPPublicKey publicKey = pgpRings.getPublicKey( sig.getKeyID() ); sig.init( new JcaPGPContentVerifierBuilderProvider().setProvider( "BC" ), publicKey ); // // read the input, making sure we ignore the last newline. // InputStream sigIn = new ByteArrayInputStream( bout.toByteArray() ); lookAhead = readInputLine( lineOut, sigIn ); processLine( sig, lineOut.toByteArray() ); if ( lookAhead != -1 ) { do { lookAhead = readInputLine( lineOut, lookAhead, sigIn ); sig.update( ( byte ) '\r' ); sig.update( ( byte ) '\n' ); processLine( sig, lineOut.toByteArray() ); } while ( lookAhead != -1 ); } sigIn.close(); return sig.verify(); }
Example 11
Source File: PGPKeyId.java From pgpverify-maven-plugin with Apache License 2.0 | 4 votes |
@Override public PGPPublicKey getKeyFromRing(PGPPublicKeyRing publicKeyRing) { return publicKeyRing.getPublicKey(keyId); }
Example 12
Source File: PGPKeyId.java From pgpverify-maven-plugin with Apache License 2.0 | 4 votes |
@Override public PGPPublicKey getKeyFromRing(PGPPublicKeyRing publicKeyRing) { return publicKeyRing.getPublicKey(fingerprint); }
Example 13
Source File: BouncyCastleTest.java From nomulus with Apache License 2.0 | 3 votes |
@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); byte[] signatureFileData = output.toByteArray(); logger.atInfo().log(".sig file data: %s", 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(); }
Example 14
Source File: BouncyCastleTest.java From nomulus with Apache License 2.0 | 3 votes |
@Test public void testEncryptDecrypt_KeyRingStyle() throws Exception { int bufferSize = 64 * 1024; // Alice loads Bob's "publicKey" into memory from her public key ring. PGPPublicKeyRingCollection publicKeyRings = new BcPGPPublicKeyRingCollection( PGPUtil.getDecoderStream(new ByteArrayInputStream(PUBLIC_KEY))); PGPPublicKeyRing publicKeyRing = publicKeyRings.getKeyRings("[email protected]", true, true).next(); 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)); } encryptedData = output.toByteArray(); } logger.atInfo().log("Encrypted data: %s", dumpHex(encryptedData)); // Bob loads his chain of private keys into memory. PGPSecretKeyRingCollection privateKeyRings = new BcPGPSecretKeyRingCollection( PGPUtil.getDecoderStream(new ByteArrayInputStream(PRIVATE_KEY))); // 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); // Bob loads the private key to which the message is addressed. PGPPrivateKey privateKey = extractPrivateKey(privateKeyRings.getSecretKey(encData.getKeyID())); try (InputStream original = encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) { assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8))) .isEqualTo(FALL_OF_HYPERION_A_DREAM); } } }