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