1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 04:42:06 +01:00

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.parsing.KeyRingReader;
import org.pgpainless.pgpainless.key.generation.KeyRingBuilder; import org.pgpainless.pgpainless.key.generation.KeyRingBuilder;
import org.pgpainless.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor; import org.pgpainless.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor;
import org.pgpainless.pgpainless.util.Passphrase;
import java.io.IOException; import java.io.IOException;
@ -72,7 +73,7 @@ public class PGPainless {
* @throws IOException IO is dangerous. * @throws IOException IO is dangerous.
* @throws PGPException PGP is brittle. * @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, return SymmetricEncryptorDecryptor.symmetricallyEncrypt(data, password,
algorithm, CompressionAlgorithm.UNCOMPRESSED); algorithm, CompressionAlgorithm.UNCOMPRESSED);
} }
@ -87,7 +88,7 @@ public class PGPainless {
* @throws IOException IO is dangerous. * @throws IOException IO is dangerous.
* @throws PGPException PGP is brittle. * @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); 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.bouncycastle.util.io.Streams;
import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.pgpainless.algorithm.SymmetricKeyAlgorithm; 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"> * 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 * @throws PGPException OpenPGP is brittle
*/ */
public static byte[] symmetricallyEncrypt(byte[] data, public static byte[] symmetricallyEncrypt(byte[] data,
char[] password, Passphrase password,
SymmetricKeyAlgorithm encryptionAlgorithm, SymmetricKeyAlgorithm encryptionAlgorithm,
CompressionAlgorithm compressionAlgorithm) CompressionAlgorithm compressionAlgorithm)
throws IOException, PGPException { throws IOException, PGPException {
@ -77,7 +78,7 @@ public class SymmetricEncryptorDecryptor {
.setSecureRandom(new SecureRandom()) .setSecureRandom(new SecureRandom())
.setProvider("BC")); .setProvider("BC"));
encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(password).setProvider("BC")); encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(password.getChars()).setProvider("BC"));
OutputStream encOut = encGen.open(bOut, compressedData.length); OutputStream encOut = encGen.open(bOut, compressedData.length);
@ -98,7 +99,7 @@ public class SymmetricEncryptorDecryptor {
* @throws IOException IO is dangerous * @throws IOException IO is dangerous
* @throws PGPException OpenPGP is brittle * @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)); InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));
in = PGPUtil.getDecoderStream(in); in = PGPUtil.getDecoderStream(in);
@ -115,7 +116,7 @@ public class SymmetricEncryptorDecryptor {
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory( InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(
password, new BcPGPDigestCalculatorProvider())); password.getChars(), new BcPGPDigestCalculatorProvider()));
BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear); BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear);

View file

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