Search in sources :

Example 76 with CipherInputStream

use of javax.crypto.CipherInputStream in project tika by apache.

the class CryptoParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    try {
        Cipher cipher;
        if (provider != null) {
            cipher = Cipher.getInstance(transformation, provider);
        } else {
            cipher = Cipher.getInstance(transformation);
        }
        Key key = context.get(Key.class);
        if (key == null) {
            throw new EncryptedDocumentException("No decryption key provided");
        }
        AlgorithmParameters params = context.get(AlgorithmParameters.class);
        SecureRandom random = context.get(SecureRandom.class);
        if (params != null && random != null) {
            cipher.init(Cipher.DECRYPT_MODE, key, params, random);
        } else if (params != null) {
            cipher.init(Cipher.DECRYPT_MODE, key, params);
        } else if (random != null) {
            cipher.init(Cipher.DECRYPT_MODE, key, random);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key);
        }
        super.parse(new CipherInputStream(stream, cipher), handler, metadata, context);
    } catch (GeneralSecurityException e) {
        throw new TikaException("Unable to decrypt document stream", e);
    }
}
Also used : EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaException(org.apache.tika.exception.TikaException) CipherInputStream(javax.crypto.CipherInputStream) GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) Key(java.security.Key) AlgorithmParameters(java.security.AlgorithmParameters)

Example 77 with CipherInputStream

use of javax.crypto.CipherInputStream in project tika by apache.

the class EncryptedPrescriptionDetector method detect.

public MediaType detect(InputStream stream, Metadata metadata) throws IOException {
    Key key = Pharmacy.getKey();
    MediaType type = MediaType.OCTET_STREAM;
    try (InputStream lookahead = new LookaheadInputStream(stream, 1024)) {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        InputStream decrypted = new CipherInputStream(lookahead, cipher);
        QName name = new XmlRootExtractor().extractRootElement(decrypted);
        if (name != null && "http://example.com/xpd".equals(name.getNamespaceURI()) && "prescription".equals(name.getLocalPart())) {
            type = MediaType.application("x-prescription");
        }
    } catch (GeneralSecurityException e) {
    // unable to decrypt, fall through
    }
    return type;
}
Also used : LookaheadInputStream(org.apache.tika.io.LookaheadInputStream) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) LookaheadInputStream(org.apache.tika.io.LookaheadInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) GeneralSecurityException(java.security.GeneralSecurityException) MediaType(org.apache.tika.mime.MediaType) XmlRootExtractor(org.apache.tika.detect.XmlRootExtractor) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 78 with CipherInputStream

use of javax.crypto.CipherInputStream in project briefcase by opendatakit.

the class ExportToCsv method getSubmissionValue.

private String getSubmissionValue(EncryptionInformation ei, TreeElement model, Element element) {
    // could not find element, return null
    if (element == null) {
        return null;
    }
    StringBuilder b = new StringBuilder();
    int maxChildren = element.getChildCount();
    for (int i = 0; i < maxChildren; i++) {
        if (element.getType(i) == Node.TEXT) {
            b.append(element.getText(i));
        }
    }
    String rawElement = b.toString();
    // Field-level encryption support -- experimental
    if (JavaRosaParserWrapper.isEncryptedField(model)) {
        InputStreamReader isr = null;
        try {
            Cipher c = ei.getCipher("field:" + model.getName(), model.getName());
            isr = new InputStreamReader(new CipherInputStream(new ByteArrayInputStream(Base64.decodeBase64(rawElement)), c), "UTF-8");
            b.setLength(0);
            int ch;
            while ((ch = isr.read()) != -1) {
                char theChar = (char) ch;
                b.append(theChar);
            }
            return b.toString();
        } catch (IOException e) {
            log.debug(" element name: " + model.getName() + " exception: " + e);
        } catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
            log.debug(" element name: " + model.getName() + " exception: " + e, e);
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    log.error("failed to close reader", e);
                }
            }
        }
    }
    return rawElement;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InputStreamReader(java.io.InputStreamReader) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ByteArrayInputStream(java.io.ByteArrayInputStream) Cipher(javax.crypto.Cipher)

Example 79 with CipherInputStream

use of javax.crypto.CipherInputStream in project opencast by opencast.

the class Crypt method decrypt.

public static String decrypt(Key key, String encryptedText) {
    final ByteArrayOutputStream decrypted;
    try {
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        final byte[] decodeBuffer = new byte[1024];
        final ByteArrayInputStream encrypted = new ByteArrayInputStream(decodeFromHex(encryptedText));
        decrypted = new ByteArrayOutputStream();
        final CipherInputStream cipherStream = new CipherInputStream(encrypted, cipher);
        try {
            int n;
            while ((n = cipherStream.read(decodeBuffer)) > 0) {
                decrypted.write(decodeBuffer, 0, n);
            }
        } finally {
            IoSupport.closeQuietly(decrypted);
            IoSupport.closeQuietly(cipherStream);
            IoSupport.closeQuietly(encrypted);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error decrypting text", e);
    }
    return decrypted.toString();
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher)

Example 80 with CipherInputStream

use of javax.crypto.CipherInputStream in project pdfbox by apache.

the class SecurityHandler method encryptDataAES256.

/**
 * Encrypt or decrypt data with AES256.
 *
 * @param data The data to encrypt.
 * @param output The output to write the encrypted data to.
 * @param decrypt true to decrypt the data, false to encrypt it.
 *
 * @throws IOException If there is an error reading the data.
 */
private void encryptDataAES256(InputStream data, OutputStream output, boolean decrypt) throws IOException {
    byte[] iv = new byte[16];
    if (!prepareAESInitializationVector(decrypt, iv, data, output)) {
        return;
    }
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    } catch (GeneralSecurityException e) {
        throw new IOException(e);
    }
    try (CipherInputStream cis = new CipherInputStream(data, cipher)) {
        IOUtils.copy(cis, output);
    } catch (IOException exception) {
        // it should be safe to swallow a GeneralSecurityException
        if (!(exception.getCause() instanceof GeneralSecurityException)) {
            throw exception;
        }
        LOG.debug("A GeneralSecurityException occured when decrypting some stream data", exception);
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)102 Cipher (javax.crypto.Cipher)66 IOException (java.io.IOException)42 InputStream (java.io.InputStream)32 ByteArrayInputStream (java.io.ByteArrayInputStream)30 IvParameterSpec (javax.crypto.spec.IvParameterSpec)21 SecretKeySpec (javax.crypto.spec.SecretKeySpec)21 FileInputStream (java.io.FileInputStream)19 InvalidKeyException (java.security.InvalidKeyException)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)13 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 NullCipher (javax.crypto.NullCipher)11 DataInputStream (java.io.DataInputStream)9 RuntimeException (java.lang.RuntimeException)9 GeneralSecurityException (java.security.GeneralSecurityException)9 CipherOutputStream (javax.crypto.CipherOutputStream)8 BufferedInputStream (java.io.BufferedInputStream)7 Key (java.security.Key)7