Use passphrase for symmetric encryption

This commit is contained in:
Paul Schaub 2018-07-12 23:16:30 +02:00
parent 71f196afe8
commit d46671e37e
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 12 additions and 8 deletions

View File

@ -25,6 +25,7 @@ import org.pgpainless.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.pgpainless.key.parsing.KeyRingReader;
import org.pgpainless.pgpainless.key.generation.KeyRingBuilder;
import org.pgpainless.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor;
import org.pgpainless.pgpainless.util.Passphrase;
import java.io.IOException;
@ -72,7 +73,7 @@ public class PGPainless {
* @throws IOException IO is dangerous.
* @throws PGPException PGP is brittle.
*/
public static byte[] encryptWithPassword(byte[] data, char[] password, SymmetricKeyAlgorithm algorithm) throws IOException, PGPException {
public static byte[] encryptWithPassword(byte[] data, Passphrase password, SymmetricKeyAlgorithm algorithm) throws IOException, PGPException {
return SymmetricEncryptorDecryptor.symmetricallyEncrypt(data, password,
algorithm, CompressionAlgorithm.UNCOMPRESSED);
}
@ -87,7 +88,7 @@ public class PGPainless {
* @throws IOException IO is dangerous.
* @throws PGPException PGP is brittle.
*/
public static byte[] decryptWithPassword(byte[] data, char[] password) throws IOException, PGPException {
public static byte[] decryptWithPassword(byte[] data, Passphrase password) throws IOException, PGPException {
return SymmetricEncryptorDecryptor.symmetricallyDecrypt(data, password);
}
}

View File

@ -41,6 +41,7 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.pgpainless.util.Passphrase;
/**
* Stolen from <a href="https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/PBEFileProcessor.java">
@ -62,7 +63,7 @@ public class SymmetricEncryptorDecryptor {
* @throws PGPException OpenPGP is brittle
*/
public static byte[] symmetricallyEncrypt(byte[] data,
char[] password,
Passphrase password,
SymmetricKeyAlgorithm encryptionAlgorithm,
CompressionAlgorithm compressionAlgorithm)
throws IOException, PGPException {
@ -77,7 +78,7 @@ public class SymmetricEncryptorDecryptor {
.setSecureRandom(new SecureRandom())
.setProvider("BC"));
encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(password).setProvider("BC"));
encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(password.getChars()).setProvider("BC"));
OutputStream encOut = encGen.open(bOut, compressedData.length);
@ -98,7 +99,7 @@ public class SymmetricEncryptorDecryptor {
* @throws IOException IO is dangerous
* @throws PGPException OpenPGP is brittle
*/
public static byte[] symmetricallyDecrypt(byte[] data, char[] password) throws IOException, PGPException {
public static byte[] symmetricallyDecrypt(byte[] data, Passphrase password) throws IOException, PGPException {
InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));
in = PGPUtil.getDecoderStream(in);
@ -115,7 +116,7 @@ public class SymmetricEncryptorDecryptor {
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(
password, new BcPGPDigestCalculatorProvider()));
password.getChars(), new BcPGPDigestCalculatorProvider()));
BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear);

View File

@ -27,6 +27,7 @@ import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException;
import org.junit.Test;
import org.pgpainless.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.pgpainless.util.Passphrase;
public class SymmetricTest extends AbstractPGPainlessTest {
@ -45,7 +46,8 @@ public class SymmetricTest extends AbstractPGPainlessTest {
@Test
public void testSymmetricEncryptionDecryption() throws IOException, PGPException {
byte[] plain = message.getBytes();
byte[] enc = PGPainless.encryptWithPassword(plain, "choose_a_better_password_please".toCharArray(), SymmetricKeyAlgorithm.AES_128);
Passphrase passphrase = new Passphrase("choose_a_better_password_please".toCharArray());
byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(out);
armor.write(enc);
@ -55,7 +57,7 @@ public class SymmetricTest extends AbstractPGPainlessTest {
// Print cipher text for validation with GnuPG.
LOGGER.log(Level.INFO, new String(out.toByteArray()));
byte[] plain2 = PGPainless.decryptWithPassword(enc, "choose_a_better_password_please".toCharArray());
byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase);
assertTrue(Arrays.equals(plain, plain2));
}
}