use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.
the class WrongAAD method decrypt.
private boolean decrypt(CipherOutputStream ciOutput, ByteArrayOutputStream baOutput) throws IOException {
try (ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
CipherInputStream ciInput = new CipherInputStream(baInput, encryptCipher)) {
byte[] buffer = new byte[TEXT_SIZE];
int len = ciInput.read(buffer);
while (len != -1) {
ciOutput.write(buffer, 0, len);
len = ciInput.read(buffer);
}
ciOutput.flush();
byte[] recoveredText = baOutput.toByteArray();
System.out.println("recoveredText: " + new String(recoveredText));
/*
* See bug 8012900, AEADBadTagException is swalloed by CI/CO streams
* If recovered text is empty, than decryption failed
*/
if (recoveredText.length == 0) {
return false;
}
return Arrays.equals(plainText, recoveredText);
} catch (IllegalStateException e) {
System.out.println("Expected IllegalStateException: " + e.getMessage());
e.printStackTrace(System.out);
return false;
}
}
use of javax.crypto.CipherInputStream in project tika by apache.
the class EncryptedPrescriptionParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
try {
Key key = Pharmacy.getKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream decrypted = new CipherInputStream(stream, cipher);
new PrescriptionParser().parse(decrypted, handler, metadata, context);
} catch (GeneralSecurityException e) {
throw new TikaException("Unable to decrypt a digital prescription", e);
}
}
use of javax.crypto.CipherInputStream in project bnd by bndtools.
the class PasswordCryptor method decrypt.
public InputStream decrypt(char[] password, InputStream out) throws Exception {
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
final Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
return new CipherInputStream(out, cipher);
}
use of javax.crypto.CipherInputStream in project ExoPlayer by google.
the class CachedContentIndex method readFile.
private boolean readFile() {
DataInputStream input = null;
try {
InputStream inputStream = new BufferedInputStream(atomicFile.openRead());
input = new DataInputStream(inputStream);
int version = input.readInt();
if (version != VERSION) {
// Currently there is no other version
return false;
}
int flags = input.readInt();
if ((flags & FLAG_ENCRYPTED_INDEX) != 0) {
if (cipher == null) {
return false;
}
byte[] initializationVector = new byte[16];
input.readFully(initializationVector);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
try {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalStateException(e);
}
input = new DataInputStream(new CipherInputStream(inputStream, cipher));
} else {
if (cipher != null) {
// Force index to be rewritten encrypted after read.
changed = true;
}
}
int count = input.readInt();
int hashCode = 0;
for (int i = 0; i < count; i++) {
CachedContent cachedContent = new CachedContent(input);
add(cachedContent);
hashCode += cachedContent.headerHashCode();
}
if (input.readInt() != hashCode) {
return false;
}
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
Log.e(TAG, "Error reading cache content index file.", e);
return false;
} finally {
if (input != null) {
Util.closeQuietly(input);
}
}
return true;
}
use of javax.crypto.CipherInputStream in project wycheproof by google.
the class CipherInputStreamTest method testCorruptDecryptEmpty.
@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecryptEmpty(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
Cipher cipher = Cipher.getInstance(t.algorithm);
cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
ct[ct.length - 1] ^= (byte) 1;
InputStream is = new ByteArrayInputStream(ct);
CipherInputStream cis = new CipherInputStream(is, cipher);
try {
byte[] result = new byte[t.pt.length];
int totalLength = 0;
int length = 0;
do {
length = cis.read(result, totalLength, result.length - totalLength);
if (length > 0) {
totalLength += length;
}
} while (length >= 0 && totalLength != result.length);
cis.close();
fail("this should fail");
} catch (IOException ex) {
// expected
}
}
}
Aggregations