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 RxCache by VictorAlbertos.
the class BuiltInEncryptor method encrypt.
@Override
public void encrypt(String key, File decryptedFile, File encryptedFile) {
initCiphers(key);
try {
CipherInputStream cis = new CipherInputStream(new FileInputStream(decryptedFile), encryptCipher);
write(cis, new FileOutputStream(encryptedFile));
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class FilterInputStreamNullSourceTest method testCipherInputStream.
public void testCipherInputStream() throws IOException {
InputStream in = new CipherInputStream(null, new NullCipher());
try {
in.read();
fail();
} catch (NullPointerException expected) {
}
assertEquals(0, in.available());
try {
in.close();
fail();
} catch (NullPointerException expected) {
}
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStreamTest method testEncrypt.
public void testEncrypt() throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
InputStream in = new CipherInputStream(new ByteArrayInputStream(plainText.getBytes("UTF-8")), cipher);
byte[] bytes = readAll(in);
assertEquals(Arrays.toString(cipherText), Arrays.toString(bytes));
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStreamTest method testDecrypt.
public void testDecrypt() throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream in = new CipherInputStream(new ByteArrayInputStream(cipherText), cipher);
byte[] bytes = readAll(in);
assertEquals(plainText, new String(bytes, "UTF-8"));
}
Aggregations