use of org.spongycastle.crypto.engines.AESEngine 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);
}
Aggregations