Search in sources :

Example 1 with GCMBlockCipher

use of org.spongycastle.crypto.modes.GCMBlockCipher in project conceal by facebook.

the class BouncyCastleHelper method bouncyCastleEncrypt.

public static Result bouncyCastleEncrypt(byte[] data, byte[] key, byte[] iv, byte[] aadData) throws UnsupportedEncodingException, InvalidCipherTextException {
    GCMBlockCipher gcm = new GCMBlockCipher(new AESEngine());
    byte[] gcmOut = new byte[CryptoTestUtils.NUM_DATA_BYTES + CryptoConfig.KEY_128.tagLength];
    KeyParameter keyParameter = new KeyParameter(key);
    // Add aad data.
    AEADParameters params = new AEADParameters(keyParameter, CryptoConfig.KEY_128.tagLength * 8, iv, aadData);
    // Init encryption.
    gcm.init(true, params);
    int written = gcm.processBytes(data, 0, data.length, gcmOut, 0);
    written += gcm.doFinal(gcmOut, written);
    byte[] bouncyCastleOut = Arrays.copyOfRange(gcmOut, 0, written);
    byte[] cipherText = Arrays.copyOfRange(bouncyCastleOut, 0, CryptoTestUtils.NUM_DATA_BYTES);
    byte[] tag = Arrays.copyOfRange(bouncyCastleOut, CryptoTestUtils.NUM_DATA_BYTES, bouncyCastleOut.length);
    return new Result(cipherText, tag);
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) AEADParameters(org.spongycastle.crypto.params.AEADParameters) KeyParameter(org.spongycastle.crypto.params.KeyParameter) GCMBlockCipher(org.spongycastle.crypto.modes.GCMBlockCipher)

Aggregations

AESEngine (org.spongycastle.crypto.engines.AESEngine)1 GCMBlockCipher (org.spongycastle.crypto.modes.GCMBlockCipher)1 AEADParameters (org.spongycastle.crypto.params.AEADParameters)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1