Search in sources :

Example 6 with CipherInputStream

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

the class CipherInputStream1Test method testSkip.

/**
     * skip(long n) method testing. Tests that the method correctly skips the
     * bytes.
     */
public void testSkip() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
    int expected = data.length;
    byte[] result = new byte[expected];
    int skipped = (int) cis.skip(2);
    int ind = skipped;
    // the number of got bytes
    int got = skipped + cis.read(result, 0, 1);
    while (true) {
        for (int j = 0; j < got - ind; j++) {
            if (result[j] != data[ind + j]) {
                fail("read(byte[] b, int off, int len) " + "returned incorrect data: Expected " + data[ind + j] + ", got: " + result[j]);
            }
        }
        if (got == expected) {
            break;
        } else if (got > expected) {
            fail("The data returned by " + "read(byte[] b, int off, int len) " + "is larger than expected.");
        } else {
            ind = got;
            got += cis.read(result, 0, 1);
        }
    }
    if ((got = cis.read(result, 0, 1)) != -1) {
        fail("read() should return -1 at the end of the stream. " + "Output is: " + got + ".");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 7 with CipherInputStream

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

the class CipherInputStream1Test method testRead3.

/**
     * read(byte[] b, int off, int len) method testing. Tests that method
     * returns the correct value (related to the InputStream), that it discards
     * bytes in the case of null buffer, and that it returns -1 at the end of
     * stream.
     */
public void testRead3() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
    int expected = data.length;
    byte[] result = new byte[expected];
    int skip = 2;
    // index into the data array (to check the got data)
    int ind = skip;
    // should read and discard bytes;
    cis.read(null, 0, skip);
    // the number of got bytes
    int got = skip + cis.read(result, 0, 1);
    while (true) {
        for (int j = 0; j < got - ind; j++) {
            assertEquals("read(byte[] b, int off, int len) " + "returned incorrect data.", result[j], data[ind + j]);
        }
        if (got == expected) {
            break;
        } else if (got > expected) {
            fail("The data returned by " + "read(byte[] b, int off, int len) " + "is larger than expected.");
        } else {
            ind = got;
            got += cis.read(result, 0, 3);
        }
    }
    if (cis.read(result, 0, 1) != -1) {
        fail("read() should return -1 at the end of the stream.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 8 with CipherInputStream

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

the class CipherInputStream1Test method testCipherInputStream.

/**
     * CipherInputStream(InputStream is) method testing. Tests that
     * CipherInputStream uses NullCipher if Cipher is not specified
     * in the constructor.
     */
public void testCipherInputStream() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis) {
    };
    for (int i = 0; i < data.length; i++) {
        if ((byte) cis.read() != data[i]) {
            fail("NullCipher should be used " + "if Cipher is not specified.");
        }
    }
    if (cis.read() != -1) {
        fail("NullCipher should be used if Cipher is not specified.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream)

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

Example 10 with CipherInputStream

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

the class CipherInputStreamTest method testCipherInputStream_TruncatedInput_Failure.

public void testCipherInputStream_TruncatedInput_Failure() throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new byte[16], "AES"), new IvParameterSpec(new byte[16]));
    InputStream is = new CipherInputStream(new ByteArrayInputStream(new byte[31]), cipher);
    is.read(new byte[4]);
    is.close();
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

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