/** * Create a new key ring generator. * * @param certificationLevel * @param masterKey * @param id * @param checksumCalculator * @param hashedPcks * @param unhashedPcks * @param keySignerBuilder * @param keyEncryptor * @throws PGPException */ public PGPKeyRingGenerator( int certificationLevel, PGPKeyPair masterKey, String id, PGPDigestCalculator checksumCalculator, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder keySignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException { this.masterKey = masterKey; this.keyEncryptor = keyEncryptor; this.checksumCalculator = checksumCalculator; this.keySignerBuilder = keySignerBuilder; this.hashedPcks = hashedPcks; this.unhashedPcks = unhashedPcks; keys.add(new PGPSecretKey(certificationLevel, masterKey, id, checksumCalculator, hashedPcks, unhashedPcks, keySignerBuilder, keyEncryptor)); }
/** * @deprecated use method taking PGPKeyPair */ public PGPSecretKey( int certificationLevel, int algorithm, PublicKey pubKey, PrivateKey privKey, Date time, String id, PGPDigestCalculator checksumCalculator, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException { this(certificationLevel, new PGPKeyPair(algorithm, pubKey, privKey, time), id, checksumCalculator, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); }
/** * @deprecated use method taking PGPKeyPair */ public PGPSecretKey( int certificationLevel, int algorithm, PublicKey pubKey, PrivateKey privKey, Date time, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException, NoSuchProviderException { this(certificationLevel, new PGPKeyPair(algorithm, pubKey, privKey, time), id, null, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); }
/** * Builds a PGPSignatureGenerator for the specified key and content. */ protected PGPSignatureGenerator buildSigner(Key key, FileMetadata meta) throws PGPException { Subkey subkey = key.getSigning(); if (log.isLoggable(Level.INFO)) log.info("using signing key " + subkey); PGPContentSignerBuilder builder = buildSignerBuilder( subkey.getPublicKey().getAlgorithm(), signingAlgorithm.ordinal() ); PGPSignatureGenerator generator = new PGPSignatureGenerator(builder); generator.init(meta.getSignatureType(), subkey.getPrivateKey()); String uid = key.getSigningUid(); if (!Util.isEmpty(uid)) { if (log.isLoggable(Level.FINE)) log.fine("using signing uid " + uid); PGPSignatureSubpacketGenerator signer = new PGPSignatureSubpacketGenerator(); signer.setSignerUserID(false, uid); generator.setHashedSubpackets(signer.generate()); } return generator; }
private static PGPPublicKeyRing addUserId( final PGPPublicKeyRing publicKeyRing, final String userId, final PGPSecretKeyRing secretKeyRing, final char[] passphrase, PGPContentSignerBuilder signerBuilder, final PGPSignatureSubpacketVector hashedSubpackets, final PGPSignatureSubpacketVector unhashedSubpackets) throws PGPException { assertNotNull(publicKeyRing, "publicKeyRing"); assertNotNull(userId, "userId"); final PGPPublicKey masterPublicKey = getMasterKeyOrFail(publicKeyRing); final PGPSecretKey masterSecretKey = secretKeyRing.getSecretKey(masterPublicKey.getKeyID()); assertNotNull(masterSecretKey, "masterSecretKey"); final PGPPrivateKey privateKey = extractPrivateKey(masterSecretKey, passphrase); final PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder); sGen.init(PGPSignature.POSITIVE_CERTIFICATION, privateKey); sGen.setHashedSubpackets(hashedSubpackets); sGen.setUnhashedSubpackets(unhashedSubpackets); final PGPSignature certification = sGen.generateCertification(userId, masterPublicKey); final PGPPublicKey newMasterPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, certification); PGPPublicKeyRing result = PGPPublicKeyRing.removePublicKey(publicKeyRing, masterPublicKey); result = PGPPublicKeyRing.insertPublicKey(result, newMasterPublicKey); return result; }
public PGPSecretKey( int certificationLevel, PGPKeyPair keyPair, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException { this(certificationLevel, keyPair, id, null, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); }
public PGPSecretKey( int certificationLevel, PGPKeyPair keyPair, String id, PGPDigestCalculator checksumCalculator, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException { this(keyPair.getPrivateKey(), certifiedPublicKey(certificationLevel, keyPair, id, hashedPcks, unhashedPcks, certificationSignerBuilder), checksumCalculator, true, keyEncryptor); }
/** * Builds a PGPContentSignerBuilder for the specified algorithms. */ protected PGPContentSignerBuilder buildSignerBuilder( int keyAlgorithm, int hashAlgorithm) { return new BcPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm); }
/** * Create a signature generator built on the passed in contentSignerBuilder. * * @param contentSignerBuilder builder to produce PGPContentSigner objects for generating signatures. */ public PGPV3SignatureGenerator( PGPContentSignerBuilder contentSignerBuilder) { this.contentSignerBuilder = contentSignerBuilder; }
/** * Create a signature generator built on the passed in contentSignerBuilder. * * @param contentSignerBuilder builder to produce PGPContentSigner objects for generating signatures. */ public PGPSignatureGenerator( PGPContentSignerBuilder contentSignerBuilder) { this.contentSignerBuilder = contentSignerBuilder; }
public String encrypt(String message, boolean sign) throws PGPEncryptionException, PGPKeyNotFoundException { try { PGPSecretKey secretKey = ourKey.getSecretKey(); PGPPrivateKey privateKey = ourKey.getPrivateKey(); byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor); encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey.getEncryptionKey())); PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB); PGPContentSignerBuilder signerBuilder = new BcPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256 ); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(signerBuilder); signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, (String) secretKey.getPublicKey().getUserIDs().next()); signatureGenerator.setHashedSubpackets(spGen.generate()); if (sign) { signatureGenerator.update(messageBytes); } ByteArrayOutputStream bOut = new ByteArrayOutputStream(); Hashtable<String, String> headers = new Hashtable<>(); headers.put("Version", "QPG"); ArmoredOutputStream armoredOut = new ArmoredOutputStream(bOut, headers); OutputStream encryptedOut = encryptedDataGenerator.open(armoredOut, new byte[BUFFER_SIZE]); OutputStream compressedOut = compressedDataGenerator.open(encryptedOut); if (sign) { signatureGenerator.generateOnePassVersion(false).encode(compressedOut); } OutputStream literalOut = literalDataGenerator.open( compressedOut, PGPLiteralData.UTF8, PGPLiteralData.CONSOLE, messageBytes.length, new Date() ); literalOut.write(messageBytes); literalDataGenerator.close(); if (sign) { signatureGenerator.generate().encode(compressedOut); } compressedDataGenerator.close(); encryptedDataGenerator.close(); armoredOut.close(); return new String(bOut.toByteArray(), StandardCharsets.UTF_8); } catch (PGPException | RuntimeException | IOException e) { throw new PGPEncryptionException("Error encrypting message", e); } }
/** * Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id * using a generated certification of certificationLevel.The secret key checksum is calculated using the original * non-digest based checksum. * * @param certificationLevel the type of certification to be added. * @param keyPair the public/private keys to use. * @param id the id to bind to the key. * @param hashedPcks the hashed packets to be added to the certification. * @param unhashedPcks the unhashed packets to be added to the certification. * @param certificationSignerBuilder the builder for generating the certification. * @param keyEncryptor an encryptor for the key if required (null otherwise). * @throws PGPException if there is an issue creating the secret key packet or the certification. */ public PGPSecretKey( int certificationLevel, PGPKeyPair keyPair, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException { this(certificationLevel, keyPair, id, null, hashedPcks, unhashedPcks, certificationSignerBuilder, keyEncryptor); }