org.bouncycastle.crypto.io.CipherInputStream Java Examples

The following examples show how to use org.bouncycastle.crypto.io.CipherInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecurityHandler.java    From sambox with Apache License 2.0 7 votes vote down vote up
/**
 * Encrypt or decrypt data with AES256.
 *
 * @param data The data to encrypt.
 * @param output The output to write the encrypted data to.
 *
 * @throws IOException If there is an error reading the data.
 */
private void decryptDataAES256(InputStream data, OutputStream output) throws IOException
{
    byte[] iv = new byte[16];

    // read IV from stream
    int ivSize = data.read(iv);
    if (ivSize == -1)
    {
        return;
    }

    if (ivSize != iv.length)
    {
        throw new IOException("AES initialization vector not fully read: only " + ivSize
                + " bytes read instead of " + iv.length);
    }
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv));
    try (CipherInputStream cis = new CipherInputStream(data, cipher))
    {
        IOUtils.copy(cis, output);
    }
}
 
Example #2
Source File: Downloader.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) {
    if (keyAndIv != null && keyAndIv.length == 48) {
        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(true, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
 
Example #3
Source File: AbstractConnectionManager.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
    if (file.getKey() != null && file.getIv() != null) {
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
 
Example #4
Source File: StreamCryptor.java    From InflatableDonkey with MIT License 5 votes vote down vote up
public CipherInputStream newCipherInputStream(InputStream is, byte[] password) throws IOException {
    byte[] salt = IOUtils.readFully(is, saltLength);
    byte[] nonce = IOUtils.readFully(is, nonceLength);
    byte[] dk = kdf.apply(password, salt);
    GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce);
    cipher.init(false, parameters);
    return new CipherInputStream(is, cipher);
}
 
Example #5
Source File: AbstractConnectionManager.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
    if (file.getKey() != null && file.getIv() != null) {
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
 
Example #6
Source File: CryptManager.java    From FishingBot with GNU General Public License v3.0 4 votes vote down vote up
public static InputStream decryptInputStream(SecretKey par0SecretKey, InputStream par1InputStream) {
    return new CipherInputStream(par1InputStream, createBufferedBlockCipher(false, par0SecretKey));
}
 
Example #7
Source File: EncryptionUtil.java    From AIBot with GNU General Public License v3.0 4 votes vote down vote up
public static InputStream decryptInputStream(InputStream inputStream, SecretKey key) {
	return new CipherInputStream(inputStream, createBlockCipher(key, false));
}
 
Example #8
Source File: ChunkListDecrypter.java    From InflatableDonkey with MIT License 4 votes vote down vote up
CipherInputStream cipherInputStream(InputStream inputStream, byte[] key, byte[] checksum) {
    CFBBlockCipher cipher = new CFBBlockCipher(new AESFastEngine(), 128);
    KeyParameter keyParameter = new KeyParameter(key);
    cipher.init(false, keyParameter);
    return new CipherInputStream(inputStream, cipher);
}
 
Example #9
Source File: FileStreamWriter.java    From InflatableDonkey with MIT License 4 votes vote down vote up
static InputStream decryptStream(InputStream in, XFileKey keyCipher) {
    BlockCipher cipher = keyCipher.ciphers().get();
    cipher.init(false, new KeyParameter(keyCipher.key()));
    return new CipherInputStream(in, new BufferedBlockCipher(cipher));
}
 
Example #10
Source File: AESEngineNoPadding.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream encryptStream(InputStream data, byte[] key, byte[] iv)
{
    init(key, iv);
    return new CipherInputStream(data, cipher);
}
 
Example #11
Source File: ARC4Engine.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream encryptStream(InputStream data, byte[] key)
{
    init(key);
    return new CipherInputStream(data, cipher);
}
 
Example #12
Source File: EncryptionUtil.java    From DarkBot with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static InputStream decryptInputStream(InputStream inputStream, SecretKey key) {
	return new CipherInputStream(inputStream, createBlockCipher(key, false));
}