0% found this document useful (0 votes)
48 views2 pages

Vishal Gowda C S 4YG17CS021 Write A Program For Simple RSA Algorithm To Encrypt and Decrypt The Data

The document describes a Java program that implements a simple RSA encryption and decryption algorithm. The program takes in two prime numbers as input to generate the public and private keys. It then encrypts a user-input message using the public key and decrypts the ciphertext using the private key, successfully recovering the original plaintext. The program outputs the keys, ciphertext, and decrypted plaintext.

Uploaded by

Rakshith Hs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

Vishal Gowda C S 4YG17CS021 Write A Program For Simple RSA Algorithm To Encrypt and Decrypt The Data

The document describes a Java program that implements a simple RSA encryption and decryption algorithm. The program takes in two prime numbers as input to generate the public and private keys. It then encrypts a user-input message using the public key and decrypts the ciphertext using the private key, successfully recovering the original plaintext. The program outputs the keys, ciphertext, and decrypted plaintext.

Uploaded by

Rakshith Hs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Vishal Gowda C S

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:-

enter the prime number p and q

11
17

enter the random number b/w 3 and z

public key pair {3,187}

the private key pair{107,187}

enter the message in decimal

25

ciphertext is:

104
the plaintext is:

25

You might also like