use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.
the class CipherInputStreamExceptions method gcm_suppressUnreadCorrupt.
/*
* Verify doFinal() exception is suppressed when input stream is not
* read before it is closed.
* This test:
* 1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
* 2) Changes the last byte to invalidate the authetication tag.
* 3) Opens a CipherInputStream and the closes it. Never reads from it.
*
* There should be no exception thrown.
*/
static void gcm_suppressUnreadCorrupt() throws Exception {
Cipher c;
byte[] read = new byte[200];
System.out.println("Running supressUnreadCorrupt test");
// Encrypt 100 bytes with AES/GCM/PKCS5Padding
byte[] ct = encryptedText("GCM", 100);
// Corrupt the encrypted message
ct = corruptGCM(ct);
// Create stream for decryption
CipherInputStream in = getStream("GCM", ct);
try {
in.close();
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 CipherStreamClose method streamDecrypt.
public static Object streamDecrypt(byte[] data, SecretKey key, MessageDigest digest) throws Exception {
Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
decCipher.init(Cipher.DECRYPT_MODE, key);
digest.reset();
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
DigestInputStream dis = new DigestInputStream(bis, digest);
CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
try (ObjectInputStream ois = new ObjectInputStream(cis)) {
return ois.readObject();
}
}
}
use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.
the class CipherInputStreamExceptions method cbc_shortRead400.
/* Check that close() does not throw an exception when the whole message is
* inside the internal buffer (ibuffer) in CipherInputStream and we read
* one byte and close the stream.
* This test:
* 1) Encrypts a 400 byte message with AES/CBC/PKCS5Padding
* 2) Read one byte from the stream
* 3) Close and expect no exception
*/
static void cbc_shortRead400() throws Exception {
System.out.println("Running cbc_shortRead400");
// Encrypt 400 byte with AES/CBC/PKCS5Padding
byte[] ct = encryptedText("CBC", 400);
// Create stream with encrypted data
CipherInputStream in = getStream("CBC", ct);
try {
in.read();
in.close();
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 cbc_shortRead600.
/* Check that close() does not throw an exception when the inside the
* internal buffer (ibuffer) in CipherInputStream does not contain the
* whole message.
* This test:
* 1) Encrypts a 600 byte message with AES/CBC/PKCS5Padding
* 2) Read one byte from the stream
* 3) Close and expect no exception
*/
static void cbc_shortRead600() throws Exception {
System.out.println("Running cbc_shortRead600");
// Encrypt 600 byte with AES/CBC/PKCS5Padding
byte[] ct = encryptedText("CBC", 600);
// Create stream with encrypted data
CipherInputStream in = getStream("CBC", ct);
try {
in.read();
in.close();
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 poi by apache.
the class StandardDecryptor method getDataStream.
@Override
@SuppressWarnings("resource")
public InputStream getDataStream(DirectoryNode dir) throws IOException {
DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
_length = dis.readLong();
if (getSecretKey() == null) {
verifyPassword(null);
}
// limit wrong calculated ole entries - (bug #57080)
// standard encryption always uses aes encoding, so blockSize is always 16
// http://stackoverflow.com/questions/3283787/size-of-data-after-aes-encryption
int blockSize = getEncryptionInfo().getHeader().getCipherAlgorithm().blockSize;
long cipherLen = (_length / blockSize + 1) * blockSize;
Cipher cipher = getCipher(getSecretKey());
InputStream boundedDis = new BoundedInputStream(dis, cipherLen);
return new BoundedInputStream(new CipherInputStream(boundedDis, cipher), _length);
}
Aggregations