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