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.");
}
}
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());
}
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());
}
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);
}
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;
}
}
Aggregations