Search in sources :

Example 1 with CipherInputStream

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

the class CipherInputStreamExceptions method gcm_oneReadByte.

/*
     * Verify noexception thrown when 1 byte is read from a GCM stream
     * and then closed
     * This test:
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Read one byte from the stream, expect no exception thrown.
     *   4) Close stream,expect no exception thrown.
     */
static void gcm_oneReadByte() throws Exception {
    System.out.println("Running gcm_oneReadByte test");
    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    try {
        in.read();
        System.out.println("  Pass.");
    } catch (Exception e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) RuntimeException(java.lang.RuntimeException) AEADBadTagException(javax.crypto.AEADBadTagException) Exception(java.lang.Exception)

Example 2 with CipherInputStream

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

the class CipherInputStreamExceptions method gcm_AEADBadTag.

/* Full read stream, check that getMoreData() is throwing an exception
     * This test
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Changes the last byte to invalidate the authetication tag.
     *   3) Fully reads CipherInputStream to decrypt the message and closes
     */
static void gcm_AEADBadTag() throws Exception {
    Cipher c;
    byte[] read = new byte[200];
    System.out.println("Running gcm_AEADBadTag");
    // 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 {
        int size = in.read(read);
        throw new RuntimeException("Fail: CipherInputStream.read() " + "returned " + size + " and didn't throw an exception.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    } finally {
        in.close();
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Throwable(java.lang.Throwable) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 3 with CipherInputStream

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

the class CipherInputStreamExceptions method gcm_oneReadByteCorrupt.

/*
     * Verify exception thrown when 1 byte is read from a corrupted GCM stream
     * and then closed
     * This test:
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Changes the last byte to invalidate the authetication tag.
     *   3) Read one byte from the stream, expect exception thrown.
     *   4) Close stream,expect no exception thrown.
     */
static void gcm_oneReadByteCorrupt() throws Exception {
    System.out.println("Running gcm_oneReadByteCorrupt 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.read();
        System.out.println("  Fail. No exception thrown.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            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) IOException(java.io.IOException) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 4 with CipherInputStream

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

the class CipherInputStreamExceptions method gcm_shortReadAEAD.

/* Short read stream,
     * This test
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Reads 100 bytes from stream to decrypt the message and closes
     *   3) Make sure no value is returned by read()
     *   4) Make sure no exception is thrown
     */
static void gcm_shortReadAEAD() throws Exception {
    Cipher c;
    byte[] read = new byte[100];
    System.out.println("Running gcm_shortReadAEAD");
    byte[] pt = new byte[600];
    pt[0] = 1;
    // Encrypt provided 600 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", pt);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    int size = 0;
    try {
        size = in.read(read);
        in.close();
        if (read.length != 100) {
            throw new RuntimeException("Fail: read size = " + read.length + "should be 100.");
        }
        if (read[0] != 1) {
            throw new RuntimeException("Fail: The decrypted text does " + "not match the plaintext: '" + read[0] + "'");
        }
    } catch (IOException e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
    System.out.println("  Pass.");
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 5 with CipherInputStream

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

the class CipherInputStreamTest method testSkip.

public void testSkip() throws Exception {
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    InputStream in = new CipherInputStream(new ByteArrayInputStream(cipherText), cipher);
    assertTrue(in.skip(5) > 0);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)97 Cipher (javax.crypto.Cipher)61 IOException (java.io.IOException)40 InputStream (java.io.InputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SecretKeySpec (javax.crypto.spec.SecretKeySpec)19 FileInputStream (java.io.FileInputStream)18 IvParameterSpec (javax.crypto.spec.IvParameterSpec)18 InvalidKeyException (java.security.InvalidKeyException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 NullCipher (javax.crypto.NullCipher)11 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)10 RuntimeException (java.lang.RuntimeException)9 DataInputStream (java.io.DataInputStream)8 GeneralSecurityException (java.security.GeneralSecurityException)8 CipherOutputStream (javax.crypto.CipherOutputStream)8 BufferedInputStream (java.io.BufferedInputStream)7 Key (java.security.Key)7