RSA Encryption and Decryption:: Experiment 6 Aim
RSA Encryption and Decryption:: Experiment 6 Aim
EXPERIMENT 6
RSA Class:
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
class RSA{
private int p;
private int q;
private int n;
private int phi;
private int e;
private int[] publicKeys = new int[2];
private int[] privateKeys = new int[2];
public static int taken;
if(d2==1) flag=1;
System.out.println("a\tb\td\tk");
System.out.println(a1+"\t"+b1+"\t"+d1+"\t"+k1);
System.out.println(a2+"\t"+b2+"\t"+d2+"\t"+k2);
while(flag==0){
int temp = a2;
a2 = a1 - a2*k2;
a1 = temp;
temp = b2;
b2 = b1 - b2*k2;
b1 = temp;
temp = d2;
d2 = d1 - d2*k2;
d1 = temp;
k1 = k2;
k2 = d1/d2;
System.out.println(a2+"\t"+b2+"\t"+d2+"\t"+k2);
IT117-Jaimish Trivedi
if(d2==1){
flag = 1;
}
}
return b2;
}
//Encryption
public String encrypt(int plain, String publicKeys){
String publicKey[] = publicKeys.split(" ");
BigInteger e = BigInteger.valueOf(Integer.parseInt(publicKey[0]));
BigInteger n = BigInteger.valueOf(Integer.parseInt(publicKey[1]));
BigInteger p = BigInteger.valueOf(plain);
//Decryption
public String decrypt(int cipher, String privateKeys){
String privateKey[] = privateKeys.split(" ");
BigInteger d = BigInteger.valueOf(Integer.parseInt(privateKey[0]));
BigInteger n = BigInteger.valueOf(Integer.parseInt(privateKey[1]));
BigInteger c = BigInteger.valueOf(cipher);
if(d<0) d+=this.phi;
if(d>phi) d%=this.phi;
ServerRSA Class:
import java.net.*;
import java.io.*;
import java.util.*;
IT117-Jaimish Trivedi
//Constructor
public ServerRSA(int port){
try{
//Generating Public and Private keys of server
RSA serv_rsa = new RSA(6);
String publicKey = serv_rsa.getPublicKeyString();
String privateKey = serv_rsa.getPrivateKeyString();
System.out.println("Server's public key : " + publicKey);
System.out.println("Server's private key : " + privateKey);
//Establishing connection
socket = server.accept();
System.out.println("\tClient accepted");
try{
//Getting client's public key and storing it in text file
String clientPublicKey = in.readUTF();
System.out.println("Client's Public Key : " + clientPublicKey);
fw = new FileWriter("Client Keys.txt");
for (int i = 0; i < clientPublicKey.length(); i++)
fw.write(clientPublicKey.charAt(i));
System.out.println("\tKey Stored in Client Keys.txt");
fw.close();
//Driver main
public static void main(String args[]){
new ServerRSA(5000);
}
}
ClientRSA Class:
import java.util.*;
import java.net.*;
import java.io.*;
//Constructor
public ClientRSA(String address, int port){
//Establishing connection with server
try{
socket = new Socket(address, port);
System.out.println("\tConnected");
input = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
IT117-Jaimish Trivedi
}catch(Exception e){
e.printStackTrace();
}
try{
//Sending client's public key
out.writeUTF(line);
//Getting server's public key
String serverPublicKey = (String)input.readUTF();
System.out.println("Server's public key : " + serverPublicKey);
ServerRSA Output:
IT117-Jaimish Trivedi
ClientRSA Output: