javax.crypto.Cipher.updateAAD()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(265)

本文整理了Java中javax.crypto.Cipher.updateAAD()方法的一些代码示例,展示了Cipher.updateAAD()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cipher.updateAAD()方法的具体详情如下:
包路径:javax.crypto.Cipher
类名称:Cipher
方法名:updateAAD

Cipher.updateAAD介绍

[英]Continues a multi-part transformation (encryption or decryption) with Authenticated Additional Data (AAD). AAD may only be added after the Cipher is initialized and before any data is passed to the instance.

This is only usable with cipher modes that support Authenticated Encryption with Additional Data (AEAD) such as Galois/Counter Mode (GCM).
[中]继续使用经过身份验证的附加数据(AAD)进行多部分转换(加密或解密)。AAD只能在密码初始化之后和任何数据传递到实例之前添加。
这仅适用于支持附加数据认证加密(AEAD)的密码模式,如Galois/计数器模式(GCM)。

代码示例

代码示例来源:origin: org.elasticsearch/elasticsearch

private Cipher createCipher(int opmode, char[] password, byte[] salt, byte[] iv) throws GeneralSecurityException {
  PBEKeySpec keySpec = new PBEKeySpec(password, salt, KDF_ITERS, CIPHER_KEY_BITS);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KDF_ALGO);
  SecretKey secretKey = keyFactory.generateSecret(keySpec);
  SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGO);
  GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_BITS, iv);
  Cipher cipher = Cipher.getInstance(CIPHER_ALGO + "/" + CIPHER_MODE + "/" + CIPHER_PADDING);
  cipher.init(opmode, secret, spec);
  cipher.updateAAD(salt);
  return cipher;
}

代码示例来源:origin: org.bitbucket.b_c/jose4j

private void updateAad(Cipher cipher, byte[] aad)
{
  if (aad != null && aad.length > 0)
  {
    cipher.updateAAD(aad);
  }
}

代码示例来源:origin: org.xipki/security

@Override
public void write(byte[] bytes, int off, int len) throws IOException {
 cipher.updateAAD(bytes, off, len);
}

代码示例来源:origin: org.xipki.tk/security

@Override
public void write(byte[] bytes) throws IOException {
  cipher.updateAAD(bytes);
}

代码示例来源:origin: org.xipki/security

@Override
public void write(int bb) throws IOException {
 cipher.updateAAD(new byte[]{(byte) bb});
}

代码示例来源:origin: org.xipki/security

@Override
public void write(byte[] bytes) throws IOException {
 cipher.updateAAD(bytes);
}

代码示例来源:origin: org.xipki.tk/security

@Override
public void write(byte[] bytes, int off, int len) throws IOException {
  cipher.updateAAD(bytes, off, len);
}

代码示例来源:origin: org.xipki.tk/security

@Override
public void write(int bb) throws IOException {
  cipher.updateAAD(new byte[]{(byte) bb});
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

private Cipher createCipher(int opmode, char[] password, byte[] salt, byte[] iv) throws GeneralSecurityException {
  PBEKeySpec keySpec = new PBEKeySpec(password, salt, KDF_ITERS, CIPHER_KEY_BITS);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KDF_ALGO);
  SecretKey secretKey = keyFactory.generateSecret(keySpec);
  SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGO);
  GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_BITS, iv);
  Cipher cipher = Cipher.getInstance(CIPHER_ALGO + "/" + CIPHER_MODE + "/" + CIPHER_PADDING);
  cipher.init(opmode, secret, spec);
  cipher.updateAAD(salt);
  return cipher;
}

代码示例来源:origin: io.grpc/grpc-alts

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);
}

代码示例来源:origin: io.grpc/grpc-alts

private void decryptAad(
  ByteBuffer plaintext, ByteBuffer ciphertext, @Nullable ByteBuffer aad, byte[] nonce)
  throws GeneralSecurityException {
 checkArgument(nonce.length == NONCE_LENGTH);
 cipher.init(
   Cipher.DECRYPT_MODE,
   new SecretKeySpec(this.key, AES),
   new GCMParameterSpec(TAG_LENGTH * 8, nonce));
 if (aad != null) {
  cipher.updateAAD(aad);
 }
 cipher.doFinal(ciphertext, plaintext);
}

代码示例来源:origin: apache/servicemix-bundles

private Cipher createCipher(int opmode, char[] password, byte[] salt, byte[] iv) throws GeneralSecurityException {
  PBEKeySpec keySpec = new PBEKeySpec(password, salt, KDF_ITERS, CIPHER_KEY_BITS);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KDF_ALGO);
  SecretKey secretKey = keyFactory.generateSecret(keySpec);
  SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGO);
  GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_BITS, iv);
  Cipher cipher = Cipher.getInstance(CIPHER_ALGO + "/" + CIPHER_MODE + "/" + CIPHER_PADDING);
  cipher.init(opmode, secret, spec);
  cipher.updateAAD(salt);
  return cipher;
}

代码示例来源:origin: Quicksign/kafka-encryption

@Override
  public byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_SPEC);
    ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
    byte[] iv = new byte[IV_SIZE];
    byteBuffer.get(iv);
    GCMParameterSpec gcmParamSpec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
    Cipher cipher = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParamSpec, secureRandom);
    cipher.updateAAD(TAG.getBytes(StandardCharsets.UTF_8));

    byte[] encryptedPayload = new byte[byteBuffer.remaining()];
    byteBuffer.get(encryptedPayload);

    return cipher.doFinal(encryptedPayload);
  }
}

代码示例来源:origin: Quicksign/kafka-encryption

@Override
  public byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_SPEC);
    ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
    byte[] iv = new byte[IV_SIZE];
    byteBuffer.get(iv);
    GCMParameterSpec gcmParamSpec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
    Cipher cipher = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParamSpec, secureRandom);
    cipher.updateAAD(TAG.getBytes(StandardCharsets.UTF_8));

    byte[] encryptedPayload = new byte[byteBuffer.remaining()];
    byteBuffer.get(encryptedPayload);

    return cipher.doFinal(encryptedPayload);
  }
}

代码示例来源:origin: Quicksign/kafka-encryption

@Override
  public byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_SPEC);
    ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
    byte[] iv = new byte[IV_SIZE];
    byteBuffer.get(iv);
    GCMParameterSpec gcmParamSpec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
    Cipher cipher = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParamSpec, secureRandom);
    cipher.updateAAD(TAG.getBytes(StandardCharsets.UTF_8));

    byte[] encryptedPayload = new byte[byteBuffer.remaining()];
    byteBuffer.get(encryptedPayload);

    return cipher.doFinal(encryptedPayload);
  }
}

代码示例来源:origin: Quicksign/kafka-encryption

@Override
public byte[] encrypt(byte[] data, byte[] key) throws Exception {
  SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_SPEC);
  byte[] iv = new byte[IV_SIZE];
  secureRandom.nextBytes(iv);
  GCMParameterSpec gcmParamSpec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
  Cipher cipher = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
  cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParamSpec, secureRandom);
  cipher.updateAAD(TAG.getBytes(StandardCharsets.UTF_8));
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  baos.write(iv);
  try (CipherOutputStream cipherOutputStream = new CipherOutputStream(baos, cipher)) {
    cipherOutputStream.write(data);
  }
  return baos.toByteArray();
}

代码示例来源:origin: aws/aws-encryption-sdk-java

@Override
  protected Cipher buildUnwrappingCipher(final Key key, final byte[] extraInfo, final int offset,
      final Map<String, String> encryptionContext) throws GeneralSecurityException {
    final GCMParameterSpec spec = bytesToSpec(extraInfo, offset);
    final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    cipher.init(Cipher.DECRYPT_MODE, key, spec);
    final byte[] aad = EncryptionContextSerializer.serialize(encryptionContext);
    cipher.updateAAD(aad);
    return cipher;
  }
}

代码示例来源:origin: aws/aws-encryption-sdk-java

@Override
protected WrappingData buildWrappingCipher(final Key key, final Map<String, String> encryptionContext)
    throws GeneralSecurityException {
  final byte[] nonce = new byte[NONCE_LENGTH];
  rnd.nextBytes(nonce);
  final GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH, nonce);
  final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  cipher.init(Cipher.ENCRYPT_MODE, key, spec);
  final byte[] aad = EncryptionContextSerializer.serialize(encryptionContext);
  cipher.updateAAD(aad);
  return new WrappingData(cipher, specToBytes(spec));
}

代码示例来源:origin: RUB-NDS/TLS-Attacker

@Override
public byte[] encrypt(byte[] iv, int tagLength, byte[] additionAuthenticatedData, byte[] someBytes)
    throws CryptoException {
  GCMParameterSpec encryptIv = new GCMParameterSpec(tagLength, iv);
  try {
    cipher = Cipher.getInstance(algorithm.getJavaName());
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, algorithm.getJavaName()), encryptIv);
    cipher.updateAAD(additionAuthenticatedData);
    byte[] result = cipher.doFinal(someBytes);
    this.iv = cipher.getIV();
    return result;
  } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException
      | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchPaddingException ex) {
    throw new CryptoException("Could not initialize JavaCipher", ex);
  }
}

代码示例来源:origin: neuhalje/bouncy-gpg

public byte[] aesGCM(byte[] nonce, byte[] keyBytes)
  throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
 GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH_BITS, nonce);
 final SecretKey secretKeySpec = new SecretKeySpec(keyBytes, "AES");
 final Cipher aes = Cipher.getInstance("AES/GCM/NoPadding");
 aes.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec);
 aes.updateAAD(AUTHENTICATED_NOT_ENCRYPTED.getBytes(StandardCharsets.UTF_8));
 aes.update(ENCRYPTED_AND_AUTHENTICATED.getBytes(StandardCharsets.UTF_8));
 final byte[] cipherText = aes.doFinal();
 return cipherText;
}

相关文章