Vishal Gowda C S 4YG17CS021 Write A Program For Simple RSA Algorithm To Encrypt and Decrypt The Data
Vishal Gowda C S 4YG17CS021 Write A Program For Simple RSA Algorithm To Encrypt and Decrypt The Data
4YG17CS021
Write a program for simple RSA algorithm to encrypt and decrypt the data.
1. import java.util.Scanner;
public class Rsa
{
static int encrypt(int M, int e, int n)
{
int k=1,i;
for(i=1;i<=e;i++)
{
k=(k*M)%n;
}
return k;
}
static int gcd(int z, int e)
{
int k;
while(true)
{
k=z%e;
z=e;
e=k;
if(k==1)
return 1;
else if(k==0)
return 0;
}
}
public static void main(String[] args)
{
Scanner t;
int p,q,n,z,e,k=1,d,i;
int ci[]= new int[20];
int in[]= new int[20];
t=new Scanner(System.in);
System.out.println("\n enter the prime number p and q\n");
p=t.nextInt();
q=t.nextInt();
n=p*q;
z=(p-1)*(q-1);
do{
System.out.println("\n enter the random number b/w 3 and z\n\n");
e=t.nextInt();
}
while((gcd(z,e)==0)&&e<(z-1));
System.out.printf("\n public key pair {%d,%d}\n",e,n);
for(k=1;((k*e)%z)!=1;k++){}
System.out.printf("\n the private key pair{%d,%d}\n\n",k,n);
System.out.println("\n enter the message in decimal\n");
t=new Scanner(System.in);
for(i=0;i<1;i++)
in[i]=t.nextInt();
for(i=0;i<1;i++)
ci[i]=encrypt(in[i],e,n);
System.out.println("\n ciphertext is:");
for(i=0;i<1;i++)
System.out.printf("\n %d\t",ci[i]);
System.out.println("\n the plaintext is:\n");
for(i=0;i<1;i++)
System.out.printf("\n%d\t",(encrypt(ci[i],k,n)));
}
}
output:-
11
17
25
ciphertext is:
104
the plaintext is:
25