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);
}
use of javax.crypto.CipherInputStream in project tika by apache.
the class EncryptedPrescriptionDetector method detect.
public MediaType detect(InputStream stream, Metadata metadata) throws IOException {
Key key = Pharmacy.getKey();
MediaType type = MediaType.OCTET_STREAM;
try (InputStream lookahead = new LookaheadInputStream(stream, 1024)) {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream decrypted = new CipherInputStream(lookahead, cipher);
QName name = new XmlRootExtractor().extractRootElement(decrypted);
if (name != null && "http://example.com/xpd".equals(name.getNamespaceURI()) && "prescription".equals(name.getLocalPart())) {
type = MediaType.application("x-prescription");
}
} catch (GeneralSecurityException e) {
// unable to decrypt, fall through
}
return type;
}
use of javax.crypto.CipherInputStream in project tika by apache.
the class CryptoParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
try {
Cipher cipher;
if (provider != null) {
cipher = Cipher.getInstance(transformation, provider);
} else {
cipher = Cipher.getInstance(transformation);
}
Key key = context.get(Key.class);
if (key == null) {
throw new EncryptedDocumentException("No decryption key provided");
}
AlgorithmParameters params = context.get(AlgorithmParameters.class);
SecureRandom random = context.get(SecureRandom.class);
if (params != null && random != null) {
cipher.init(Cipher.DECRYPT_MODE, key, params, random);
} else if (params != null) {
cipher.init(Cipher.DECRYPT_MODE, key, params);
} else if (random != null) {
cipher.init(Cipher.DECRYPT_MODE, key, random);
} else {
cipher.init(Cipher.DECRYPT_MODE, key);
}
super.parse(new CipherInputStream(stream, cipher), handler, metadata, context);
} catch (GeneralSecurityException e) {
throw new TikaException("Unable to decrypt document stream", e);
}
}
Aggregations