1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Port Encrypt example

This commit is contained in:
Paul Schaub 2025-04-07 16:27:39 +02:00
parent ff62a39dc8
commit 96fa3af08c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -127,14 +127,15 @@ public class Encrypt {
/** /**
* In this example, Alice is sending a signed and encrypted message to Bob. * In this example, Alice is sending a signed and encrypted message to Bob.
* She signs the message using her key and then encrypts the message to both bobs certificate and her own. * She signs the message using her key and then encrypts the message to both bobs certificate and her own.
* * <p>
* Bob subsequently decrypts the message using his key and verifies that the message was signed by Alice using * Bob subsequently decrypts the message using his key and verifies that the message was signed by Alice using
* her certificate. * her certificate.
*/ */
@Test @Test
public void encryptAndSignMessage() throws PGPException, IOException { public void encryptAndSignMessage() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
// Prepare keys // Prepare keys
OpenPGPKeyReader reader = PGPainless.getInstance().readKey(); OpenPGPKeyReader reader = api.readKey();
OpenPGPKey keyAlice = reader.parseKey(ALICE_KEY); OpenPGPKey keyAlice = reader.parseKey(ALICE_KEY);
OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT); OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT);
SecretKeyRingProtector protectorAlice = SecretKeyRingProtector.unprotectedKeys(); SecretKeyRingProtector protectorAlice = SecretKeyRingProtector.unprotectedKeys();
@ -147,14 +148,14 @@ public class Encrypt {
String message = "Hello, World!\n"; String message = "Hello, World!\n";
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream(); ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
// Encrypt and sign // Encrypt and sign
EncryptionStream encryptor = PGPainless.encryptAndOrSign() EncryptionStream encryptor = api.generateMessage()
.onOutputStream(ciphertext) .onOutputStream(ciphertext)
.withOptions(ProducerOptions.signAndEncrypt( .withOptions(ProducerOptions.signAndEncrypt(
// we want to encrypt communication (affects key selection based on key flags) // we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications() EncryptionOptions.encryptCommunications(api)
.addRecipient(certificateBob) .addRecipient(certificateBob)
.addRecipient(certificateAlice), .addRecipient(certificateAlice),
SigningOptions.get() SigningOptions.get(api)
.addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT) .addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
).setAsciiArmor(true) ).setAsciiArmor(true)
); );
@ -167,7 +168,7 @@ public class Encrypt {
// Decrypt and verify signatures // Decrypt and verify signatures
DecryptionStream decryptor = PGPainless.decryptAndOrVerify() DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8))) .onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8)))
.withOptions(ConsumerOptions.get() .withOptions(ConsumerOptions.get(api)
.addDecryptionKey(keyBob, protectorBob) .addDecryptionKey(keyBob, protectorBob)
.addVerificationCert(certificateAlice) .addVerificationCert(certificateAlice)
); );
@ -190,13 +191,14 @@ public class Encrypt {
*/ */
@Test @Test
public void encryptUsingPassphrase() throws PGPException, IOException { public void encryptUsingPassphrase() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
String message = "Hello, World!"; String message = "Hello, World!";
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream(); ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
// Encrypt // Encrypt
EncryptionStream encryptor = PGPainless.encryptAndOrSign() EncryptionStream encryptor = api.generateMessage()
.onOutputStream(ciphertext) .onOutputStream(ciphertext)
.withOptions(ProducerOptions .withOptions(ProducerOptions
.encrypt(EncryptionOptions.encryptCommunications() .encrypt(EncryptionOptions.encryptCommunications(api)
.addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3")) .addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3"))
).setAsciiArmor(true) ).setAsciiArmor(true)
); );
@ -209,7 +211,7 @@ public class Encrypt {
// Decrypt // Decrypt
DecryptionStream decryptor = PGPainless.decryptAndOrVerify() DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(asciiCiphertext.getBytes(StandardCharsets.UTF_8))) .onInputStream(new ByteArrayInputStream(asciiCiphertext.getBytes(StandardCharsets.UTF_8)))
.withOptions(ConsumerOptions.get().addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3"))); .withOptions(ConsumerOptions.get(api).addMessagePassphrase(Passphrase.fromPassword("p4ssphr4s3")));
ByteArrayOutputStream plaintext = new ByteArrayOutputStream(); ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, plaintext); Streams.pipeAll(decryptor, plaintext);
@ -223,13 +225,14 @@ public class Encrypt {
* In this example, Alice is sending a signed and encrypted message to Bob. * In this example, Alice is sending a signed and encrypted message to Bob.
* She encrypts the message to both bobs certificate and her own. * She encrypts the message to both bobs certificate and her own.
* A multiline comment header is added using the fluent ProducerOption syntax. * A multiline comment header is added using the fluent ProducerOption syntax.
* * <p>
* Bob subsequently decrypts the message using his key. * Bob subsequently decrypts the message using his key.
*/ */
@Test @Test
public void encryptWithCommentHeader() throws PGPException, IOException { public void encryptWithCommentHeader() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
// Prepare keys // Prepare keys
OpenPGPKeyReader reader = PGPainless.getInstance().readKey(); OpenPGPKeyReader reader = api.readKey();
OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT); OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT);
OpenPGPKey keyBob = reader.parseKey(BOB_KEY); OpenPGPKey keyBob = reader.parseKey(BOB_KEY);
@ -247,11 +250,11 @@ public class Encrypt {
String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3]; String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3];
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream(); ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
// Encrypt and sign // Encrypt and sign
EncryptionStream encryptor = PGPainless.encryptAndOrSign() EncryptionStream encryptor = api.generateMessage()
.onOutputStream(ciphertext) .onOutputStream(ciphertext)
.withOptions(ProducerOptions.encrypt( .withOptions(ProducerOptions.encrypt(
// we want to encrypt communication (affects key selection based on key flags) // we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications() EncryptionOptions.encryptCommunications(api)
.addRecipient(certificateBob) .addRecipient(certificateBob)
.addRecipient(certificateAlice) .addRecipient(certificateAlice)
).setAsciiArmor(true) ).setAsciiArmor(true)
@ -273,7 +276,7 @@ public class Encrypt {
// Decrypt and verify signatures // Decrypt and verify signatures
DecryptionStream decryptor = PGPainless.decryptAndOrVerify() DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8))) .onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes(StandardCharsets.UTF_8)))
.withOptions(ConsumerOptions.get() .withOptions(ConsumerOptions.get(api)
.addDecryptionKey(keyBob, protectorBob) .addDecryptionKey(keyBob, protectorBob)
.addVerificationCert(certificateAlice) .addVerificationCert(certificateAlice)
); );