In: Computer Science
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Answer : Given data
* Step 1
Actually, the given a message and an integer n=pq
Where p and q are odd primes and an integer e>1 relatively prime to (p-1) (q-1)
So, the code has given below:
* Step 2
import java.io.*;
import java.util.*;
class RSATest
{
public static void main(String args[])
{
Scanner ip=new Scanner(System.in);
int p,q,n,e=1,j;
int d=1,msglen;
int t1,t2;
int pt[]= new int[10]; //plain text array
int ct[]= new int[10]; //cipher text array
int rt[]= new int[10]; //unecrypted received text array
String msg=new String();
System.out.println("Enter value of p & q number: ");
p=ip.nextInt();
q=ip.nextInt();
System.out.println("Enter the message: ");
msg=ip.next();
msglen=msg.length();
for(j=0;j<msglen;j++)
{
pt[j]=(msg.charAt(j))-96;
}
n=p*q;
t1=p-1;
t2=q-1;
while((t1*t2)%e==0)
{
e++;
}
for(j=0;j<msglen;j++)
{
ct[j]=((int)Math.pow(pt[j],e))%n;
}
System.out.println("Plaintext Message:");
System.out.println("----------------------");
System.out.println("Public Key(e)= "+e);
for(j=0;j<msglen;j++)
{
System.out.println("Cipher Text= "+ct[j]);
}
System.out.println("Plaintext Message:");
System.out.println("----------------------");
while((d*e)%(t1*t2)!=1)
{
d++;
}
System.out.println("Private Key(d)= "+d);
for(j=0;j<msglen;j++)
{
rt[j]=((int)Math.pow(ct[j],d))%n;
System.out.println("Plaintext Message= "+rt[j]);
}
System.out.print("Decrypted Message:");
for(j=0;j<msglen;j++)
{
rt[j]=rt[j]+96;
System.out.print((char)rt[j]);
}
}
}
* Step 3
* Step 4
__________THE END____________