private void attemptEncrypt() { mPlaintextView.setError(null); mCiphertextView.setError(null); mCiphertextView.setText(""); try { byte[] plaintext = mPlaintextView.getText().toString().getBytes("UTF-8"); byte[] ciphertext = mApplication.aead.encrypt(plaintext, EMPTY_ASSOCIATED_DATA); mCiphertextView.setText(base64Encode(ciphertext)); } catch (UnsupportedEncodingException | GeneralSecurityException | IllegalArgumentException e) { mCiphertextView.setError( String.format("%s: %s", getString(R.string.error_cannot_encrypt), e.toString())); mPlaintextView.requestFocus(); } }
private byte[] encrypt(KeysetHandle keysetHandle) throws GeneralSecurityException { Aead aead = AeadFactory.getPrimitive(keysetHandle); return aead.encrypt(INITIAL_TEXT.getBytes(), ASSOCIATED_DATA.getBytes()); }
private byte[] encrypt(KeysetHandle keysetHandle) throws GeneralSecurityException { Aead aead = AeadFactory.getPrimitive(keysetHandle); return aead.encrypt(INITIAL_TEXT.getBytes(), ASSOCIATED_DATA.getBytes()); }
@Override public byte[] encrypt(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { // Generate a new DEK. byte[] dek = Registry.newKey(dekTemplate).toByteArray(); // Wrap it with remote. byte[] encryptedDek = remote.encrypt(dek, EMPTY_AAD); // Use DEK to encrypt plaintext. Aead aead = Registry.getPrimitive(dekTemplate.getTypeUrl(), dek); byte[] payload = aead.encrypt(plaintext, associatedData); // Build ciphertext protobuf and return result. return buildCiphertext(encryptedDek, payload); }
/** * Encrypts {@code plaintext} using {@code contextInfo} as <b>info</b>-parameter of the underlying * HKDF. * * @return resulting ciphertext. */ @Override public byte[] encrypt(final byte[] plaintext, final byte[] contextInfo) throws GeneralSecurityException { EciesHkdfSenderKem.KemKey kemKey = senderKem.generateKey( hkdfHmacAlgo, hkdfSalt, contextInfo, demHelper.getSymmetricKeySizeInBytes(), ecPointFormat); Aead aead = demHelper.getAead(kemKey.getSymmetricKey()); byte[] ciphertext = aead.encrypt(plaintext, EMPTY_AAD); byte[] header = kemKey.getKemBytes(); return ByteBuffer.allocate(header.length + ciphertext.length) .put(header) .put(ciphertext) .array(); } }
byte[] payloadCiphertext = aead.encrypt(plaintext, emptyEad);
byte[] payloadCiphertext = aead.encrypt(plaintext, emptyEad);
@Override public byte[] encrypt(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { return Bytes.concat( pset.getPrimary().getIdentifier(), pset.getPrimary().getPrimitive().encrypt(plaintext, associatedData)); }
/** Encrypts the keyset with the {@link Aead} master key. */ private static EncryptedKeyset encrypt(Keyset keyset, Aead masterKey) throws GeneralSecurityException { byte[] encryptedKeyset = masterKey.encrypt(keyset.toByteArray(), /* associatedData= */ new byte[0]); // Check if we can decrypt, to detect errors try { final Keyset keyset2 = Keyset.parseFrom(masterKey.decrypt(encryptedKeyset, /* associatedData= */ new byte[0])); if (!keyset2.equals(keyset)) { throw new GeneralSecurityException("cannot encrypt keyset"); } } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException("invalid keyset, corrupted key material"); } return EncryptedKeyset.newBuilder() .setEncryptedKeyset(ByteString.copyFrom(encryptedKeyset)) .setKeysetInfo(Util.getKeysetInfo(keyset)) .build(); }