Skip to content

Instantly share code, notes, and snippets.

@TechNov
Forked from piyush-malaviya/DeCryptor.java
Created July 7, 2021 14:36

Revisions

  1. Josias Sena revised this gist Feb 12, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions SampleUsage.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    /**
    _____ _ _ _
    / ____| | | | | | |
    | (___ __ _ _ __ ___ _ __ | | ___ | | | |___ __ _ __ _ ___
    \___ \ / _` | '_ ` _ \| '_ \| |/ _ \ | | | / __|/ _` |/ _` |/ _ \
    ____) | (_| | | | | | | |_) | | __/ | |__| \__ \ (_| | (_| | __/
    |_____/ \__,_|_| |_| |_| .__/|_|\___| \____/|___/\__,_|\__, |\___|
    | | __/ |
    |_| |___/
    */
    public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
  2. Josias Sena created this gist Feb 12, 2017.
    45 changes: 45 additions & 0 deletions DeCryptor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    /**
    _____ _____ _
    | __ \ / ____| | |
    | | | | ___| | _ __ _ _ _ __ | |_ ___ _ __
    | | | |/ _ \ | | '__| | | | '_ \| __/ _ \| '__|
    | |__| | __/ |____| | | |_| | |_) | || (_) | |
    |_____/ \___|\_____|_| \__, | .__/ \__\___/|_|
    __/ | |
    |___/|_|
    */
    class DeCryptor {

    private static final String TRANSFORMATION = "AES/GCM/NoPadding";
    private static final String ANDROID_KEY_STORE = "AndroidKeyStore";

    private KeyStore keyStore;

    DeCryptor() throws CertificateException, NoSuchAlgorithmException, KeyStoreException,
    IOException {
    initKeyStore();
    }

    private void initKeyStore() throws KeyStoreException, CertificateException,
    NoSuchAlgorithmException, IOException {
    keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
    keyStore.load(null);
    }

    String decryptData(final String alias, final byte[] encryptedData, final byte[] encryptionIv)
    throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException,
    NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException,
    BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {

    final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    final GCMParameterSpec spec = new GCMParameterSpec(128, encryptionIv);
    cipher.init(Cipher.DECRYPT_MODE, getSecretKey(alias), spec);

    return new String(cipher.doFinal(encryptedData), "UTF-8");
    }

    private SecretKey getSecretKey(final String alias) throws NoSuchAlgorithmException,
    UnrecoverableEntryException, KeyStoreException {
    return ((KeyStore.SecretKeyEntry) keyStore.getEntry(alias, null)).getSecretKey();
    }
    }
    60 changes: 60 additions & 0 deletions EnCryptor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@

    /**
    ______ _____ _
    | ____| / ____| | |
    | |__ _ __ | | _ __ _ _ _ __ | |_ ___ _ __
    | __| | '_ \| | | '__| | | | '_ \| __/ _ \| '__|
    | |____| | | | |____| | | |_| | |_) | || (_) | |
    |______|_| |_|\_____|_| \__, | .__/ \__\___/|_|
    __/ | |
    |___/|_|
    */
    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;
    }
    }
    80 changes: 80 additions & 0 deletions SampleUsage.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String SAMPLE_ALIAS = "MYALIAS";

    @BindView (R.id.toolbar)
    Toolbar toolbar;

    @BindView (R.id.ed_text_to_encrypt)
    EditText edTextToEncrypt;

    @BindView (R.id.tv_encrypted_text)
    TextView tvEncryptedText;

    @BindView (R.id.tv_decrypted_text)
    TextView tvDecryptedText;

    private EnCryptor encryptor;
    private DeCryptor decryptor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);

    encryptor = new EnCryptor();

    try {
    decryptor = new DeCryptor();
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException |
    IOException e) {
    e.printStackTrace();
    }
    }

    @OnClick ({R.id.btn_encrypt, R.id.btn_decrypt})
    public void onClick(final View view) {

    final int id = view.getId();

    switch (id) {
    case R.id.btn_encrypt:
    encryptText();
    break;
    case R.id.btn_decrypt:
    decryptText();
    break;
    }
    }

    private void decryptText() {
    try {
    tvDecryptedText.setText(decryptor
    .decryptData(SAMPLE_ALIAS, encryptor.getEncryption(), encryptor.getIv()));
    } catch (UnrecoverableEntryException | NoSuchAlgorithmException |
    KeyStoreException | NoSuchPaddingException | NoSuchProviderException |
    IOException | InvalidKeyException e) {
    Log.e(TAG, "decryptData() called with: " + e.getMessage(), e);
    } catch (IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
    e.printStackTrace();
    }
    }

    private void encryptText() {

    try {
    final byte[] encryptedText = encryptor
    .encryptText(SAMPLE_ALIAS, edTextToEncrypt.getText().toString());
    tvEncryptedText.setText(Base64.encodeToString(encryptedText, Base64.DEFAULT));
    } catch (UnrecoverableEntryException | NoSuchAlgorithmException | NoSuchProviderException |
    KeyStoreException | IOException | NoSuchPaddingException | InvalidKeyException e) {
    Log.e(TAG, "onClick() called with: " + e.getMessage(), e);
    } catch (InvalidAlgorithmParameterException | SignatureException |
    IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
    }
    }
    }