use of javax.crypto.spec.GCMParameterSpec in project grpc-java by grpc.
the class AesGcmAeadCrypter method encryptAad.
private int encryptAad(ByteBuffer ciphertext, ByteBuffer plaintext, @Nullable ByteBuffer aad, byte[] nonce) throws GeneralSecurityException {
checkArgument(nonce.length == NONCE_LENGTH);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(this.key, AES), new GCMParameterSpec(TAG_LENGTH * 8, nonce));
if (aad != null) {
cipher.updateAAD(aad);
}
return cipher.doFinal(plaintext, ciphertext);
}
use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.
the class GCMParameterSpecTest method testGetIV_Subarray_Success.
public void testGetIV_Subarray_Success() throws Exception {
GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV, 2, 4);
assertEquals(Arrays.toString(Arrays.copyOfRange(TEST_IV, 2, 6)), Arrays.toString(spec.getIV()));
}
use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.
the class GCMParameterSpecTest method testGetTLen_Success.
public void testGetTLen_Success() throws Exception {
GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
assertEquals(8, spec.getTLen());
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class SignalStorageCipher method encrypt.
public static byte[] encrypt(StorageCipherKey key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
byte[] iv = Util.getSecretBytes(IV_LENGTH);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, iv));
byte[] ciphertext = cipher.doFinal(data);
return Util.join(iv, ciphertext);
} catch (NoSuchAlgorithmException | java.security.InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class AESCipher method decrypt.
static byte[] decrypt(byte[] key, byte[] iv, byte[] ciphertext, byte[] tag) throws InvalidCiphertextException {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, iv));
return cipher.doFinal(ByteUtil.combine(ciphertext, tag));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException e) {
throw new AssertionError(e);
} catch (InvalidKeyException | BadPaddingException e) {
throw new InvalidCiphertextException(e);
}
}
Aggregations