Search in sources :

Example 46 with CipherInputStream

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

the class ReadWriteSkip method doTest.

final void doTest(BufferType type) throws Exception {
    // init ciphers
    encryptCipher = createCipher(Cipher.ENCRYPT_MODE);
    decryptCipher = createCipher(Cipher.DECRYPT_MODE);
    // init cipher input stream
    ciInput = new CipherInputStream(new ByteArrayInputStream(plaintext), encryptCipher);
    runTest(type);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 47 with CipherInputStream

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

the class CipherInputStreamExceptions method cbc_shortStream.

/* Check that close() does not throw an exception with full message in
     * CipherInputStream's ibuffer.
     * This test:
     *   1) Encrypts a 97 byte message with AES/CBC/PKCS5Padding
     *   2) Create a stream that sends 96 bytes.
     *   3) Read stream once,
     *   4) Close and expect no exception
     */
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];
    System.out.println("Running cbc_shortStream");
    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 96);
    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " + "returned " + size + ". Should have been 80");
        }
        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) AlgorithmParameters(java.security.AlgorithmParameters)

Example 48 with CipherInputStream

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

the class CipherInputStreamExceptions method getStream.

/* Generic method to get a properly setup CipherInputStream */
static CipherInputStream getStream(String mode, byte[] ct, int length) throws Exception {
    Cipher c;
    if (mode.compareTo("GCM") == 0) {
        c = Cipher.getInstance("AES/GCM/PKCS5Padding", "SunJCE");
        c.init(Cipher.DECRYPT_MODE, key, gcmspec);
    } else if (mode.compareTo("CBC") == 0) {
        c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
        c.init(Cipher.DECRYPT_MODE, key, iv);
    } else {
        return null;
    }
    return new CipherInputStream(new ByteArrayInputStream(ct, 0, length), c);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Cipher(javax.crypto.Cipher)

Example 49 with CipherInputStream

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

the class CipherInputStreamExceptions method cbc_readAllIllegalBlockSize.

/* Check that exception is thrown when message is fully read
     * This test:
     *   1) Encrypts a 96 byte message with AES/CBC/PKCS5Padding
     *   2) Create a stream that sends 95 bytes.
     *   3) Read stream to the end
     *   4) Expect IllegalBlockSizeException thrown
     */
static void cbc_readAllIllegalBlockSize() throws Exception {
    byte[] read = new byte[200];
    System.out.println("Running cbc_readAllIllegalBlockSize test");
    // Encrypt 96 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 96);
    // Create a stream with only 95 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 95);
    try {
        int s, size = 0;
        while ((s = in.read(read)) != -1) {
            size += s;
        }
        throw new RuntimeException("Fail: No IllegalBlockSizeException. " + "CipherInputStream.read() returned " + size);
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof IllegalBlockSizeException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Throwable(java.lang.Throwable) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException)

Example 50 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)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)58 Cipher (javax.crypto.Cipher)33 IOException (java.io.IOException)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 Key (java.security.Key)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)5 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidKeyException (java.security.InvalidKeyException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 BufferedInputStream (java.io.BufferedInputStream)3 DataInputStream (java.io.DataInputStream)3