Encryption and Decryption Application in Android using Caesar Cipher Algorithm
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 MainActivity.java.
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.
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 utility.java file
All the logic to encrypt and decrypt our message will be written in this class. Use the code provided below in this class:
Step 5: Working with MainActivity.java file
In MainActivity.java file onClickListener is used on buttons of encryption and decryption and methods from the utility class are directly passed here. Use the following code in the MainActivity.java file.
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.