Search in sources :

Example 1 with CipherOutputStream

use of org.bouncycastle.crypto.io.CipherOutputStream in project Zom-Android by zom.

the class Downloader method setupOutputStream.

public static OutputStream setupOutputStream(OutputStream os, String reference) {
    if (reference != null && reference.length() == 96) {
        byte[] keyAndIv = hexToBytes(reference);
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(keyAndIv, 0, iv, 0, 16);
        System.arraycopy(keyAndIv, 16, key, 0, 32);
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(false, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherOutputStream(os, cipher);
    } else {
        return os;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherOutputStream(org.bouncycastle.crypto.io.CipherOutputStream) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 2 with CipherOutputStream

use of org.bouncycastle.crypto.io.CipherOutputStream in project Conversations by siacs.

the class AbstractConnectionManager method createOutputStream.

public static OutputStream createOutputStream(DownloadableFile file, boolean append, boolean decrypt) {
    FileOutputStream os;
    try {
        os = new FileOutputStream(file, append);
        if (file.getKey() == null || !decrypt) {
            return os;
        }
    } catch (FileNotFoundException e) {
        Log.d(Config.LOGTAG, "unable to create output stream", e);
        return null;
    }
    try {
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
        return new CipherOutputStream(os, cipher);
    } catch (Exception e) {
        Log.d(Config.LOGTAG, "unable to create cipher output stream", e);
        return null;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherOutputStream(org.bouncycastle.crypto.io.CipherOutputStream) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) FileOutputStream(java.io.FileOutputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) FileNotFoundException(java.io.FileNotFoundException) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

AESEngine (org.bouncycastle.crypto.engines.AESEngine)2 CipherOutputStream (org.bouncycastle.crypto.io.CipherOutputStream)2 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)2 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)2 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)2 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1