Java Program to Implement the RSA Algorithm


The RSA name is given by their inventors which is used to encrypt the text with high security. The RSA technique is one of the most used techniques to encrypt text, as it is the asymmetric encryption algorithm. It encrypts the text by utilizing the mathematical properties of the prime numbers.

In the RSA algorithm, the sender and receiver have private keys. Also, a common public key exists, which the sender shares with the receiver. The sender encrypts the plain text using their own public and private key, and the receiver decrypts the message using their private and public common key.

Problem statement - We need to generate the cipher text related to the given plain text using the RSA algorithm.

Sample examples

Input

prime1 = 5, prime2 = 7, message = 32

Output

2.0

Explanation - We use the RSA algorithm to encrypt the text.

Input

prime1 = 11, prime2 = 23, message = 3434

Output

228.0

Explanation - We performed operations according to the RSA algorithm to decrypt it.

Approach 1

In this approach, we will write Java code by following the RSA algorithm. The RSA technique contains three parts. In the first part, we need to find the private key. In the second part, we need to encrypt the message, and in the last part, we need to decrypt the message.

Below, we have given the step-by-step guide for writing the RSA algorithm.

Algorithm

Step 1 - Initialize the 'd' variable with 0, a private key, and 'e' to store the exponent. Also, define the prime1 and prime2 variables and initialize them.

Step 2 - Also, Initialize the message with a positive integer value.

Step 3 - After that, multiply prime1 and prime2 and store them in the primeMul variable.

Step 4 - Next, multiply prime1 - 1 and prime2 - 1 and store them in the primeMul1 variable.

Step 5 - Now, we need to find the value of 'e' in such a way so that the GCD of 'e' and primeMul1 is 1. The value of e can be between 2 and primeMul1.

Step 5.1 - In the getGCD() function, return the num value if the mod is zero. Otherwise, recursively call the getGCD() function.

Step 6 - Our public key is {e, n}.

Step 7 - Now, find the private key.

Step 7.1 - Traverse through 1 to 9 digits. In the loop, if 1 + (m * primeMul1) is divisible by 'e', store (1 + (m * primeMul1))/e to the 'd', which will be used to create a private key.

Step 8 - Our private key is {d, n}.

Step 9 - To get the cipher text, use the Math.pow() method to find the message and take its modulo with the primeMul variable's value.

Step 10 - We got the cipher text successfully. We need to decrypt the cipher text to convert it into plain text.

Step 11 - To get the plain text again, we need to take (cipherd % primeMul).

Example

import java.math.*;
import java.util.*;
public class Main {
   public static int getGCD(int mod, int num) {
      // If the mod is zero, return the num
      if (mod == 0)
         return num;
      else
         // recursive function call
         return getGCD(num % mod, mod);
   }
   public static void main(String args[]) {
      int d = 0, e; // Intialization      
      int message = 32; // number message      
      int prime1 = 5; // 1st prime number p
      int prime2 = 7; // 2nd prime number q
      int primeMul = prime1 * prime2; // performing operations
      int primeMul1 = (prime1 - 1) * (prime2 - 1);
      System.out.println("primeMul1 is equal to : " + primeMul1 + "\n");
      // Finding the valid public key
      for (e = 2; e < primeMul1; e++) {
         // Here e is a public key
         if (getGCD(e, primeMul1) == 1) {
            break;
         }
      }
      // Printing the public key
      System.out.println("Public key e is = " + e);
      // Calculating the private key
      for (int m = 0; m <= 9; m++) {
         // get the value of temp
         int temp = 1 + (m * primeMul1);
         // private key
         if (temp % e == 0) {
            d = temp / e;
            break;
         }
      }
      System.out.println("d is : " + d);
      double cipher;
      BigInteger d_message;
      // getting the cipher text
      cipher = (Math.pow(message, e)) % primeMul;
      System.out.println("Cipher text is : " + cipher);
      // Int to BigInteger
      BigInteger bigN = BigInteger.valueOf(primeMul);
      // Float to bigINt
      BigInteger bigC = BigDecimal.valueOf(cipher).toBigInteger();
      // decrypting the message
      d_message = (bigC.pow(d)).mod(bigN);
      // print decrypted message
      System.out.println("Decrypted text is : " + d_message);
   }
}

Output

primeMul1 is equal to : 24

Public key e is = 5
d is : 5
Cipher text is : 2.0
Decrypted text is : 32

Time complexity - O(logn) as we find GCD.

Space complexity - O(1) as we use constant space.

We learned to implement the RSA algorithm. It is one of the best techniques to encrypt important messages. However, it is also time expensive for large messages and prime numbers, but as we use large prime numbers, it becomes more complex and hard to break the message for hackers.

Updated on: 24-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements