Search in sources :

Example 61 with CipherInputStream

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

the class CipherInputStream1Test method testRead1.

/**
     * read() method testing. Tests that method returns the correct value
     * (related to the InputStream) and that it returns -1 at the end of stream.
     */
public void testRead1() 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());
    byte res;
    for (int i = 0; i < data.length; i++) {
        if ((res = (byte) cis.read()) != data[i]) {
            fail("read() returned the incorrect value. " + "Expected: " + data[i] + ", Got: " + res + ".");
        }
    }
    if (cis.read() != -1) {
        fail("read() should return -1 at the end of the stream.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 62 with CipherInputStream

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

the class CipherInputStream1Test method testRead2.

/**
     * read(byte[] b) method testing. Tests that method returns the correct
     * value (related to the InputStream) and that it returns -1 at the end of
     * stream.
     */
public void testRead2() 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];
    // index into the data array (to check the got data)
    int ind = 0;
    // the number of got bytes
    int got = cis.read(result);
    while (true) {
        for (int j = 0; j < got - ind; j++) {
            if (result[j] != data[ind + j]) {
                fail("read(byte[] b) returned incorrect data.");
            }
        }
        if (got == expected) {
            break;
        } else if (got > expected) {
            fail("The data returned by read(byte[] b) " + "is larger than expected.");
        } else {
            ind = got;
            got += cis.read(result);
        }
    }
    if (cis.read(result) != -1) {
        fail("read(byte[] b) should return -1 " + "at the end of the stream.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 63 with CipherInputStream

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

the class CipherInputStream1Test method testClose.

/**
     * close() method testing. Tests that the method calls the close()
     * method of the underlying input stream.
     */
public void testClose() 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());
    cis.close();
    assertTrue("The close() method should call the close() method " + "of its underlying input stream.", tis.wasClosed());
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 64 with CipherInputStream

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

the class CipherInputStream1Test method testMarkSupported.

/**
     * markSupported() method testing. Tests that mark is not supported.
     */
public void testMarkSupported() {
    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());
    assertFalse("The returned by markSupported() method value " + "should be false.", cis.markSupported());
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 65 with CipherInputStream

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

the class CipherHelper method desCbcDecrypt.

/**
     * Helper routine to decrypt from an InputStream and write the
     * application data straight to an output array with minimal
     * buffer copies. The confounder and the padding are stored
     * separately and not copied into this output array.
     * @param key the DES key to use
     * @param is the InputStream from which the cipher text should be
     * read
     * @param len the length of the ciphertext data
     * @param dataOutBuf the output buffer where the application data
     * should be writte
     * @param dataOffset the offser where the application data should
     * be written.
     * @throws GSSException is an error occurs while decrypting the
     * data
     */
private void desCbcDecrypt(WrapToken token, byte[] key, InputStream is, int len, byte[] dataOutBuf, int dataOffset) throws GSSException, IOException {
    int temp = 0;
    Cipher des = getInitializedDes(false, key, ZERO_IV);
    WrapTokenInputStream truncatedInputStream = new WrapTokenInputStream(is, len);
    CipherInputStream cis = new CipherInputStream(truncatedInputStream, des);
    /*
         * Remove the counfounder first.
         * CONFOUNDER_SIZE is one DES block ie 8 bytes.
         */
    temp = cis.read(token.confounder);
    len -= temp;
    // temp should be CONFOUNDER_SIZE
    // debug("Got " + temp + " bytes; CONFOUNDER_SIZE is "
    //     + CONFOUNDER_SIZE + "\n");
    // debug("Confounder is " + getHexBytes(confounder) + "\n");
    /*
         * len is a multiple of 8 due to padding.
         * Decrypt all blocks directly into the output buffer except for
         * the very last block. Remove the trailing padding bytes from the
         * very last block and copy that into the output buffer.
         */
    int blockSize = des.getBlockSize();
    int numBlocks = len / blockSize - 1;
    // Iterate over all but the last block
    for (int i = 0; i < numBlocks; i++) {
        // debug("dataOffset is " + dataOffset + "\n");
        temp = cis.read(dataOutBuf, dataOffset, blockSize);
        // temp should be blockSize
        // debug("Got " + temp + " bytes and blockSize is "
        //    + blockSize + "\n");
        // debug("Bytes are: "
        //    + getHexBytes(dataOutBuf, dataOffset, temp) + "\n");
        dataOffset += blockSize;
    }
    // Now process the last block
    byte[] finalBlock = new byte[blockSize];
    // debug("Will call read on finalBlock" + "\n");
    temp = cis.read(finalBlock);
    /*
          debug("Got " + temp + " bytes and blockSize is "
          + blockSize + "\n");
          debug("Bytes are: "
          + getHexBytes(finalBlock, 0, temp) + "\n");
          debug("Will call doFinal" + "\n");
        */
    try {
        des.doFinal();
    } catch (GeneralSecurityException e) {
        GSSException ge = new GSSException(GSSException.FAILURE, -1, "Could not use DES cipher - " + e.getMessage());
        ge.initCause(e);
        throw ge;
    }
    /*
         * There is always at least one padding byte. The padding bytes
         * are all the value of the number of padding bytes.
         */
    int padSize = finalBlock[blockSize - 1];
    if (padSize < 1 || padSize > 8)
        throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1, "Invalid padding on Wrap Token");
    token.padding = WrapToken.pads[padSize];
    blockSize -= padSize;
    // Copy this last block into the output buffer
    System.arraycopy(finalBlock, 0, dataOutBuf, dataOffset, blockSize);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher)

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