Search in sources :

Example 51 with CipherInputStream

use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.

the class CipherInputStreamExceptions method cbc_shortRead600.

/* Check that close() does not throw an exception when the  inside the
     * internal buffer (ibuffer) in CipherInputStream does not contain the
     * whole message.
     * This test:
     *   1) Encrypts a 600 byte message with AES/CBC/PKCS5Padding
     *   2) Read one byte from the stream
     *   3) Close and expect no exception
     */
static void cbc_shortRead600() throws Exception {
    System.out.println("Running cbc_shortRead600");
    // Encrypt 600 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 600);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);
    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) IOException(java.io.IOException)

Example 52 with CipherInputStream

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

the class StandardDecryptor method getDataStream.

@Override
@SuppressWarnings("resource")
public InputStream getDataStream(DirectoryNode dir) throws IOException {
    DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
    _length = dis.readLong();
    if (getSecretKey() == null) {
        verifyPassword(null);
    }
    // limit wrong calculated ole entries - (bug #57080)
    // standard encryption always uses aes encoding, so blockSize is always 16 
    // http://stackoverflow.com/questions/3283787/size-of-data-after-aes-encryption
    int blockSize = getEncryptionInfo().getHeader().getCipherAlgorithm().blockSize;
    long cipherLen = (_length / blockSize + 1) * blockSize;
    Cipher cipher = getCipher(getSecretKey());
    InputStream boundedDis = new BoundedInputStream(dis, cipherLen);
    return new BoundedInputStream(new CipherInputStream(boundedDis, cipher), _length);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) InputStream(java.io.InputStream) BoundedInputStream(org.apache.poi.util.BoundedInputStream) Cipher(javax.crypto.Cipher) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 53 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 54 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)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)54 Cipher (javax.crypto.Cipher)31 ByteArrayInputStream (java.io.ByteArrayInputStream)18 IOException (java.io.IOException)17 InputStream (java.io.InputStream)14 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 Key (java.security.Key)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 IvParameterSpec (javax.crypto.spec.IvParameterSpec)6 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 Throwable (java.lang.Throwable)3 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3