Skip to content
Related Articles

Related Articles

Improve Article

Encryption and Decryption Application in Android using Caesar Cipher Algorithm

  • Last Updated : 30 Jun, 2021
Geek Week

Here, we are going to make an application of “Encryption-decryption”. By making this application we will be able to learn that how we can convert a normal text to ciphertext and encrypt our message. We will also be decrypting our message with help of a key by again converting it to a readable form. This article will help you to introduce basic concepts of cryptography in android development.


Before proceeding with an application you should be aware of Caesar Cipher Algorithm of Cryptography. If you are not aware of it, use Caesar Cipher in Cryptography article to understand it.

What we are going to build in this article? 

In this application, we will provide a space(TextView) to display the output of encrypted or decrypted messages. The message, ciphertext, and key will be taken as input from the user. A java class named the utility will be made to write the logic for encryption and decryption buttons. Note that we are going to implement this application using Java language. A sample video is given below to get an idea about what we are going to do in this article.

Now let see the step-by-step implementation of this application.

Step by Step Implementation

Step 1: Creating a new project

  • Open a new project.
  • We will be working on Empty Activity with language as Java. Leave all other options unchanged.
  • You can change the name of the project at your convenience.
  • There will be two default files named as activity_main.xml and

If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio? 

Step 2: Working with activity_main.xml file

Here we will design the user interface of our application. We will be using the following components for their respective works:

  • TextView – to show output(encrypted or decrypted message).
  • EditText – to take input(message, ciphertext, and key).
  • Button – to encrypt or decrypt the message on click.

Navigate to the app > res > layout > activity_main.xml and add the below code to that file.


<?xml version="1.0" encoding="utf-8"?>
    <!-- This text view is used to show the 
         output of encrypted or decrypted message  -->
        android:hint="Enter Your Text"
        app:layout_constraintTop_toBottomOf="@+id/tV2" />
    <!-- This text view is used to show 
         the text "Caesar Cipher AAlgorithm" -->
        android:text="Caesar Cipher Algorithm"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- Button to perform the operations 
         to encrypt the message-->
        app:layout_constraintStart_toStartOf="parent" />
    <!-- Button to perform the operations
         to decrypt the message-->
        app:layout_constraintTop_toBottomOf="@+id/key_dt" />
    <!-- Edit text to take input of message 
         which user want to encrypt-->
        android:hint="Your Message"
        app:layout_constraintTop_toBottomOf="@+id/tV2" />
    <!-- Edit text to take input of ciphertext 
         using which encryption will be done -->
        android:hint="Cipher Text"
        app:layout_constraintTop_toBottomOf="@+id/inputMessage" />
    <!-- Edit text to take input of key using
         which message will be decrypted -->
        app:layout_constraintTop_toBottomOf="@+id/ciphEdt" />

After implementing the above code, the design of the activity_main.xml file looks like this.

Step 3: Creating a new java class

Create a new java class as shown below and name it as “utility“.

Step 4: Working with file

All the logic to encrypt and decrypt our message will be written in this class. Use the code provided below in this class:


public class utility {
    // Declaration of all the required variables
    private static String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static int index;
    private static int updated_index;
    private static int final_index;
    private static int index_p_t_l;
    private static int index_s_t_l;
    private static String plainTxt;
    private static String cipherTxt;
    private static String finalTxt;
    // code for encryption
    public static String encrypt1(String plaintext, int encrptionKey) {
        // plaintext is converted to uppercase 
        // so that it is easy to convert according
        // to Caesar Cipher algorithm
        plaintext = plaintext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < plaintext.length(); index++) {
            // checking the condition for plaintext to be
            // null at some character position
            if (plaintext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(plaintext.charAt(index));
                // index is being updated 
                // so that next and final index
                // be used for ciphertext
                updated_index = encrptionKey + alphabet.indexOf(plaintext.charAt(index));
                if (updated_index >= alphabet.length()) {
                    final_index = updated_index - alphabet.length();
                } else
                    final_index = updated_index;
                // substring is used so that every character
                // can be separately converted to ciphertext
                cipherTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt = finalTxt + cipherTxt;
        // returning the 
        // final changed text
        return finalTxt;
    // code for decryption
    public static String decrypt1(String ciphertext, int decryptionKey) {
        ciphertext = ciphertext.toUpperCase();
        // using a for loop the use index and 
        // change text with help of it
        for (index = 0; index < ciphertext.length(); index++) {
            if (ciphertext.charAt(index) != ' ') {
                index_p_t_l = alphabet.indexOf(ciphertext.charAt(index));
                index_s_t_l = index_p_t_l;
                // index is updated with help of decryption
                // key which we provided as input
                updated_index = alphabet.indexOf(ciphertext.charAt(index)) - decryptionKey;
                if (updated_index < 0) {
                    final_index = updated_index + alphabet.length();
                } else
                    final_index = updated_index;
                // reverse of encryption is done as 
                // substring here is used to convert
                // each ciphertext character to plaintext 
                plainTxt = alphabet.substring(final_index, final_index + 1);
                finalTxt += plainTxt;
        // returning the 
        // final changed text
        return finalTxt;
    // method to reset the text
    // in the output textview
    private static void reset() {
        finalTxt = "";

Step 5: Working with file

In file onClickListerner is used on buttons of encryption and decryption and methods from the utility class are directly passed here. Use the following code in the file.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    // declaring all essential variables
    private Button encrypt, decrypt;
    private EditText message, cipher, key;
    private TextView screen_output;
    private static final String alphabetString = "abcdefghijklmnopqrstuvwxyz";
    protected void onCreate(Bundle savedInstanceState) {
        // findViewById is the method that 
        // finds the View by the ID it is given
        encrypt = findViewById(;
        decrypt = findViewById(;
        screen_output = findViewById(;
        message = findViewById(;
        cipher = findViewById(;
        key = findViewById(;
        // setting onCLickLister on encrypt button
        encrypt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                encrypt12(message.getText().toString(), Integer.parseInt(key.getText().toString()));
        // setting onCLickLister on decrypt button
        decrypt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                decrypt12(cipher.getText().toString(), Integer.parseInt(key.getText().toString()));
    // method to show the final output on the output
    // textView when decrypt button is clicked
    public void decrypt12(String cipher, int key) {
        screen_output.setText((utility.decrypt1(cipher, key).toLowerCase()));
    // method to show the final output on the output
    // textView when encrypt button is clicked
    public String encrypt12(String message, int shiftkey) {
        message = message.toLowerCase();
        String cipherText = "";
        for (int i = 0; i < message.length(); i++) {
            int charPosition = alphabetString.indexOf(message.charAt(i));
            int keyval = (shiftkey + charPosition) % 26;
            char replaceVAL = alphabetString.charAt(keyval);
            cipherText += replaceVAL;
        // returning the final ciphertext
        return cipherText;

Congratulations!! you have successfully made the encryption and decryption application using Caesar Cipher Algorithm. Here is the final output of our application.


Note: Do not enter the key number more than 26 because we have used the Caesar Cipher Algorithm for 26 alphabets. The app may crash if the key is more than 26.

GitHub link:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :