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 cxf by apache.
the class CachedOutputStream method getInputStream.
public InputStream getInputStream() throws IOException {
flush();
if (inmem) {
if (currentStream instanceof LoadingByteArrayOutputStream) {
return ((LoadingByteArrayOutputStream) currentStream).createInputStream();
} else if (currentStream instanceof ByteArrayOutputStream) {
return new ByteArrayInputStream(((ByteArrayOutputStream) currentStream).toByteArray());
} else {
return null;
}
}
try {
InputStream fileInputStream = new TransferableFileInputStream(tempFile);
streamList.add(fileInputStream);
if (cipherTransformation != null) {
fileInputStream = new CipherInputStream(fileInputStream, ciphers.getDecryptor()) {
boolean closed;
public void close() throws IOException {
if (!closed) {
super.close();
closed = true;
}
}
};
}
return fileInputStream;
} catch (FileNotFoundException e) {
throw new IOException("Cached file was deleted, " + e.toString());
}
}
use of javax.crypto.CipherInputStream in project apex-malhar by apache.
the class AbstractFileOutputOperatorTest method checkCompressedFile.
private void checkCompressedFile(File file, List<Long> offsets, int startVal, int totalWindows, int totalRecords, SecretKey secretKey, byte[] iv) throws IOException {
FileInputStream fis;
InputStream gss = null;
GZIPInputStream gis = null;
BufferedReader br = null;
Cipher cipher = null;
if (secretKey != null) {
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivps);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
int numWindows = 0;
try {
fis = new FileInputStream(file);
// fis.skip(startOffset);
gss = fis;
if (secretKey != null) {
try {
/*
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivps);
*/
gss = new CipherInputStream(fis, cipher);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
long startOffset = 0;
for (long offset : offsets) {
// Skip initial case in case file is not yet created
if (offset == 0) {
continue;
}
long limit = offset - startOffset;
LimitInputStream lis = new LimitInputStream(gss, limit);
// gis = new GZIPInputStream(fis);
gis = new GZIPInputStream(lis);
br = new BufferedReader(new InputStreamReader(gis));
// br = new BufferedReader(new InputStreamReader(gss));
String eline = "" + (startVal + numWindows * 2);
int count = 0;
String line;
while ((line = br.readLine()) != null) {
Assert.assertEquals("File line", eline, line);
++count;
if ((count % totalRecords) == 0) {
++numWindows;
eline = "" + (startVal + numWindows * 2);
}
}
startOffset = offset;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
} else {
if (gis != null) {
gis.close();
} else if (gss != null) {
gss.close();
}
}
}
Assert.assertEquals("Total", totalWindows, numWindows);
}
Aggregations