org.bouncycastle.openpgp.PGPUtil Java Examples
The following examples show how to use
org.bouncycastle.openpgp.PGPUtil.
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: KeyFilesOperationsPgpImpl.java From pgptool with GNU General Public License v3.0 | 7 votes |
@SuppressWarnings("rawtypes") private static void readKeyFromStream(KeyDataPgp data, InputStream stream) throws IOException { PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream), fingerprintCalculator); for (Iterator iter = factory.iterator(); iter.hasNext();) { Object section = iter.next(); log.debug("Section found: " + section); if (section instanceof PGPSecretKeyRing) { data.setSecretKeyRing((PGPSecretKeyRing) section); } else if (section instanceof PGPPublicKeyRing) { data.setPublicKeyRing((PGPPublicKeyRing) section); } else { log.error("Unknown section enountered in a key file: " + section); } } }
Example #2
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
private PGPSecretKey readSecretKey() throws IOException { try { PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream(new ByteArrayInputStream(config.keypair.getBytes(Charsets.UTF_8))), new JcaKeyFingerprintCalculator()); Iterator<PGPSecretKeyRing> keyRings = pgpSec.getKeyRings(); while (keyRings.hasNext()) { PGPSecretKeyRing keyRing = keyRings.next(); Iterator<PGPSecretKey> keys = keyRing.getSecretKeys(); while (keys.hasNext()) { PGPSecretKey key = keys.next(); if (key.isSigningKey()) { return key; } } } } catch (PGPException ex) { throw new RuntimeException(ex); } throw new IllegalStateException("Can't find signing key in key ring."); }
Example #3
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 6 votes |
private PGPSecretKey readSecretKey() throws IOException, PGPException { PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream(new ByteArrayInputStream(config.keypair.getBytes())), new JcaKeyFingerprintCalculator()); Iterator<PGPSecretKeyRing> keyRings = pgpSec.getKeyRings(); while (keyRings.hasNext()) { PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRings.next(); Iterator<PGPSecretKey> keys = keyRing.getSecretKeys(); while (keys.hasNext()) { PGPSecretKey key = (PGPSecretKey) keys.next(); if (key.isSigningKey()) { return key; } } } throw new IllegalStateException("Can't find signing key in key ring."); }
Example #4
Source File: AptITSupport.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
public boolean verifyReleaseFilePgpSignature(final InputStream signedData, final InputStream signature, final InputStream publicKey) throws Exception { PGPObjectFactory pgpFact = new PGPObjectFactory(PGPUtil.getDecoderStream(signature), new JcaKeyFingerprintCalculator()); PGPSignature sig = ((PGPSignatureList) pgpFact.nextObject()).get(0); PGPPublicKeyRingCollection pgpPubRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicKey), new JcaKeyFingerprintCalculator()); PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID()); sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); byte[] buff = new byte[1024]; int read = 0; while ((read = signedData.read(buff)) != -1) { sig.update(buff, 0, read); } signedData.close(); return sig.verify(); }
Example #5
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
public byte[] signExternal(final String input) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.update(input.getBytes(Charsets.UTF_8)); sigGenerator.generate().encode(bOut); } } catch (PGPException ex) { throw new RuntimeException(ex); } return buffer.toByteArray(); }
Example #6
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 6 votes |
@SuppressWarnings( "unchecked" ) private static Iterator<PGPPublicKeyEncryptedData> getEncryptedObjects( final byte[] message ) throws IOException { try { final PGPObjectFactory factory = new PGPObjectFactory( PGPUtil.getDecoderStream( new ByteArrayInputStream( message ) ), new JcaKeyFingerprintCalculator() ); final Object first = factory.nextObject(); final Object list = ( first instanceof PGPEncryptedDataList ) ? first : factory.nextObject(); return ( ( PGPEncryptedDataList ) list ).getEncryptedDataObjects(); } catch ( IOException e ) { throw new IOException( e ); } }
Example #7
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 6 votes |
private static PGPLiteralData asLiteral( final byte[] message, final InputStream secretKeyRing, final String secretPwd ) throws IOException, PGPException { PGPPrivateKey key = null; PGPPublicKeyEncryptedData encrypted = null; final PGPSecretKeyRingCollection keys = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secretKeyRing ), new JcaKeyFingerprintCalculator() ); for ( final Iterator<PGPPublicKeyEncryptedData> i = getEncryptedObjects( message ); ( key == null ) && i.hasNext(); ) { encrypted = i.next(); key = getPrivateKey( keys, encrypted.getKeyID(), secretPwd ); } if ( key == null ) { throw new IllegalArgumentException( "secret key for message not found." ); } final InputStream stream = encrypted .getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( provider ).build( key ) ); return asLiteral( stream ); }
Example #8
Source File: BcGpgDoer.java From jeka with Apache License 2.0 | 6 votes |
private static List<PGPSecretKeyRing> extractSecrectKeyRings(InputStream inputStream) { InputStream decodedInput; try { decodedInput = PGPUtil.getDecoderStream(inputStream); } catch (final IOException e) { throw JkUtilsThrowable.unchecked(e); } final KeyFingerPrintCalculator fingerPrintCalculator = new JcaKeyFingerprintCalculator(); final InnerPGPObjectFactory pgpFact = new InnerPGPObjectFactory(decodedInput, fingerPrintCalculator); PGPSecretKeyRing secKeyRing; final List<PGPSecretKeyRing> result = new LinkedList<>(); while ((secKeyRing = pgpFact.nextSecretKey()) != null) { result.add(secKeyRing); } return result; }
Example #9
Source File: PgpHelper.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
public static PGPSecretKey loadSecretKey ( final InputStream input, final String keyId ) throws IOException, PGPException { final long keyIdNum = Long.parseUnsignedLong ( keyId, 16 ); final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection ( PGPUtil.getDecoderStream ( input ) ); final Iterator<?> keyRingIter = keyrings.getKeyRings (); while ( keyRingIter.hasNext () ) { final PGPSecretKeyRing secretKeyRing = (PGPSecretKeyRing)keyRingIter.next (); final Iterator<?> secretKeyIterator = secretKeyRing.getSecretKeys (); while ( secretKeyIterator.hasNext () ) { final PGPSecretKey key = (PGPSecretKey)secretKeyIterator.next (); if ( !key.isSigningKey () ) { continue; } final long shortId = key.getKeyID () & 0xFFFFFFFFL; if ( key.getKeyID () != keyIdNum && shortId != keyIdNum ) { continue; } return key; } } return null; }
Example #10
Source File: BcGpgDoer.java From jeka with Apache License 2.0 | 5 votes |
static boolean verify(InputStream streamToVerify, InputStream signatureStream, InputStream keyInputStream) throws IOException, PGPException { final InputStream sigInputStream = PGPUtil.getDecoderStream(new BufferedInputStream( signatureStream)); final KeyFingerPrintCalculator fingerPrintCalculator = new JcaKeyFingerprintCalculator(); final PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, fingerPrintCalculator); final PGPSignatureList signatureList; final Object gpgObject = pgpObjectFactory.nextObject(); if (gpgObject == null) { throw new IllegalArgumentException("no PGP signature found in " + sigInputStream); } if (gpgObject instanceof PGPCompressedData) { final PGPCompressedData compressedData = (PGPCompressedData) gpgObject; final PGPObjectFactory compressedPgpObjectFactory = new PGPObjectFactory( compressedData.getDataStream(), fingerPrintCalculator); signatureList = (PGPSignatureList) compressedPgpObjectFactory.nextObject(); } else { signatureList = (PGPSignatureList) gpgObject; } final PGPPublicKeyRingCollection pgpPubRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(keyInputStream), fingerPrintCalculator); final InputStream bufferedStream = new BufferedInputStream(streamToVerify); final PGPSignature signature = signatureList.get(0); final PGPPublicKey publicKey = pgpPubRingCollection.getPublicKey(signature.getKeyID()); final PGPContentVerifierBuilderProvider builderProvider = new BcPGPContentVerifierBuilderProvider(); signature.init(builderProvider, publicKey); int character; while ((character = bufferedStream.read()) >= 0) { signature.update((byte) character); } return signature.verify(); }
Example #11
Source File: PGPUtils.java From desktopclient-java with GNU General Public License v3.0 | 5 votes |
/** * Read a public key from ASCII armored key ring data. */ public static Optional<PGPCoderKey> readPublicKey(String armoredInput) { try { return readPublicKey(IOUtils.toByteArray( PGPUtil.getDecoderStream(IOUtils.toInputStream(armoredInput, "UTF-8")))); } catch (IOException ex) { LOGGER.log(Level.WARNING, "can't read armored input", ex); return Optional.empty(); } }
Example #12
Source File: OpenPGPSignatureGenerator.java From ant-ivy with Apache License 2.0 | 5 votes |
private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException { in = PGPUtil.getDecoderStream(in); PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in, new BcKeyFingerprintCalculator()); PGPSecretKey key = null; Iterator<PGPSecretKeyRing> it = pgpSec.getKeyRings(); while (key == null && it.hasNext()) { PGPSecretKeyRing kRing = it.next(); Iterator<PGPSecretKey> it2 = kRing.getSecretKeys(); while (key == null && it2.hasNext()) { PGPSecretKey k = it2.next(); if (keyId == null && k.isSigningKey()) { key = k; } if (keyId != null && Long.valueOf(keyId, 16) == (k.getKeyID() & MASK)) { key = k; } } } if (key == null) { throw new IllegalArgumentException("Can't find encryption key" + (keyId != null ? " '" + keyId + "' " : " ") + "in key ring."); } return key; }
Example #13
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public byte[] signInline(final String input) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey); Iterator<String> userIds = signKey.getUserIDs(); if (userIds.hasNext()) { PGPSignatureSubpacketGenerator sigSubpacketGenerator = new PGPSignatureSubpacketGenerator(); sigSubpacketGenerator.setSignerUserID(false, userIds.next()); sigGenerator.setHashedSubpackets(sigSubpacketGenerator.generate()); } String[] lines = input.split("\r?\n"); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { aOut.beginClearText(PGPUtil.SHA256); boolean firstLine = true; for (String line : lines) { String sigLine = (firstLine ? "" : "\r\n") + line.replaceAll("\\s*$", ""); sigGenerator.update(sigLine.getBytes(Charsets.UTF_8)); aOut.write((line + "\n").getBytes(Charsets.UTF_8)); firstLine = false; } aOut.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.generate().encode(bOut); } } catch (PGPException ex) { throw new RuntimeException(ex); } return buffer.toByteArray(); }
Example #14
Source File: GPGFileEncryptor.java From incubator-gobblin with Apache License 2.0 | 5 votes |
/** * Taking in an input {@link OutputStream}, keyring inputstream and a passPhrase, generate an encrypted {@link OutputStream}. * @param outputStream {@link OutputStream} that will receive the encrypted content * @param keyIn keyring inputstream. This InputStream is owned by the caller. * @param keyId key identifier * @param cipher the symmetric cipher to use for encryption. If null or empty then a default cipher is used. * @return an {@link OutputStream} to write content to for encryption * @throws IOException */ public OutputStream encryptFile(OutputStream outputStream, InputStream keyIn, long keyId, String cipher) throws IOException { try { if (Security.getProvider(PROVIDER_NAME) == null) { Security.addProvider(new BouncyCastleProvider()); } PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator( new JcePGPDataEncryptorBuilder(symmetricKeyAlgorithmNameToTag(cipher)) .setSecureRandom(new SecureRandom()) .setProvider(PROVIDER_NAME)); PGPPublicKey publicKey; PGPPublicKeyRingCollection keyRings = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn), new BcKeyFingerprintCalculator()); publicKey = keyRings.getPublicKey(keyId); if (publicKey == null) { throw new IllegalArgumentException("public key for encryption not found"); } cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider(PROVIDER_NAME)); OutputStream cOut = cPk.open(outputStream, new byte[BUFFER_SIZE]); PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator(); OutputStream _literalOut = literalGen.open(cOut, PGPLiteralDataGenerator.BINARY, PAYLOAD_NAME, new Date(), new byte[BUFFER_SIZE]); return new ClosingWrapperOutputStream(_literalOut, cOut, outputStream); } catch (PGPException e) { throw new IOException(e); } }
Example #15
Source File: KeyFilesOperationsPgpImpl.java From pgptool with GNU General Public License v3.0 | 5 votes |
@SuppressWarnings("rawtypes") private static String resolveAlgorithm(PGPPublicKey key) throws PGPException { for (Iterator iter = key.getSignatures(); iter.hasNext();) { PGPSignature sig = (PGPSignature) iter.next(); return PGPUtil.getSignatureName(sig.getKeyAlgorithm(), sig.getHashAlgorithm()); } return null; }
Example #16
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 5 votes |
public byte[] signExternal(String input) throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.update(input.getBytes(Charsets.UTF_8)); sigGenerator.generate().encode(bOut); } return buffer.toByteArray(); }
Example #17
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 5 votes |
public byte[] signInline(String input) throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey); @SuppressWarnings("unchecked") Iterator<String> userIds = signKey.getUserIDs(); if (userIds.hasNext()) { PGPSignatureSubpacketGenerator sigSubpacketGenerator = new PGPSignatureSubpacketGenerator(); sigSubpacketGenerator.setSignerUserID(false, userIds.next()); sigGenerator.setHashedSubpackets(sigSubpacketGenerator.generate()); } String[] lines = input.split("\r?\n"); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { aOut.beginClearText(PGPUtil.SHA256); boolean firstLine = true; for (String line : lines) { String sigLine = (firstLine ? "" : "\r\n") + line.replaceAll("\\s*$", ""); sigGenerator.update(sigLine.getBytes(Charsets.UTF_8)); aOut.write((line + "\n").getBytes(Charsets.UTF_8)); firstLine = false; } aOut.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.generate().encode(bOut); } return buffer.toByteArray(); }
Example #18
Source File: KeySerializerTest.java From nomulus with Apache License 2.0 | 5 votes |
private static BcPGPSecretKeyRing getSecretKeyring() { try { return new BcPGPSecretKeyRing( PGPUtil.getDecoderStream(new ByteArrayInputStream(ARMORED_KEY_STRING.getBytes(UTF_8)))); } catch (IOException | PGPException e) { throw new Error(e); } }
Example #19
Source File: DummyKeyringModule.java From nomulus with Apache License 2.0 | 5 votes |
/** Always returns a {@link InMemoryKeyring} instance. */ @Provides @Named("DummyKeyring") static InMemoryKeyring provideDummyKeyring() { PGPKeyPair dummyKey; try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream(); InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) { PGPPublicKeyRingCollection publicKeys = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput)); PGPSecretKeyRingCollection privateKeys = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput)); dummyKey = lookupKeyPair(publicKeys, privateKeys, EMAIL_ADDRESS, ENCRYPT_SIGN); } catch (PGPException | IOException e) { throw new VerifyException("Failed to load PGP keys from jar", e); } // Use the same dummy PGP keypair for all required PGP keys -- a real production system would // have different values for these keys. Pass dummy values for all Strings. return new InMemoryKeyring( dummyKey, dummyKey, dummyKey.getPublicKey(), dummyKey, dummyKey.getPublicKey(), "not a real key", "not a real key", "not a real password", "not a real API key", "not a real login", "not a real password", "not a real login", "not a real credential", "not a real password", "not a real password"); }
Example #20
Source File: KeySerializer.java From nomulus with Apache License 2.0 | 5 votes |
/** Deserialize a PGPKeyPair */ public static PGPKeyPair deserializeKeyPair(byte[] serialized) throws IOException, PGPException { PGPSecretKey secretKey = new BcPGPSecretKeyRing( PGPUtil.getDecoderStream( new ByteArrayInputStream(serialized))).getSecretKey(); return new PGPKeyPair( secretKey.getPublicKey(), secretKey.extractPrivateKey(createSecretKeyDecryptor())); }
Example #21
Source File: KeySerializer.java From nomulus with Apache License 2.0 | 5 votes |
/** Deserialize a PGPPublicKey */ public static PGPPublicKey deserializePublicKey(byte[] serialized) throws IOException { return new BcPGPPublicKeyRing( PGPUtil.getDecoderStream( new ByteArrayInputStream(serialized))).getPublicKey(); }
Example #22
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 #23
Source File: EncryptionServicePgpImpl.java From pgptool with GNU General Public License v3.0 | 5 votes |
@SuppressWarnings("rawtypes") private PGPPublicKeyEncryptedData getPublicKeyEncryptedDataByKeyId(InputStream in, PGPSecretKey secretKey) { try { PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(in), KeyFilesOperationsPgpImpl.fingerprintCalculator); for (Iterator iter = factory.iterator(); iter.hasNext();) { Object section = iter.next(); if (section instanceof PGPEncryptedDataList) { PGPEncryptedDataList d = (PGPEncryptedDataList) section; for (Iterator dataIter = d.getEncryptedDataObjects(); dataIter.hasNext();) { PGPPublicKeyEncryptedData data = (PGPPublicKeyEncryptedData) dataIter.next(); if (data.getKeyID() == secretKey.getKeyID()) { return data; } } } } // NOTE: That is actually should NEVER happen since secret key we're // supposed to use here was taken exactly same way as we're looking // for PGPPublicKeyEncryptedData now throw new RuntimeException("Encryption data matching given key " + KeyDataPgp.buildKeyIdStr(secretKey.getKeyID()) + " wasn't found"); } catch (Throwable t) { throw new RuntimeException("Failed to find Encryption data section in encrypted file", t); } }
Example #24
Source File: PGPSign.java From peer-os with Apache License 2.0 | 5 votes |
private static PGPSignatureGenerator getSignatureGenerator( PGPPrivateKey privateKey, BCPGOutputStream bcOut ) throws PGPException, IOException { PGPSignatureGenerator signGen = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( privateKey.getPublicKeyPacket().getAlgorithm(), PGPUtil.SHA1 ) .setProvider( "BC" ) ); signGen.init( PGPSignature.BINARY_DOCUMENT, privateKey ); signGen.generateOnePassVersion( false ).encode( bcOut ); return signGen; }
Example #25
Source File: PGPKeyHelper.java From peer-os with Apache License 2.0 | 5 votes |
public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException { PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() ); Iterator keyRingIter = pgpPub.getKeyRings(); while ( keyRingIter.hasNext() ) { PGPPublicKeyRing keyRing = ( PGPPublicKeyRing ) keyRingIter.next(); Iterator keyIter = keyRing.getPublicKeys(); while ( keyIter.hasNext() ) { PGPPublicKey key = ( PGPPublicKey ) keyIter.next(); if ( key.isEncryptionKey() ) { return key; } } } throw new IllegalArgumentException( "Can't find encryption key in key ring." ); }
Example #26
Source File: PGPKeyHelper.java From peer-os with Apache License 2.0 | 5 votes |
private static PGPSecretKey readSecretKey( InputStream is ) throws IOException, PGPException { PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() ); Iterator keyRingIter = pgpSec.getKeyRings(); while ( keyRingIter.hasNext() ) { PGPSecretKeyRing keyRing = ( PGPSecretKeyRing ) keyRingIter.next(); Iterator keyIter = keyRing.getSecretKeys(); while ( keyIter.hasNext() ) { PGPSecretKey key = ( PGPSecretKey ) keyIter.next(); if ( key.isSigningKey() ) { return key; } } } throw new IllegalArgumentException( "Can't find signing key in key ring." ); }
Example #27
Source File: TmchData.java From nomulus with Apache License 2.0 | 5 votes |
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); } }
Example #28
Source File: PGPEncryptionUtilTest.java From peer-os with Apache License 2.0 | 5 votes |
@Test public void testKeySigning() throws PGPException, IOException { KeyPair first = PGPEncryptionUtil.generateKeyPair( "[email protected]", "first", false ); KeyPair second = PGPEncryptionUtil.generateKeyPair( "[email protected]", "second", false ); signKeyAndPrintIds( first, second, "second" ); InputStream firstPublicStream = new ByteArrayInputStream( first.getPubKeyring() ); InputStream secondPublicStream = new ByteArrayInputStream( second.getPubKeyring() ); PGPPublicKeyRingCollection firstPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( firstPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRingCollection secondPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ), new JcaKeyFingerprintCalculator() ); if ( firstPublicKeyRingCollection.getKeyRings().hasNext() ) { PGPPublicKeyRing firstPublicKeyRing = null; PGPPublicKeyRing secondPublicKeyRing = null; firstPublicKeyRing = firstPublicKeyRingCollection.getKeyRings().next(); secondPublicKeyRing = secondPublicKeyRingCollection.getKeyRings().next(); assertEquals( true, printPublicKeySignatures( firstPublicKeyRing.getPublicKey(), secondPublicKeyRing.getPublicKey() ) ); } }
Example #29
Source File: PGPEncryptionUtilTest.java From peer-os with Apache License 2.0 | 5 votes |
private void signKeyAndPrintIds( KeyPair first, KeyPair second, String password ) throws IOException, PGPException { InputStream firstPublicStream = new ByteArrayInputStream( first.getPubKeyring() ); InputStream secondPublicStream = new ByteArrayInputStream( second.getPubKeyring() ); InputStream secondSecretStream = new ByteArrayInputStream( second.getSecKeyring() ); PGPPublicKeyRingCollection keyrings = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( firstPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRing firstPublicKeyRing = null; if ( keyrings.getKeyRings().hasNext() ) { firstPublicKeyRing = keyrings.getKeyRings().next(); PGPSecretKey secondSecretKey = PGPEncryptionUtil.findSecretKeyById( secondSecretStream, second.getPrimaryKeyId() ); PGPPublicKey secondPublicKey = PGPEncryptionUtil.findPublicKeyById( secondPublicStream, second.getPrimaryKeyId() ); if ( secondSecretKey != null ) { String keyId = Long.toHexString( secondSecretKey.getKeyID() ); PGPPublicKeyRing firstSignedPublicKeyRing = PGPEncryptionUtil.signPublicKey( firstPublicKeyRing, keyId, secondSecretKey, password ); printPublicKeySignatures( firstSignedPublicKeyRing.getPublicKey(), secondPublicKey ); first.setPubKeyring( firstSignedPublicKeyRing.getEncoded() ); } } }
Example #30
Source File: PGPEncryptionUtilTest.java From peer-os with Apache License 2.0 | 5 votes |
@Test public void testVerifyClearSign() throws Exception { InputStream secondPublicStream = findFile( PLUGIN_PUBLIC_KEY ); PGPPublicKeyRingCollection secondPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRing pgpKeyring = secondPublicKeyRingCollection .getPublicKeyRing( secondPublicKeyRingCollection.iterator().next().getPublicKey().getKeyID() ); String signedMessage = IOUtils.toString( findFile( "signedMessage.txt" ) ); logger.info( "\n" + signedMessage ); boolean result = PGPEncryptionUtil.verifyClearSign( signedMessage.getBytes(), pgpKeyring ); if ( result ) { logger.info( "signature verified." ); } else { logger.info( "signature verification failed." ); } assertEquals( true, result ); }