Search in sources :

Example 71 with CipherInputStream

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

the class CipherInputStreamExceptions method gcm_suppressUnreadCorrupt.

/*
     * Verify doFinal() exception is suppressed when input stream is not
     * read before it is closed.
     * This test:
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Changes the last byte to invalidate the authetication tag.
     *   3) Opens a CipherInputStream and the closes it. Never reads from it.
     *
     * There should be no exception thrown.
     */
static void gcm_suppressUnreadCorrupt() throws Exception {
    Cipher c;
    byte[] read = new byte[200];
    System.out.println("Running supressUnreadCorrupt test");
    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    try {
        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) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 72 with CipherInputStream

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

the class CipherStreamClose method streamDecrypt.

public static Object streamDecrypt(byte[] data, SecretKey key, MessageDigest digest) throws Exception {
    Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    decCipher.init(Cipher.DECRYPT_MODE, key);
    digest.reset();
    try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
        try (ObjectInputStream ois = new ObjectInputStream(cis)) {
            return ois.readObject();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) CipherInputStream(javax.crypto.CipherInputStream) Cipher(javax.crypto.Cipher)

Example 73 with CipherInputStream

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

the class CipherInputStreamExceptions method cbc_shortRead400.

/* Check that close() does not throw an exception when the whole message is
     * inside the internal buffer (ibuffer) in CipherInputStream and we read
     * one byte and close the stream.
     * This test:
     *   1) Encrypts a 400 byte message with AES/CBC/PKCS5Padding
     *   2) Read one byte from the stream
     *   3) Close and expect no exception
     */
static void cbc_shortRead400() throws Exception {
    System.out.println("Running cbc_shortRead400");
    // Encrypt 400 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 400);
    // 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 74 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 75 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)

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