Android Studio : String encryption and decryption implementation

String encryption and decryption implementation in android studio

Hello Guys, in this article I will be implementing android app which will encrypt or decrypt string. Encryption and decryption is very important for security. Now a days security is main priority of every system, without good security no body can trust your application or product. So encryption or decryption is one of best way to secure your data. I will be using AES algorithm or technique for text (String) encryption or decryption. Below is the image of Android app.

 

Pratical Implementation

 

I will be explaining thing in simple word or straight forward. So first I will be sharing code with you. This is template code you must use it without doing any changes

AESUtils.java

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtils
{
private static final byte[] keyValue =
new byte[]{‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’, ‘a’, ‘f’, ‘f’, ‘a’, ‘i’, ‘r’, ‘s’, ‘c’, ‘o’, ‘m’};

public static String encrypt(String cleartext)
throws Exception {
byte[] rawKey = getRawKey();
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String encrypted)
throws Exception {
byte[] enc = toByte(encrypted);
byte[] result = decrypt(enc);
return new String(result);
}
private static byte[] getRawKey() throws Exception {
SecretKey key = new SecretKeySpec(keyValue, “AES”);
byte[] raw = key.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKey skeySpec = new SecretKeySpec(raw, “AES”);
Cipher cipher = Cipher.getInstance(“AES”);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] encrypted)
throws Exception {
SecretKey skeySpec = new SecretKeySpec(keyValue, “AES”);
Cipher cipher = Cipher.getInstance(“AES”);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return “”;
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = “0123456789ABCDEF”;
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}

 

Lets share code of design and main activity, you can change it according to your needs.


activity_main.xml

 <?xml version=“1.0” encoding=“utf-8”?><android.support.constraint.ConstraintLayout 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” tools:context=“.MainActivity”>
<EditText android:id=“@+id/decrypt_text” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:layout_marginTop=“32dp” android:ems=“10”android:hint=“Enter Text to Decrypt” android:inputType=“textPersonName”app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/divider” />
<EditText android:id=“@+id/decrypt_result” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:layout_marginTop=“16dp” android:ems=“10”android:hint=“Decrypt Result” android:inputType=“textPersonName”app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/decrypt_text” />
<Button android:id=“@+id/decrypt” android:layout_width=“213dp”android:layout_height=“wrap_content” android:layout_marginTop=“16dp”android:text=“Decrypt Text” app:layout_constraintEnd_toEndOf=“parent”app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/decrypt_result” />
<TextView android:id=“@+id/textView” android:layout_width=“0dp”android:layout_height=“wrap_content” android:layout_marginLeft=“24dp”android:layout_marginTop=“24dp” android:layout_marginRight=“24dp”android:gravity=“center” android:text=“Text Encryption and decryption by coder vlog”android:textSize=“24sp” app:layout_constraintLeft_toLeftOf=“parent”app:layout_constraintRight_toRightOf=“parent” app:layout_constraintTop_toTopOf=“parent” />
<EditText android:id=“@+id/encrypt_text” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:layout_marginTop=“60dp” android:ems=“10”android:hint=“Enter Text to Encrypt” android:inputType=“textPersonName”app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintHorizontal_bias=“0.5”app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/textView” />
<EditText android:id=“@+id/encrypt_result” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:layout_marginTop=“16dp” android:ems=“10”android:hint=“Encrypt Result” android:inputType=“textPersonName”app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintHorizontal_bias=“0.5”app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/encrypt_text” />
<Button android:id=“@+id/encrypt” android:layout_width=“213dp”android:layout_height=“wrap_content” android:layout_marginTop=“16dp”android:text=“Encrypt Text” app:layout_constraintEnd_toEndOf=“parent”app:layout_constraintHorizontal_bias=“0.5” app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/encrypt_result” />
<View android:id=“@+id/divider” android:layout_width=“395dp” android:layout_height=“2dp”android:layout_marginStart=“8dp” android:layout_marginLeft=“8dp”android:layout_marginTop=“24dp” android:layout_marginEnd=“8dp”android:layout_marginRight=“8dp” android:background=“?android:attr/listDivider”app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintStart_toStartOf=“parent”app:layout_constraintTop_toBottomOf=“@+id/encrypt” />
</android.support.constraint.ConstraintLayout>

 

MainActivity.java

 

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText encrypt_text, encrypt_result , decrypt_text, decrypt_result;
Button encrypt , decrypt;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

encrypt_text = ( EditText ) findViewById( R.id.encrypt_text );
encrypt_result = ( EditText ) findViewById( R.id.encrypt_result );
decrypt_text = ( EditText ) findViewById( R.id.decrypt_text );
decrypt_result = ( EditText ) findViewById( R.id.decrypt_result );

encrypt = ( Button ) findViewById( R.id.encrypt );
decrypt = ( Button ) findViewById( R.id.decrypt );

encrypt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if( encrypt_text.getText().toString().isEmpty() ){
Toast.makeText(MainActivity.this,”Please fill input field”,Toast.LENGTH_SHORT).show();

}
else{

String encrypted = “”;
try {
encrypted = AESUtils.encrypt( encrypt_text.getText().toString() );
encrypt_result.setText( encrypted );
//Log.d(“TEST”, “encrypted:” + encrypted); } catch (Exception e) {
e.printStackTrace();
}

}
}
});

decrypt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if( decrypt_text.getText().toString().isEmpty() ){
Toast.makeText(MainActivity.this,”Please fill input field”,Toast.LENGTH_SHORT).show();
}
else{
String decrypted = “”;
try {
decrypted = AESUtils.decrypt( decrypt_text.getText().toString() );
//Log.d(“TEST”, “decrypted:” + decrypted); decrypt_result.setText( decrypted );
}
catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}

 

Now with the help of one line of code you encrypt or decrypt string in android and that is AESUtils.encrypt ( any string to encrypt )  or  AESUtils.decrypt( any string to decrypt ). Your app is ready, now enjoyed it. Thanks