@Test(expected = AcmEncryptionBadKeyOrDataException.class) public void testPGPDecryptionInvalidPassPhrase() throws IOException, AcmEncryptionBadKeyOrDataException { Resource encryptedFile = new ClassPathResource("encrypted.bin"); assertTrue(encryptedFile.exists()); String passPhrase = "text1"; cryptoUtils.decryptInputStreamWithPGP(encryptedFile.getInputStream(), passPhrase.toCharArray()); }
@Override public byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase) throws AcmEncryptionBadKeyOrDataException { try { in = PGPUtil.getDecoderStream(in); JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); InputStream clear = pbe.getDataStream( new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) .setProvider("BC").build(passPhrase)); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear); PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); return Streams.readAll(ld.getInputStream()); } catch (IOException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } catch (PGPException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase) throws AcmEncryptionBadKeyOrDataException { try { in = PGPUtil.getDecoderStream(in); JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); InputStream clear = pbe.getDataStream( new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) .setProvider("BC").build(passPhrase)); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear); PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); return Streams.readAll(ld.getInputStream()); } catch (IOException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } catch (PGPException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } } }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase) throws AcmEncryptionBadKeyOrDataException { try { in = PGPUtil.getDecoderStream(in); JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); InputStream clear = pbe.getDataStream( new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) .setProvider("BC").build(passPhrase)); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear); PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); return Streams.readAll(ld.getInputStream()); } catch (IOException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } catch (PGPException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } } AcmCryptoUtilsImpl(); }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase) throws AcmEncryptionBadKeyOrDataException { try { in = PGPUtil.getDecoderStream(in); JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); InputStream clear = pbe.getDataStream( new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) .setProvider("BC").build(passPhrase)); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear); PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); return Streams.readAll(ld.getInputStream()); } catch (IOException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } catch (PGPException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } } AcmCryptoUtilsImpl(); @Override byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce); @Override byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce); @Override byte[] decryptData(PrivateKey key, byte[] data, boolean hasNonce, String encryptionAlgorithm); @Override byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase); @Override byte[] encryptWithPGP(byte[] clearData, char[] passPhrase, String fileName, int algorithm, boolean armor); @Override byte[] decryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); @Override byte[] encryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); }
AcmCryptoUtilsImpl implements AcmCryptoUtils { @Override public byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase) throws AcmEncryptionBadKeyOrDataException { try { in = PGPUtil.getDecoderStream(in); JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); InputStream clear = pbe.getDataStream( new JcePBEDataDecryptorFactoryBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()) .setProvider("BC").build(passPhrase)); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear); PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject(); pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject(); return Streams.readAll(ld.getInputStream()); } catch (IOException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } catch (PGPException e) { throw new AcmEncryptionBadKeyOrDataException("Bad key or data", e); } } AcmCryptoUtilsImpl(); @Override byte[] encryptData(byte[] passPhrase, byte[] data, boolean addNonce); @Override byte[] decryptData(byte[] passPhrase, byte[] data, boolean hasNonce); @Override byte[] decryptData(PrivateKey key, byte[] data, boolean hasNonce, String encryptionAlgorithm); @Override byte[] decryptInputStreamWithPGP(InputStream in, char[] passPhrase); @Override byte[] encryptWithPGP(byte[] clearData, char[] passPhrase, String fileName, int algorithm, boolean armor); @Override byte[] decryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding); @Override byte[] encryptData(byte[] passPhrase, byte[] data, int keySize, int ivSize, int magicSize, int saltSize,
            int passPhraseIterations, String passPhraseHashAlgorithm, String encryptionAlgorithm, String blockCipherMode, String padding);  }