use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class AESCipher method encrypt.
static AESEncryptedResult encrypt(byte[] key, byte[] aad, byte[] requestData) {
try {
byte[] iv = Util.getSecretBytes(12);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, iv));
if (aad != null) {
cipher.updateAAD(aad);
}
byte[] cipherText = cipher.doFinal(requestData);
byte[][] parts = ByteUtil.split(cipherText, cipherText.length - TAG_LENGTH_BYTES, TAG_LENGTH_BYTES);
byte[] mac = parts[1];
byte[] data = parts[0];
return new AESEncryptedResult(iv, data, mac, aad);
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class ProfileCipher method decrypt.
/**
* Returns original data with padding still intact.
*/
public byte[] decrypt(byte[] input) throws InvalidCiphertextException {
try {
if (input.length < 12 + 16 + 1) {
throw new InvalidCiphertextException("Too short: " + input.length);
}
byte[] nonce = new byte[12];
System.arraycopy(input, 0, nonce, 0, nonce.length);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, nonce));
return cipher.doFinal(input, nonce.length, input.length - nonce.length);
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException e) {
throw new AssertionError(e);
} catch (InvalidKeyException | BadPaddingException e) {
throw new InvalidCiphertextException(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project cas by apereo.
the class BaseBinaryCipherExecutor method buildParameterSpec.
private AlgorithmParameterSpec buildParameterSpec(final int encryptionKeySize) {
val iv = new byte[encryptionSecretKey.length];
System.arraycopy(this.encryptionSecretKey, 0, iv, 0, encryptionSecretKey.length);
return encryptionKeySize <= MINIMUM_ENCRYPTION_KEY_LENGTH ? new IvParameterSpec(iv) : new GCMParameterSpec(GCM_TAG_LENGTH, iv);
}
use of javax.crypto.spec.GCMParameterSpec in project keywhiz by square.
the class ContentCryptographer method gcm.
private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
cipher.init(mode.cipherMode, derivedKey, gcmParameters);
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
throw Throwables.propagate(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project sonarqube by SonarSource.
the class AesGCMCipher method encrypt.
@Override
public String encrypt(String clearText) {
try {
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_ALGO);
byte[] iv = new byte[GCM_IV_LENGTH_IN_BYTES];
new SecureRandom().nextBytes(iv);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, loadSecretFile(), new GCMParameterSpec(GCM_TAG_LENGTH_IN_BITS, iv));
byte[] encryptedText = cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8.name()));
return Base64.encodeBase64String(ByteBuffer.allocate(GCM_IV_LENGTH_IN_BYTES + encryptedText.length).put(iv).put(encryptedText).array());
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
Aggregations