4th RSA algorithm to encrypt and decrypt the data
4th RSA algorithm to encrypt and decrypt the data
class RSA {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // Creating Scanner object for user
input
// 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
/**
* 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
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;
}
}