Encrypt and decrypt text

This post is about creating an android app to encrypt and decrypt text.

Step 1: Create a new project with name Encryption.

Step 2: Open res/layout/xml (or) main.xml and add following code. Here we add an EditText, two Buttons, and two TextViews.

 <LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical">
	<ScrollView
		android:id="@+id/vscroll1"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:padding="8dp">
		<LinearLayout
			android:id="@+id/linear1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="8dp"
			android:orientation="vertical">
			<EditText
				android:id="@+id/edittext1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:textSize="16sp"
				android:textColor="#000000"
				android:hint="Edit Text"
				android:textColorHint="#607D8B"/>
			<Button
				android:id="@+id/button1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="Encrypt"
				android:textSize="20sp"
				android:textColor="#000000"/>
			<TextView
				android:id="@+id/textview1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="TextView"
				android:textSize="16sp"
				android:textColor="#000000"/>
			<Button
				android:id="@+id/button2"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="Decrypt"
				android:textSize="18sp"
				android:textColor="#000000"/>
			<TextView
				android:id="@+id/textview2"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="TextView"
				android:textSize="16sp"
				android:textColor="#000000"/>
		</LinearLayout>
	</ScrollView>
</LinearLayout>

Step 3: Open app/src/main/java/package and open MainActivity.java. Add following code in it.

package com.my.newproject19;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.text.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;

public class MainActivity extends Activity {
	
	
	private ScrollView vscroll1;
	private LinearLayout linear1;
	private EditText edittext1;
	private Button button1;
	private TextView textview1;
	private Button button2;
	private TextView textview2;
	
	KeyGenerator keyGenerator;
	SecretKey secretKey;
	byte[] secretKeyen;
	String strSecretKey;
	byte[] IV = new byte[16];
	byte[] cipherText;
	
	@Override
	protected void onCreate(Bundle _savedInstanceState) {
		super.onCreate(_savedInstanceState);
		setContentView(R.layout.main);
		initialize(_savedInstanceState);
	}
	
	private void initialize(Bundle _savedInstanceState) {
		
		vscroll1 = (ScrollView) findViewById(R.id.vscroll1);
		linear1 = (LinearLayout) findViewById(R.id.linear1);
		edittext1 = (EditText) findViewById(R.id.edittext1);
		button1 = (Button) findViewById(R.id.button1);
		textview1 = (TextView) findViewById(R.id.textview1);
		button2 = (Button) findViewById(R.id.button2);
		textview2 = (TextView) findViewById(R.id.textview2);
		
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _view) {
				button2.setVisibility(View.VISIBLE);
				textview2.setVisibility(View.VISIBLE);
				try {
					keyGenerator = javax.crypto.KeyGenerator.getInstance("AES");
					keyGenerator.init(256);
					secretKey = keyGenerator.generateKey();
					secretKeyen=secretKey.getEncoded();
					cipherText = encrypt(edittext1.getText().toString().getBytes(), secretKey, IV);
					textview1.setText(android.util.Base64.encodeToString(cipherText, android.util.Base64.DEFAULT));
				} catch (NoSuchAlgorithmException e){
					showMessage(e.toString());
				} catch (Exception e){
					showMessage(e.toString());
				}
			}
		});
		
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _view) {
				try {
					SecretKey originalSecretKey = new SecretKeySpec(secretKeyen, 0, secretKeyen.length, "AES");
					String decryptedText = decrypt(cipherText, originalSecretKey, IV); textview2.setText(decryptedText);
				} catch (Exception e) {
					showMessage(e.toString());
				}
			}
		});
		
		button2.setVisibility(View.GONE);
		textview2.setVisibility(View.GONE);
	}
	
	public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception{
		Cipher cipher = Cipher.getInstance("AES");
		SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(IV);
		cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
		byte[] cipherText = cipher.doFinal(plaintext);
		return cipherText;
	}
	
	public static String decrypt(byte[] cipherText, SecretKey key, byte[] IV) throws Exception{
			Cipher cipher = Cipher.getInstance("AES");
			SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
			IvParameterSpec ivSpec = new IvParameterSpec(IV);
			cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
			byte[] decryptedText = cipher.doFinal(cipherText);
			return new String(decryptedText);
	}
	
	public void showMessage(String _s) {
		Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
	}

}

Output:
Now run the app. Enter text in EditText field and click on encrypt button. The textview1 will display the encrypted text. Now click on decrypt button. The textview2 will show the decrypted text, which is same as text in EditText field.

Leave a Reply