Search in sources :

Example 66 with CipherInputStream

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

the class CICO method runTest.

public void runTest(String algo, String mo, String pad, int whichRead) throws Exception {
    Cipher ci1 = null;
    Cipher ci2 = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Do initialization
        Random rdm = new Random();
        rdm.nextBytes(plainText);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        if (!kg.getAlgorithm().equals(algo)) {
            throw new RuntimeException("Unexpected algorithm <" + kg.getAlgorithm() + ">, expected value is <" + algo + ">");
        }
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        if (mo.equalsIgnoreCase("ECB")) {
            ci1.init(Cipher.ENCRYPT_MODE, key);
        } else {
            ci1.init(Cipher.ENCRYPT_MODE, key, aps);
        }
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci1.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        if (mo.equalsIgnoreCase("ECB")) {
            ci2.init(Cipher.DECRYPT_MODE, key);
        } else {
            ci2.init(Cipher.DECRYPT_MODE, key, aps);
        }
        ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
        ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
        try (CipherInputStream ciInput = new CipherInputStream(baInput, ci1);
            CipherOutputStream ciOutput = new CipherOutputStream(baOutput, ci2)) {
            // mark and reset methods
            if (ciInput.markSupported()) {
                throw new RuntimeException("CipherInputStream unexpectedly supports the mark and reset methods");
            }
            // of buffering : byte[] and int
            switch(whichRead) {
                case 0:
                    int buffer0 = ciInput.read();
                    while (buffer0 != -1) {
                        ciOutput.write(buffer0);
                        buffer0 = ciInput.read();
                    }
                    break;
                case 1:
                    byte[] buffer1 = new byte[20];
                    int len1 = ciInput.read(buffer1);
                    while (len1 != -1) {
                        ciOutput.write(buffer1, 0, len1);
                        len1 = ciInput.read(buffer1);
                    }
                    break;
                case NREADS - 1:
                    byte[] buffer2 = new byte[ci1.getOutputSize(plainText.length)];
                    int offset2 = 0;
                    int len2 = 0;
                    while (len2 != -1) {
                        len2 = ciInput.read(buffer2, offset2, buffer2.length - offset2);
                        offset2 += len2;
                    }
                    ciOutput.write(buffer2, 0, buffer2.length);
                    break;
            }
        }
        // Get the output
        byte[] recoveredText = new byte[baOutput.size()];
        recoveredText = baOutput.toByteArray();
        if (!java.util.Arrays.equals(plainText, recoveredText)) {
            throw new RuntimeException("Original text is not equal with recovered text, with " + algo + "/" + mo + "/" + pad + "/" + whichRead);
        }
    // Compare input and output
    } catch (NoSuchAlgorithmException e) {
        //OFB20 is for negative testing
        if (!mo.equalsIgnoreCase("OFB20")) {
            System.out.println("Unexpected NoSuchAlgorithmException with " + algo + "/" + mo + "/" + pad + "/" + whichRead);
            throw new RuntimeException("Test failed!");
        }
    } catch (IOException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
        System.out.println("Unexpected Exception with " + algo + "/" + mo + "/" + pad + "/" + whichRead);
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 67 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 68 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 69 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 70 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)

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