RSA CryptoSystem Using JAVA
RSA is one of the first practicable public-key cryptosystems and is
widely used for secure data transmission. In such a cryptosystem, the encryption
key is public and differs from the decryption key which is kept secret. In RSA,
this asymmetry is based on the practical difficulty of factoring the product of
two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi
Shamir and Leonard Adleman, who first publicly described the algorithm in 1977.
Clifford Cocks, an English mathematician, had developed an equivalent system in
1973, but it wasn't declassified until 1997.
A user of RSA creates and then publishes a public key based on the two large
prime numbers, along with an auxiliary value. The prime numbers must be kept
secret. Anyone can use the public key to encrypt a message, but with currently
published methods, if the public key is large enough, only someone with
knowledge of the prime numbers can feasibly decode the message. Breaking RSA encryption
is known as the RSA problem. It is an open question whether it is as hard as
the factoring problem.
Encryption :
import java.util.*;
class RSACryptoSystem{
int plainText,cipherText;
int p,q,n;
int d,e;
RSACryptoSystem(){
plainText=cipherText=0;
p=q=n=0;
d=e=0;
}
void setPlainText(int p){
plainText=p;
}
void setCipherText(int c){
cipherText=c;
}
void setPQ(int p,int q){
this.p=p;
this.q=q;
n=p*q;
}
void setDE(int d,int e){
this.d=d;
this.e=e;
}
int getCipherText(){return cipherText;}
int getPlainText(){return plainText;}
boolean checkDE(int d,int e){
int delN=(p-1)*(q-1);
if(((e*d)%delN)==1)
return true;
else
return false;
}
int fastExponentiation(int a,int b,int c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return (int) x%c;
}
void makeCipher(){
cipherText=fastExponentiation(plainText,e,n);
}
void makePlain(){
plainText=fastExponentiation(cipherText,d,n);
}
}
class RSAEncryption{
public static void main(String [] args){
Scanner read=new Scanner(System.in);
RSACryptoSystem r=new RSACryptoSystem();
System.out.print("Enter plianText:- ");
r.setPlainText(Integer.parseInt(read.nextLine()));
System.out.print("enter value of P:-");
int p=Integer.parseInt(read.nextLine());
System.out.print("enter value of Q:-");
int q=Integer.parseInt(read.nextLine());
r.setPQ(p,q);
System.out.print("enter value of E:-");
int e=Integer.parseInt(read.nextLine());
System.out.print("enter value of D:-");
int d=Integer.parseInt(read.nextLine());
if(r.checkDE(d,e)){
r.setDE(d,e);
r.makeCipher();
System.out.println("plainText:- "+r.getPlainText());
System.out.println("CipherText:- "+r.getCipherText());
}
else
System.out.println("Wrong D and E value sorry....");
}
}
Decryption :
import java.util.*;
class RSADecryption{
public static void main(String [] args){
Scanner read=new Scanner(System.in);
RSACryptoSystem r=new RSACryptoSystem();
System.out.print("Enter CipherText:- ");
r.setCipherText(Integer.parseInt(read.nextLine()));
System.out.print("enter value of P:-");
int p=Integer.parseInt(read.nextLine());
System.out.print("enter value of Q:-");
int q=Integer.parseInt(read.nextLine());
r.setPQ(p,q);
System.out.print("enter value of E:-");
int e=Integer.parseInt(read.nextLine());
System.out.print("enter value of D:-");
int d=Integer.parseInt(read.nextLine());
if(r.checkDE(d,e)){
r.setDE(d,e);
r.makePlain();
System.out.println("CipherText:- "+r.getCipherText());
System.out.println("plainText:- "+r.getPlainText());
}
else
System.out.println("Wrong D and E value sorry....");
}
}
No comments:
Post a Comment