mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 12:52:07 +01:00
Use passphrase for symmetric encryption
This commit is contained in:
parent
71f196afe8
commit
d46671e37e
3 changed files with 12 additions and 8 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue