1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-09-27 18:19:34 +02:00
pgpainless/pgpainless-core/src/test/java/org/pgpainless/symmetric_encryption/SymmetricEncryptionTest.java

118 lines
5.2 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2020-01-11 13:11:14 +01:00
package org.pgpainless.symmetric_encryption;
2020-11-13 16:31:59 +01:00
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2021-05-28 23:29:41 +02:00
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
2021-05-28 23:29:41 +02:00
import java.util.Random;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
2021-07-31 20:40:31 +02:00
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.pgpainless.util.TestAllImplementations;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.MissingKeyPassphraseStrategy;
2021-05-28 23:29:41 +02:00
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionStream;
2021-05-28 23:29:41 +02:00
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.exception.MissingDecryptionMethodException;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.KeyRingProtectionSettings;
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvider;
import org.pgpainless.util.Passphrase;
/**
* Test parallel symmetric and public key encryption/decryption.
*/
public class SymmetricEncryptionTest {
@TestTemplate
@ExtendWith(TestAllImplementations.class)
public void encryptWithKeyAndPassphrase_DecryptWithKey() throws IOException, PGPException {
byte[] plaintext = "This is a secret message".getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream plaintextIn = new ByteArrayInputStream(plaintext);
PGPPublicKeyRing encryptionKey = TestKeys.getCryptiePublicKeyRing();
Passphrase encryptionPassphrase = Passphrase.fromPassword("greenBeans");
2018-07-02 21:40:59 +02:00
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
.onOutputStream(ciphertextOut)
.withOptions(ProducerOptions.encrypt(
EncryptionOptions.encryptCommunications()
.addPassphrase(encryptionPassphrase)
.addRecipient(encryptionKey)
));
2021-07-31 20:40:31 +02:00
Streams.pipeAll(plaintextIn, encryptor);
encryptor.close();
byte[] ciphertext = ciphertextOut.toByteArray();
// Test symmetric decryption
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext))
.withOptions(new ConsumerOptions()
.addDecryptionPassphrase(encryptionPassphrase));
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
2021-07-31 20:40:31 +02:00
Streams.pipeAll(decryptor, decrypted);
decryptor.close();
assertArrayEquals(plaintext, decrypted.toByteArray());
// Test public key decryption
PGPSecretKeyRingCollection decryptionKeys = TestKeys.getCryptieSecretKeyRingCollection();
SecretKeyRingProtector protector = new PasswordBasedSecretKeyRingProtector(
KeyRingProtectionSettings.saltedAndIterated(),
new SolitaryPassphraseProvider(Passphrase.fromPassword(TestKeys.CRYPTIE_PASSWORD)));
decryptor = PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertext))
.withOptions(new ConsumerOptions()
.addDecryptionKeys(decryptionKeys, protector));
decrypted = new ByteArrayOutputStream();
2021-07-31 20:40:31 +02:00
Streams.pipeAll(decryptor, decrypted);
decryptor.close();
assertArrayEquals(plaintext, decrypted.toByteArray());
}
2021-05-28 23:29:41 +02:00
@TestTemplate
@ExtendWith(TestAllImplementations.class)
public void testMismatchPassphraseFails() throws IOException, PGPException {
2021-05-28 23:29:41 +02:00
byte[] bytes = new byte[5000];
new Random().nextBytes(bytes);
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
EncryptionStream encryptor = PGPainless.encryptAndOrSign().onOutputStream(ciphertextOut)
.withOptions(ProducerOptions.encrypt(
EncryptionOptions.encryptCommunications()
.addPassphrase(Passphrase.fromPassword("mellon"))));
2021-07-31 20:40:31 +02:00
Streams.pipeAll(new ByteArrayInputStream(bytes), encryptor);
2021-05-28 23:29:41 +02:00
encryptor.close();
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertextOut.toByteArray()))
.withOptions(new ConsumerOptions()
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
.addDecryptionPassphrase(Passphrase.fromPassword("meldir"))));
2021-05-28 23:29:41 +02:00
}
}