Encryption And Decryption example in android.

Just write the below code and execute program.

XML Code:-

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F3F3F3" android:gravity="center" android:orientation="vertical" android:padding="16dp" tools:context="com.amtechgcc.materialform.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="25dp" android:fontFamily="casual" android:gravity="center" android:padding="5dp" android:text="Encryption and Decryption Example \n by\n Ashique Aziz" android:textColor="@android:color/black" android:textSize="18sp" /> <EditText android:id="@+id/InputText" android:layout_width="match_parent" android:layout_height="46dp" android:layout_margin="3dp" android:background="@android:color/white" android:hint="Enter any text" android:inputType="textPersonName" android:padding="10dp" android:textColor="#474747" android:textSize="14sp" /> <TextView android:id="@+id/encode" android:layout_width="match_parent" android:layout_height="46dp" android:layout_margin="3dp" android:padding="5dp" android:text="" android:textColor="#474747" android:textSize="14sp" /> <TextView android:id="@+id/decode" android:layout_width="match_parent" android:layout_height="46dp" android:layout_margin="3dp" android:padding="5dp" android:text="" android:textColor="#474747" android:textSize="14sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnEncode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@android:color/holo_orange_light" android:padding="10dp" android:text="@string/encode" tools:ignore="ButtonStyle" /> <Button android:id="@+id/btnDecode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@android:color/holo_green_light" android:padding="10dp" android:text="@string/decode" tools:ignore="ButtonStyle" /> </LinearLayout> </LinearLayout>

Java Code:-


package com.amtechgcc.materialform; import android.annotation.SuppressLint; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; public class MainActivity extends Activity { SecretKeySpec sks = null; byte[] encodedBytes = null; byte[] decodedBytes = null; TextView text, encode, decode; Button enc, dec; String TestText; @SuppressLint("SetTextI18n") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = findViewById(R.id.InputText); encode = findViewById(R.id.encode); decode = findViewById(R.id.decode); enc = findViewById(R.id.btnEncode); dec = findViewById(R.id.btnDecode); //.setText("\n[ORIGINAL]:\n" + TestText + "\n"); // Set up secret key spec for 128-bit AES encryption and decryption try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed("any data used as random seed".getBytes()); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128, sr); sks = new SecretKeySpec((kg.generateKey()).getEncoded(), "AES"); } catch (Exception e) { Toast.makeText(this, "AES secret key spec error", Toast.LENGTH_SHORT).show(); } enc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Original text TestText = text.getText().toString(); // Encode the original data with AES try { @SuppressLint("GetInstance") Cipher c = Cipher.getInstance("AES"); c.init(Cipher.ENCRYPT_MODE, sks); encodedBytes = c.doFinal(TestText.getBytes()); } catch (Exception e) { Toast.makeText(MainActivity.this, "AES encryption error", Toast.LENGTH_SHORT).show(); } encode.setText("[ENCODED]:\n" + Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n"); } }); dec.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Original text TestText = text.getText().toString(); // Decode the encoded data with AES try { @SuppressLint("GetInstance") Cipher c = Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE, sks); decodedBytes = c.doFinal(encodedBytes); } catch (Exception e) { Toast.makeText(MainActivity.this, "AES decryption error", Toast.LENGTH_SHORT).show();
} if (decoagdedBytes != null) { decode.setText("[DECODED]:\n" + new String(decodedBytes) + "\n"); } } }); } }

Screenshot :

Tag:- Encryption, Decryption, secure text, change password in encrypt value,  username password encryption.

Comments