Android RSA encrypt decrypt message Tutorial

RSA (Rivest-Shamir-Adleman) is one of the best secure encryption algorithms that is currently used by many developers. This algorithm involves four steps which are the key generation, key distribution, encryption, and decryption. It is asymmetric encryption by using public key and private key for encrypting and decryption confidential data. The public key is open to all people and its mostly used to encrypt data.

There have two methods in RSA which are encrypted and signed. What is the difference between encrypting and signing data in RSA?

By encrypting, you use the public key to write a message and use the private key to read a message.

By signing, you use the private key to write message’s signature and use the public key to verify the message.

In this tutorial, I will teach you how to use RSA encrypt and decrypt a message in your android project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RSAExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Create a new class

Right-click your package name and create a class “RSA”, after that paste the source code from the bottom sample to this class. This class provides all the encrypt, decrypt, sign and verify function. So you can just use this class in your activity.

Create two new activity

By create an activity, right click package name > new > Activity > Empty Activity. (Repeat 2 times cause we need extra 2 activity) First Activity “EncryptionActivity” and another Activity “SignatureActivity”.

Edit activity_encryption layout

Go to this layout and edit to the sample below. I add 3 edit text and two buttons in this layout.

Edit activity_signature layout

Modify the signature activity layout to the sample source code below.

Edit class

Go to EncryptionActivity.class and this class will perform encrypt using public key and decrypt the message using the private key.

Edit class

Modify this class to below sample so you can sign and verify message in this activity.

Edit activity_main layout

After that, modify this class by adding 3 buttons and 2 edit text.

Edit class

Edit this class to the source code below so you can generate private and public key in this activity.

Run Your Project

Finally, you are complete this project, now you can check it how the RSA encrypt and signature work in android.

(Android RSA encrypt decrypt message)

Source Code

(Visited 8,106 times, 3 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

2 Responses

  1. issacnitin says:

    Why doesn’t this work with key generated by JSEncrypt

  2. Vishal Bhayani says:

    Superb Post
    Helped me a Lot

Leave a Reply

Your email address will not be published. Required fields are marked *