mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 04:42:06 +01:00
commit
ea6f89e2bc
4 changed files with 43 additions and 28 deletions
|
@ -193,6 +193,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public <O> WithAlgorithms andToSelf(@Nonnull PublicKeyRingSelectionStrategy<O> ringSelectionStrategy,
|
public <O> WithAlgorithms andToSelf(@Nonnull PublicKeyRingSelectionStrategy<O> ringSelectionStrategy,
|
||||||
@Nonnull MultiMap<O, PGPPublicKeyRingCollection> keys) {
|
@Nonnull MultiMap<O, PGPPublicKeyRingCollection> keys) {
|
||||||
if (keys.isEmpty()) {
|
if (keys.isEmpty()) {
|
||||||
|
|
|
@ -102,43 +102,52 @@ public class SymmetricEncryptorDecryptor {
|
||||||
*/
|
*/
|
||||||
public static byte[] symmetricallyDecrypt(@Nonnull byte[] data, @Nonnull Passphrase password)
|
public static byte[] symmetricallyDecrypt(@Nonnull byte[] data, @Nonnull Passphrase password)
|
||||||
throws IOException, PGPException {
|
throws IOException, PGPException {
|
||||||
InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));
|
PGPPBEEncryptedData pbe;
|
||||||
in = PGPUtil.getDecoderStream(in);
|
ByteArrayOutputStream outputStream = null;
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data));
|
||||||
|
InputStream in = PGPUtil.getDecoderStream(bis);
|
||||||
|
|
||||||
BcPGPObjectFactory pgpF = new BcPGPObjectFactory(in);
|
try {
|
||||||
PGPEncryptedDataList enc;
|
BcPGPObjectFactory pgpF = new BcPGPObjectFactory(in);
|
||||||
Object o = pgpF.nextObject();
|
PGPEncryptedDataList enc;
|
||||||
|
Object o = pgpF.nextObject();
|
||||||
|
|
||||||
if (o instanceof PGPEncryptedDataList) {
|
if (o instanceof PGPEncryptedDataList) {
|
||||||
enc = (PGPEncryptedDataList) o;
|
enc = (PGPEncryptedDataList) o;
|
||||||
} else {
|
} else {
|
||||||
enc = (PGPEncryptedDataList) pgpF.nextObject();
|
enc = (PGPEncryptedDataList) pgpF.nextObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
|
pbe = (PGPPBEEncryptedData) enc.get(0);
|
||||||
|
|
||||||
InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(
|
InputStream clear = pbe.getDataStream(
|
||||||
password.getChars(), new BcPGPDigestCalculatorProvider()));
|
new BcPBEDataDecryptorFactory(password.getChars(), new BcPGPDigestCalculatorProvider()));
|
||||||
|
|
||||||
|
BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear);
|
||||||
|
|
||||||
BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear);
|
|
||||||
|
|
||||||
o = pgpFact.nextObject();
|
|
||||||
if (o instanceof PGPCompressedData) {
|
|
||||||
PGPCompressedData cData = (PGPCompressedData) o;
|
|
||||||
pgpFact = new BcPGPObjectFactory(cData.getDataStream());
|
|
||||||
o = pgpFact.nextObject();
|
o = pgpFact.nextObject();
|
||||||
|
if (o instanceof PGPCompressedData) {
|
||||||
|
PGPCompressedData cData = (PGPCompressedData) o;
|
||||||
|
pgpFact = new BcPGPObjectFactory(cData.getDataStream());
|
||||||
|
o = pgpFact.nextObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
PGPLiteralData ld = (PGPLiteralData) o;
|
||||||
|
InputStream unc = ld.getInputStream();
|
||||||
|
|
||||||
|
try {
|
||||||
|
outputStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
Streams.pipeAll(unc, outputStream);
|
||||||
|
} finally {
|
||||||
|
if (outputStream != null) {
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
PGPLiteralData ld = (PGPLiteralData) o;
|
|
||||||
InputStream unc = ld.getInputStream();
|
|
||||||
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
Streams.pipeAll(unc, outputStream);
|
|
||||||
|
|
||||||
outputStream.close();
|
|
||||||
|
|
||||||
if (pbe.isIntegrityProtected()) {
|
if (pbe.isIntegrityProtected()) {
|
||||||
if (!pbe.verify()) {
|
if (!pbe.verify()) {
|
||||||
throw new PGPException("Integrity check failed.");
|
throw new PGPException("Integrity check failed.");
|
||||||
|
|
|
@ -106,6 +106,7 @@ public class BCUtilTest extends AbstractPGPainlessTest {
|
||||||
@Test
|
@Test
|
||||||
public void removeUnsignedKeysTest()
|
public void removeUnsignedKeysTest()
|
||||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
|
PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
|
||||||
PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
|
PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,9 @@ public class LengthTest extends AbstractPGPainlessTest {
|
||||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||||
IOException {
|
IOException {
|
||||||
LOGGER.log(Level.INFO, "\nRSA-2048 -> RSA-2048");
|
LOGGER.log(Level.INFO, "\nRSA-2048 -> RSA-2048");
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||||
}
|
}
|
||||||
|
@ -78,6 +80,7 @@ public class LengthTest extends AbstractPGPainlessTest {
|
||||||
public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
|
public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
|
||||||
NoSuchProviderException {
|
NoSuchProviderException {
|
||||||
LOGGER.log(Level.INFO, "\nRSA-2048 -> EC");
|
LOGGER.log(Level.INFO, "\nRSA-2048 -> EC");
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048);
|
||||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv");
|
||||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||||
|
@ -89,6 +92,7 @@ public class LengthTest extends AbstractPGPainlessTest {
|
||||||
IOException {
|
IOException {
|
||||||
LOGGER.log(Level.INFO, "\nEC -> RSA-2048");
|
LOGGER.log(Level.INFO, "\nEC -> RSA-2048");
|
||||||
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld");
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048);
|
||||||
encryptDecryptForSecretKeyRings(sender, recipient);
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue