/** ______ _____ _ | ____| / ____| | | | |__ _ __ | | _ __ _ _ _ __ | |_ ___ _ __ | __| | '_ \| | | '__| | | | '_ \| __/ _ \| '__| | |____| | | | |____| | | |_| | |_) | || (_) | | |______|_| |_|\_____|_| \__, | .__/ \__\___/|_| __/ | | |___/|_| */ class EnCryptor { private static final String TRANSFORMATION = "AES/GCM/NoPadding"; private static final String ANDROID_KEY_STORE = "AndroidKeyStore"; private byte[] encryption; private byte[] iv; EnCryptor() { } byte[] encryptText(final String alias, final String textToEncrypt) throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidAlgorithmParameterException, SignatureException, BadPaddingException, IllegalBlockSizeException { final Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(alias)); iv = cipher.getIV(); return (encryption = cipher.doFinal(textToEncrypt.getBytes("UTF-8"))); } @NonNull private SecretKey getSecretKey(final String alias) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { final KeyGenerator keyGenerator = KeyGenerator .getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE); keyGenerator.init(new KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_GCM) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) .build()); return keyGenerator.generateKey(); } byte[] getEncryption() { return encryption; } byte[] getIv() { return iv; } }