mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-14 20:59:39 +02:00
Allow encryption and decryption using symmetric passphrases in the main API
This commit is contained in:
parent
ce4f98423f
commit
7d374f10a7
10 changed files with 303 additions and 84 deletions
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright 2018 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.symmetric_encryption;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
public class LegacySymmetricEncryptionTest {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(LegacySymmetricEncryptionTest.class.getName());
|
||||
|
||||
private static final String message =
|
||||
"I grew up with the understanding that the world " +
|
||||
"I lived in was one where people enjoyed a sort of freedom " +
|
||||
"to communicate with each other in privacy, without it " +
|
||||
"being monitored, without it being measured or analyzed " +
|
||||
"or sort of judged by these shadowy figures or systems, " +
|
||||
"any time they mention anything that travels across " +
|
||||
"public lines.\n" +
|
||||
"\n" +
|
||||
"- Edward Snowden -";
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testSymmetricEncryptionDecryption() throws IOException, PGPException {
|
||||
byte[] plain = message.getBytes();
|
||||
String password = "choose_a_better_password_please";
|
||||
Passphrase passphrase = new Passphrase(password.toCharArray());
|
||||
byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armor = new ArmoredOutputStream(out);
|
||||
armor.write(enc);
|
||||
armor.flush();
|
||||
armor.close();
|
||||
|
||||
// Print cipher text for validation with GnuPG.
|
||||
LOGGER.log(Level.INFO, String.format("Use ciphertext below for manual validation with GnuPG " +
|
||||
"(passphrase = '%s').\n\n%s", password, new String(out.toByteArray())));
|
||||
|
||||
byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase);
|
||||
assertArrayEquals(plain, plain2);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2018 Paul Schaub.
|
||||
* Copyright 2020 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -17,50 +17,82 @@ package org.pgpainless.symmetric_encryption;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||
import org.pgpainless.encryption_signing.EncryptionStream;
|
||||
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 {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(SymmetricEncryptionTest.class.getName());
|
||||
|
||||
private static final String message =
|
||||
"I grew up with the understanding that the world " +
|
||||
"I lived in was one where people enjoyed a sort of freedom " +
|
||||
"to communicate with each other in privacy, without it " +
|
||||
"being monitored, without it being measured or analyzed " +
|
||||
"or sort of judged by these shadowy figures or systems, " +
|
||||
"any time they mention anything that travels across " +
|
||||
"public lines.\n" +
|
||||
"\n" +
|
||||
"- Edward Snowden -";
|
||||
|
||||
@Test
|
||||
public void testSymmetricEncryptionDecryption() throws IOException, PGPException {
|
||||
byte[] plain = message.getBytes();
|
||||
String password = "choose_a_better_password_please";
|
||||
Passphrase passphrase = new Passphrase(password.toCharArray());
|
||||
byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armor = new ArmoredOutputStream(out);
|
||||
armor.write(enc);
|
||||
armor.flush();
|
||||
armor.close();
|
||||
public void test() 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");
|
||||
|
||||
// Print cipher text for validation with GnuPG.
|
||||
LOGGER.log(Level.INFO, String.format("Use ciphertext below for manual validation with GnuPG " +
|
||||
"(passphrase = '%s').\n\n%s", password, new String(out.toByteArray())));
|
||||
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptor = PGPainless.encryptAndOrSign().onOutputStream(ciphertextOut)
|
||||
.forPassphrases(encryptionPassphrase)
|
||||
.and()
|
||||
.toRecipients(encryptionKey)
|
||||
.usingSecureAlgorithms()
|
||||
.doNotSign()
|
||||
.noArmor();
|
||||
|
||||
byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase);
|
||||
assertArrayEquals(plain, plain2);
|
||||
Streams.pipeAll(plaintextIn, encryptor);
|
||||
encryptor.close();
|
||||
|
||||
byte[] ciphertext = ciphertextOut.toByteArray();
|
||||
|
||||
// Test symmetric decryption
|
||||
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(new ByteArrayInputStream(ciphertext))
|
||||
.decryptWith(encryptionPassphrase)
|
||||
.doNotVerify()
|
||||
.build();
|
||||
|
||||
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
|
||||
|
||||
Streams.pipeAll(decryptor, decrypted);
|
||||
decryptor.close();
|
||||
|
||||
assertArrayEquals(plaintext, decrypted.toByteArray());
|
||||
|
||||
// Test public key decryption
|
||||
PGPSecretKeyRingCollection decryptionKeys = TestKeys.getCryptieSecretKeyRingCollection();
|
||||
SecretKeyRingProtector protector = new PasswordBasedSecretKeyRingProtector(
|
||||
KeyRingProtectionSettings.secureDefaultSettings(),
|
||||
new SolitaryPassphraseProvider(Passphrase.fromPassword(TestKeys.CRYPTIE_PASSWORD)));
|
||||
decryptor = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(new ByteArrayInputStream(ciphertext))
|
||||
.decryptWith(protector, decryptionKeys)
|
||||
.doNotVerify()
|
||||
.build();
|
||||
|
||||
decrypted = new ByteArrayOutputStream();
|
||||
|
||||
Streams.pipeAll(decryptor, decrypted);
|
||||
decryptor.close();
|
||||
|
||||
assertArrayEquals(plaintext, decrypted.toByteArray());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue