0% found this document useful (0 votes)
4 views

4th RSA algorithm to encrypt and decrypt the data

The document provides a Java implementation of the RSA algorithm for encrypting and decrypting messages. It includes steps for user input of prime numbers, calculation of public and private keys, and methods for encryption and decryption using modular arithmetic. The program also handles the computation of the greatest common divisor and modular inverse necessary for RSA operations.

Uploaded by

Hemanth BR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

4th RSA algorithm to encrypt and decrypt the data

The document provides a Java implementation of the RSA algorithm for encrypting and decrypting messages. It includes steps for user input of prime numbers, calculation of public and private keys, and methods for encryption and decryption using modular arithmetic. The program also handles the computation of the greatest common divisor and modular inverse necessary for RSA operations.

Uploaded by

Hemanth BR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Develop a program for a simple RSA algorithm to encrypt and


decrypt the data.

import java.util.*; // Importing Java utilities for Scanner input


import java.math.BigInteger; // Importing BigInteger for handling large
numbers in RSA

class RSA {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // Creating Scanner object for user
input

// Step 1: Get the message from the user


System.out.println("Enter the message: ");
String msg = in.nextLine(); // Read the input message as a string

// Step 2: Get two large prime numbers from the user


System.out.println("Enter two large prime numbers p & q: ");
int p = in.nextInt(); // Read first prime number
int q = in.nextInt(); // Read second prime number

// Step 3: Compute n (modulus) and Euler’s Totient Function (phi)


int n = p * q; // Compute n = p * q
int phi = (p - 1) * (q - 1); // Compute Euler’s totient function φ(n) = (p-
1)*(q-1)

// Step 4: Choose a public exponent 'e' such that gcd(e, phi) = 1 (relatively
prime)
int e;
do {
System.out.println("Enter a value e such that e is relatively prime to " +
phi);
e = in.nextInt(); // Read the value of e
} while (gcd(phi, e) != 1); // Ensure e and φ(n) are coprime

// Step 5: Compute private key 'd' using modular inverse


int d = modInverse(e, phi); // Compute modular inverse of e mod φ(n)

// Step 6: Display the generated public and private keys


System.out.println("\nThe public key is {" + e + ", " + n + "}"); // Public
key (e, n)
System.out.println("The private key (d) is: " + d); // Private key (d, n)

// Step 7: Encrypt the message


System.out.println("\nCipher text is:");
BigInteger[] cipher = new BigInteger[msg.length()]; // Array to store
encrypted values
for (int i = 0; i < msg.length(); i++) {
int s = (int) msg.charAt(i); // Convert character to ASCII
cipher[i] = encrypt(s, e, n); // Encrypt using RSA
System.out.print(cipher[i] + "\t"); // Print encrypted values
}

// Step 8: Decrypt the message


StringBuilder decryptedMsg = new StringBuilder(); // StringBuilder to
store decrypted message
for (BigInteger c : cipher) {
int m1 = decrypt(c, d, n); // Decrypt each character
decryptedMsg.append((char) m1); // Convert back to character and
append
}
System.out.println("\nThe decrypted message is: " + decryptedMsg); //
Display decrypted message

in.close(); // Close Scanner to prevent resource leaks


}

/**
* Function to perform RSA encryption using modular exponentiation.
* @param m The message character (as integer)
* @param e The public key exponent
* @param n The modulus (product of two primes)
* @return Encrypted value as BigInteger
*/
public static BigInteger encrypt(int m, int e, int n) {
return BigInteger.valueOf(m).modPow(BigInteger.valueOf(e),
BigInteger.valueOf(n));
}

/**
* Function to perform RSA decryption using modular exponentiation.
* @param c The encrypted message character (ciphertext)
* @param d The private key exponent
* @param n The modulus (product of two primes)
* @return Decrypted value as integer (original character)
*/
public static int decrypt(BigInteger c, int d, int n) {
return c.modPow(BigInteger.valueOf(d),
BigInteger.valueOf(n)).intValue();
}

/**
* Function to compute the Greatest Common Divisor (GCD) using
Euclidean Algorithm.
* @param a First number
* @param b Second number
* @return The GCD of a and b
*/
public static int gcd(int a, int b) {
while (b != 0) { // Loop until remainder becomes zero
int r = a % b; // Compute remainder
a = b; // Swap values
b = r; // Assign remainder to b
}
return a; // Return GCD value
}

/**
* Function to compute modular inverse using Extended Euclidean
Algorithm.
* @param e The public exponent
* @param phi Euler's totient function φ(n)
* @return The modular inverse of e mod φ(n) (i.e., private key d)
*/
public static int modInverse(int e, int phi) {
int t = 0, newT = 1; // Initialize variables for Extended Euclidean
Algorithm
int r = phi, newR = e; // Initialize remainder values

while (newR != 0) { // Loop until remainder becomes zero


int quotient = r / newR; // Compute quotient
int temp = t;
t = newT;
newT = temp - quotient * newT; // Update coefficients

temp = r;
r = newR;
newR = temp - quotient * newR; // Update remainders
}

if (r > 1) {
throw new ArithmeticException("e is not invertible"); // Ensure e has an
inverse
}
if (t < 0) {
t += phi; // Ensure positive modular inverse
}
return t; // Return modular inverse (private key d)
}
}
Program

import java.util.*;
import java.lang.String;
class rsa
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the message ");
String msg=in.nextLine();
System.out.println("Enter Two large prime numbers p & q");
int p=in.nextInt();
int q=in.nextInt();
int n=p*q;
int phi=(p-1)*(q-1);
int e,k=0;
do
{
System.out.println("Enter a value e such that e is relatively prime
to "+phi);
e=in.nextInt();
}while(gcd(phi,e)!=1);
for(k=1;((phi*k)+1)%e!=0;k++);
int d=((phi*k)+1)/e;
System.out.println("\n The public key is {"+e+","+n+"} \n");
System.out.println("d="+d);
System.out.println("Cipher text is:");
int[] cipher=new int[50];
int i;
for(i=0;i<msg.length();i++)
{
int s=(int)msg.charAt(i);
cipher[i]=enc(s,e,n);
System.out.print(cipher[i]+"\t");
}
String msg1=””;
for(i=0;i<msg.length();i++)

{
int m1=enc(cipher[i],d,n);
msg1+=(char)m1;
}
System.out.println("\nThe decrypted msg is "+msg1);
}
public static int enc(int m,int a,int b)
{
int c=1;
for(int j=0;j<a;j++)
c=(c*m)%b;
return c;
}
public static int gcd(int m, int n)
{
while(n!=0)
{
int r=m%n;
m=n;
n=r;
}
return m;
}
}

You might also like