Search in sources :

Example 31 with CipherInputStream

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;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) CipherInputStream(javax.crypto.CipherInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) InvalidKeyException(java.security.InvalidKeyException) BufferedInputStream(java.io.BufferedInputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 32 with CipherInputStream

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();
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 33 with CipherInputStream

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) {
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher) DataInputStream(java.io.DataInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) PushbackInputStream(java.io.PushbackInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) CipherInputStream(javax.crypto.CipherInputStream) LineNumberInputStream(java.io.LineNumberInputStream) FilterInputStream(java.io.FilterInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream)

Example 34 with CipherInputStream

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));
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Example 35 with CipherInputStream

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"));
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)58 Cipher (javax.crypto.Cipher)33 IOException (java.io.IOException)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 Key (java.security.Key)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)5 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidKeyException (java.security.InvalidKeyException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 BufferedInputStream (java.io.BufferedInputStream)3 DataInputStream (java.io.DataInputStream)3