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);
}
}
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 briefcase by opendatakit.
the class ExportToCsv method getSubmissionValue.
private String getSubmissionValue(EncryptionInformation ei, TreeElement model, Element element) {
// could not find element, return null
if (element == null) {
return null;
}
StringBuilder b = new StringBuilder();
int maxChildren = element.getChildCount();
for (int i = 0; i < maxChildren; i++) {
if (element.getType(i) == Node.TEXT) {
b.append(element.getText(i));
}
}
String rawElement = b.toString();
// Field-level encryption support -- experimental
if (JavaRosaParserWrapper.isEncryptedField(model)) {
InputStreamReader isr = null;
try {
Cipher c = ei.getCipher("field:" + model.getName(), model.getName());
isr = new InputStreamReader(new CipherInputStream(new ByteArrayInputStream(Base64.decodeBase64(rawElement)), c), "UTF-8");
b.setLength(0);
int ch;
while ((ch = isr.read()) != -1) {
char theChar = (char) ch;
b.append(theChar);
}
return b.toString();
} catch (IOException e) {
log.debug(" element name: " + model.getName() + " exception: " + e);
} catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
log.debug(" element name: " + model.getName() + " exception: " + e, e);
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
log.error("failed to close reader", e);
}
}
}
}
return rawElement;
}
use of javax.crypto.CipherInputStream in project opencast by opencast.
the class Crypt method decrypt.
public static String decrypt(Key key, String encryptedText) {
final ByteArrayOutputStream decrypted;
try {
final Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
final byte[] decodeBuffer = new byte[1024];
final ByteArrayInputStream encrypted = new ByteArrayInputStream(decodeFromHex(encryptedText));
decrypted = new ByteArrayOutputStream();
final CipherInputStream cipherStream = new CipherInputStream(encrypted, cipher);
try {
int n;
while ((n = cipherStream.read(decodeBuffer)) > 0) {
decrypted.write(decodeBuffer, 0, n);
}
} finally {
IoSupport.closeQuietly(decrypted);
IoSupport.closeQuietly(cipherStream);
IoSupport.closeQuietly(encrypted);
}
} catch (Exception e) {
throw new RuntimeException("Error decrypting text", e);
}
return decrypted.toString();
}
use of javax.crypto.CipherInputStream in project pdfbox by apache.
the class SecurityHandler method encryptDataAES256.
/**
* Encrypt or decrypt data with AES256.
*
* @param data The data to encrypt.
* @param output The output to write the encrypted data to.
* @param decrypt true to decrypt the data, false to encrypt it.
*
* @throws IOException If there is an error reading the data.
*/
private void encryptDataAES256(InputStream data, OutputStream output, boolean decrypt) throws IOException {
byte[] iv = new byte[16];
if (!prepareAESInitializationVector(decrypt, iv, data, output)) {
return;
}
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, keySpec, ivSpec);
} catch (GeneralSecurityException e) {
throw new IOException(e);
}
try (CipherInputStream cis = new CipherInputStream(data, cipher)) {
IOUtils.copy(cis, output);
} catch (IOException exception) {
// it should be safe to swallow a GeneralSecurityException
if (!(exception.getCause() instanceof GeneralSecurityException)) {
throw exception;
}
LOG.debug("A GeneralSecurityException occured when decrypting some stream data", exception);
}
}
Aggregations