org.bouncycastle.crypto.modes.AEADBlockCipher Java Examples

The following examples show how to use org.bouncycastle.crypto.modes.AEADBlockCipher. 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: 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 #2
Source File: AESGCMBytesEncryptor.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
private byte[] process(AEADBlockCipher blockCipher, byte[] in) {
    byte[] buf = new byte[blockCipher.getOutputSize(in.length)];
    int bytesWritten = blockCipher.processBytes(in, 0, in.length, buf, 0);
    try {
        bytesWritten += blockCipher.doFinal(buf, bytesWritten);
    } catch (InvalidCipherTextException e) {
        throw new IllegalStateException("unable to encrypt/decrypt", e);
    }
    if (bytesWritten == buf.length) {
        return buf;
    }
    byte[] out = new byte[bytesWritten];
    System.arraycopy(buf, 0, out, 0, bytesWritten);
    return out;
}
 
Example #3
Source File: AesGcmCrypt.java    From shadowsocks-java with MIT License 5 votes vote down vote up
@Override
protected AEADBlockCipher getCipher(boolean isEncrypted)
        throws GeneralSecurityException {
    switch (_name) {
        case CIPHER_AEAD_128_GCM:
        case CIPHER_AEAD_256_GCM:
            return new GCMBlockCipher(new AESEngine());
        default:
            throw new InvalidAlgorithmParameterException(_name);
    }
}
 
Example #4
Source File: CipherWriteStreamValidation.java    From sfs with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.
 */
public CipherInputStream(InputStream is, AEADBlockCipher cipher) {
    super(is);

    this.aeadBlockCipher = cipher;

    int outSize = cipher.getOutputSize(INPUT_BUF_SIZE);

    buf = new byte[(outSize > INPUT_BUF_SIZE) ? outSize : INPUT_BUF_SIZE];
    inBuf = new byte[INPUT_BUF_SIZE];
}
 
Example #5
Source File: Downloader.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example #6
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 #7
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 #8
Source File: AESGCMEncryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(randomKeyBytes), 128, randomIvBytes));
    return cipherData(cipher, data);
}
 
Example #9
Source File: AESGCMEncryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static byte[] cipherData(AEADBlockCipher cipher, byte[] data) throws IllegalStateException, InvalidCipherTextException {
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    if (actualLength == minSize) {
        return outBuf;
    } else {
        return Arrays.copyOf(outBuf, actualLength);
    }
}
 
Example #10
Source File: AESGCMEncryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] decrypt(byte[] data, byte[] randomKey)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException,
        IllegalStateException, InvalidCipherTextException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(randomKey), 128, randomIvBytes));
    return cipherData(cipher, data);
}
 
Example #11
Source File: CryptAeadBase.java    From shadowsocks-java with MIT License 4 votes vote down vote up
protected abstract AEADBlockCipher getCipher(boolean isEncrypted)
throws GeneralSecurityException;
 
Example #12
Source File: CipherEndableWriteStream.java    From sfs with Apache License 2.0 4 votes vote down vote up
public CipherEndableWriteStream(BufferEndableWriteStream delegate, AEADBlockCipher cipher) {
    this.delegate = delegate;
    this.bufferEndableWriteStreamOutputStream = new BufferEndableWriteStreamOutputStream(delegate);
    this.outputStream = new CipherOutputStream(bufferEndableWriteStreamOutputStream, cipher);
}
 
Example #13
Source File: CipherReadStream.java    From sfs with Apache License 2.0 4 votes vote down vote up
public CipherReadStream(ReadStream<Buffer> delegate, AEADBlockCipher aeadBlockCipher) {
    this.delegate = delegate;
    this.readStreamDataHandlerOutputStream = new ReadStreamDataHandlerOutputStream(dataHandler);
    this.cipherOutputStream = new CipherOutputStream(readStreamDataHandlerOutputStream, aeadBlockCipher);
}
 
Example #14
Source File: CipherWriteStreamValidation.java    From sfs with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a CipherOutputStream from an OutputStream and a AEADBlockCipher.
 */
public CipherOutputStream(OutputStream os, AEADBlockCipher cipher) {
    super(os);
    this.aeadBlockCipher = cipher;
}
 
Example #15
Source File: Keymaster1Blob.java    From keystore-decryptor with Apache License 2.0 4 votes vote down vote up
private static AEADBlockCipher createOCBCipher(boolean forEncryption, AEADParameters parameters) {
    AEADBlockCipher c = new OCBBlockCipher(new AESEngine(), new AESEngine());
    c.init(forEncryption, parameters);

    return c;
}