Java Code Examples for org.bouncycastle.openpgp.PGPEncryptedData#SAFER

The following examples show how to use org.bouncycastle.openpgp.PGPEncryptedData#SAFER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenPGPKeyBasedEncryptorTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldDecryptExternalFile() throws Exception {
    for (int i = 1; i<14; i++) {
        if (PGPEncryptedData.SAFER != i) { // SAFER cipher is not supported and therefore its test is skipped
            Integer cipher = i;
            // Arrange
            byte[] plainBytes = Files.readAllBytes(Paths.get(plainFile.getPath()));
            final String PLAINTEXT = new String(plainBytes, "UTF-8");

            InputStream cipherStream = new FileInputStream(unsignedFile);
            OutputStream recoveredStream = new ByteArrayOutputStream();

            // No file, just streams
            String filename = unsignedFile.getName();

            OpenPGPKeyBasedEncryptor encryptor = new OpenPGPKeyBasedEncryptor(
                    EncryptionMethod.PGP.getAlgorithm(), cipher, EncryptionMethod.PGP.getProvider(), SECRET_KEYRING_PATH, USER_ID, PASSWORD.toCharArray(), filename);

            StreamCallback decryptionCallback = encryptor.getDecryptionCallback();

            // Act
            decryptionCallback.process(cipherStream, recoveredStream);

            // Assert
            byte[] recoveredBytes = ((ByteArrayOutputStream) recoveredStream).toByteArray();
            String recovered = new String(recoveredBytes, "UTF-8");
            logger.info("Recovered: {}", recovered);
            Assert.assertEquals("Recovered text", PLAINTEXT, recovered);
        }
    }
}
 
Example 2
Source File: OpenPGPPasswordBasedEncryptorTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldDecryptExternalFile() throws Exception {
    for (int i = 1; i<14; i++) {
        if (PGPEncryptedData.SAFER != i) {  // SAFER cipher is not supported and therefore its test is skipped
            Integer cipher = i;
            // Arrange
            byte[] plainBytes = Files.readAllBytes(Paths.get(plainFile.getPath()));
            final String PLAINTEXT = new String(plainBytes, "UTF-8");

            InputStream cipherStream = new FileInputStream(encryptedFile);
            OutputStream recoveredStream = new ByteArrayOutputStream();

            // No file, just streams
            String filename = encryptedFile.getName();

            OpenPGPPasswordBasedEncryptor encryptor = new OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(), cipher,
                    EncryptionMethod.PGP.getProvider(), LEGACY_PASSWORD.toCharArray(), filename);

            StreamCallback decryptionCallback = encryptor.getDecryptionCallback();

            // Act
            decryptionCallback.process(cipherStream, recoveredStream);

            // Assert
            byte[] recoveredBytes = ((ByteArrayOutputStream) recoveredStream).toByteArray();
            String recovered = new String(recoveredBytes, "UTF-8");
            logger.info("Recovered: {}", recovered);
            Assert.assertEquals("Recovered text", PLAINTEXT, recovered);
        }
    }
}