@Override public final void onCreate() { super.onCreate(); try { Config.register(TinkConfig.TINK_1_0_0); aead = getOrGenerateNewKeysetHandle().getPrimitive(Aead.class); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
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 void attemptDecrypt() { mPlaintextView.setError(null); mPlaintextView.setText(""); mCiphertextView.setError(null); try { byte[] ciphertext = base64Decode(mCiphertextView.getText().toString()); byte[] plaintext = mApplication.aead.decrypt(ciphertext, EMPTY_ASSOCIATED_DATA); mPlaintextView.setText(new String(plaintext, "UTF-8")); } catch (UnsupportedEncodingException | GeneralSecurityException | IllegalArgumentException e) { mPlaintextView.setError( String.format("%s: %s", getString(R.string.error_cannot_decrypt), e.toString())); mCiphertextView.requestFocus(); } }
private static void generateEcdsaKeyPair(File publicKeyFile, File privatekeyFile) throws GeneralSecurityException, IOException { KeysetHandle privateKeyHandle = KeysetHandle.generateNew(SIGNATURE_KEY_TEMPLATE); CleartextKeysetHandle.write(privateKeyHandle, BinaryKeysetWriter.withFile(privatekeyFile)); KeysetHandle publicKeyHandle = privateKeyHandle.getPublicKeysetHandle(); CleartextKeysetHandle.write(publicKeyHandle, BinaryKeysetWriter.withFile(publicKeyFile)); } }
@Override public byte[] encrypt(final byte[] plaintext, final byte[] contextInfo) throws GeneralSecurityException { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().encrypt(plaintext, contextInfo)); } };
@Override public byte[] encrypt(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { return Bytes.concat( pset.getPrimary().getIdentifier(), pset.getPrimary().getPrimitive().encrypt(plaintext, associatedData)); }
/** * Stores the private keyset in the projects resources/keysets directory if it does not exist yet. * * @throws IOException Failure during saving * @throws GeneralSecurityException Failure during keyset generation */ private void generateAndStorePrivateKey() throws IOException, GeneralSecurityException { KeysetHandle keysetHandle = KeysetHandle.generateNew(HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM); File keysetFile = new File(PRIVATE_KEYSET_FILENAME); if (!keysetFile.exists()) { CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(keysetFile)); } }
@Override public byte[] encryptDeterministically(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives .getPrimary() .getPrimitive() .encryptDeterministically(plaintext, associatedData)); }
/** * Initializes the Capillary library. * * <p>This should be called before using any functionality provided by Capillary. * * @throws GeneralSecurityException if the initialization fails. */ public static void initialize() throws GeneralSecurityException { com.google.crypto.tink.Config.register(SignatureConfig.TINK_1_1_0); com.google.crypto.tink.Config.register(AeadConfig.TINK_1_1_0); } }
/** * @return a new {@link KeysetHandle} that contains a single fresh key generated according to * {@code keyTemplate}. * @throws GeneralSecurityException */ public static final KeysetHandle generateNew(KeyTemplate keyTemplate) throws GeneralSecurityException { return KeysetManager.withEmptyKeyset().rotate(keyTemplate).getKeysetHandle(); }
@Override public WritableByteChannel newEncryptingChannel( WritableByteChannel ciphertextDestination, byte[] associatedData) throws GeneralSecurityException, IOException { return primitives.getPrimary().getPrimitive() .newEncryptingChannel(ciphertextDestination, associatedData); }
@Override public OutputStream newEncryptingStream( OutputStream ciphertext, byte[] associatedData) throws GeneralSecurityException, IOException { return primitives.getPrimary().getPrimitive() .newEncryptingStream(ciphertext, associatedData); } }
/** * @return a new keyset handle from a keyset obtained from {@code reader}. * @throws GeneralSecurityException */ public static final KeysetHandle read(KeysetReader reader) throws GeneralSecurityException, IOException { Keyset keyset = reader.read(); validate(keyset); return KeysetHandle.fromKeyset(keyset); }
/** * @return a new {@link KeysetHandle} from a {@link Keyset} read with {@code reader}. * @throws GeneralSecurityException */ public static KeysetHandle read(KeysetReader reader) throws GeneralSecurityException, IOException { return KeysetHandle.fromKeyset(reader.read()); }
/** Serializes, encrypts with {@code masterKey} and writes the keyset to {@code outputStream}. */ public void write(KeysetWriter keysetWriter, Aead masterKey) throws GeneralSecurityException, IOException { EncryptedKeyset encryptedKeyset = encrypt(keyset, masterKey); keysetWriter.write(encryptedKeyset); return; }
/** @return the entries with primitives identified by the ciphertext prefix of {@code key}. */ protected List<Entry<P>> getPrimitive(Keyset.Key key) throws GeneralSecurityException { return getPrimitive(CryptoFormat.getOutputPrefix(key)); }
/** * Convenience method for creating a new primitive for the key given in {@code proto}. * * <p>It looks up a {@link KeyManager} identified by {@code type_url}, and calls {@link * KeyManager#getPrimitive} with {@code serialized} as the parameter. * * @return a new primitive */ @SuppressWarnings("TypeParameterUnusedInFormals") public static <P> P getPrimitive(String typeUrl, ByteString serialized) throws GeneralSecurityException { KeyManager<P> manager = getKeyManager(typeUrl); return manager.getPrimitive(serialized); }
/** * @return a new {@link KeysetHandle} from a {@code keyset}. * @throws GeneralSecurityException */ static final KeysetHandle fromKeyset(Keyset keyset) throws GeneralSecurityException { assertEnoughKeyMaterial(keyset); return new KeysetHandle(keyset); }
private static void validate(final PrimitiveSet<HybridDecrypt> pset) throws GeneralSecurityException { for (Collection<PrimitiveSet.Entry<HybridDecrypt>> entries : pset.getAll()) { for (PrimitiveSet.Entry<HybridDecrypt> entry : entries) { if (!(entry.getPrimitive() instanceof HybridDecrypt)) { throw new GeneralSecurityException("invalid HybridDecrypt key material"); } } } } }