0% found this document useful (0 votes)
95 views8 pages

RSA Encryption and Decryption:: Experiment 6 Aim

The document describes experiments with RSA encryption, authentication, and a combination of the two. It includes code for RSA, ServerRSA, and ClientRSA classes. The RSA class handles key generation and encryption/decryption methods. The ServerRSA class acts as a server that generates keys, receives an encrypted message from a client, decrypts it, then encrypts and sends a response. The ClientRSA class connects to the server, sends an encrypted message, and receives and decrypts the response.

Uploaded by

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

RSA Encryption and Decryption:: Experiment 6 Aim

The document describes experiments with RSA encryption, authentication, and a combination of the two. It includes code for RSA, ServerRSA, and ClientRSA classes. The RSA class handles key generation and encryption/decryption methods. The ServerRSA class acts as a server that generates keys, receives an encrypted message from a client, decrypts it, then encrypts and sends a response. The ClientRSA class connects to the server, sends an encrypted message, and receives and decrypts the response.

Uploaded by

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

IT117-Jaimish Trivedi

EXPERIMENT 6

AIM:​ Perform Encryption, Authentication and both using RSA.

RSA Encryption And Decryption:

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;

​//Helper function for getting public key string


​public​ String ​getPublicKeyString​(){
StringBuilder temp = ​new​ StringBuilder();
temp.append(publicKeys[​0​] + ​" "​ + publicKeys[​1​]);
​return​ temp.toString();
}

​//Helper function for getting private key string


​public​ String ​getPrivateKeyString​(){
StringBuilder temp = ​new​ StringBuilder();
temp.append(privateKeys[​0​] + ​" "​ + privateKeys[​1​]);
​return​ temp.toString();
}

​public​ ​RSA​(​int​ n){


​this​.p = ​13​; ​//Taking fixed P
​this​.q = ​17​; ​//Taking fixed q
​this​.n = p*q;
​this​.phi = (p-​1​)*(q-​1​);

​//Getting the e based on the constraints


​int​ e;
​for​(e=n; e<phi; e++){
​if​(validPublicKey(e)){
​this​.e = e;
​break​;
}
}
generateKeys();
IT117-Jaimish Trivedi

​//Helper function to check whether the public key e is valid or not


​private​ ​boolean​ ​validPublicKey​(​int​ e){
​if​(gcd(e, phi)==​1​ && e!=p && e!=q){
​if​(RSA.taken==​0​){
RSA.taken=e;
​return​ ​true​;
}​else​{
​if​(RSA.taken==e)
​return​ ​false​;
​return​ ​true​;
}
}​else​{
​return​ ​false​;
}
}

​//Helper function to find gcd of two numbers


​private​ ​int​ ​gcd​(​int​ a, ​int​ b) {
​if​(b == ​0​){
​return​ a;
}
​return​ gcd(b, a%b);
}

​//Helper function to find private key with Extended euclid Algorithm


​private​ ​int​ ​euclid​(​int​ phi, ​int​ e){
System.out.println(​"*****Extended Euclid's Algorithm*****"​);
​int​ a1 = ​1​, a2 = ​0​;
​int​ b1 = ​0​, b2 = ​1​;
​int​ d1 = phi, d2 = e;
​int​ k1 = ​0​,k2=d1/d2;
​int​ flag=​0​;

​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);

​int​ cipher = p.modPow(e, n).intValue();


​return​ ​""​+cipher;
}

​//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);

​int​ plain = c.modPow(d, n).intValue();


​return​ ​""​+plain;
}

​//Helper function for generating private key


​private​ ​void​ ​generateKeys​(){
​int​ d = euclid(​this​.phi, ​this​.e);

​if​(d<​0​) d+=​this​.phi;
​if​(d>phi) d%=​this​.phi;

publicKeys[​0​] = ​this​.e; publicKeys[​1​] = n;


privateKeys[​0​] = d; privateKeys[​1​] = n;

System.out.println(​"\ne : "​+​this​.e+​"\nd : "​+d+​"\nphi : "​+​this​.phi);


}
}

ServerRSA Class:

import​ java.net.*;
import​ java.io.*;
import​ java.util.*;
IT117-Jaimish Trivedi

public​ ​class​ ​ServerRSA​{


​private​ Socket socket = ​null​;
​private​ ServerSocket server = ​null​;
​private​ DataInputStream in = ​null​;
​private​ DataOutputStream out = ​null​;
​private​ FileWriter fw = ​null​;

​//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);

​//Starting the server


server = ​new​ ServerSocket(port);
System.out.println(​"\tServer started"​);

System.out.println(​"\tWaiting for a client ..."​);

​//Establishing connection
socket = server.accept();
System.out.println(​"\tClient accepted"​);

in = ​new​ DataInputStream(​new​ BufferedInputStream(socket.getInputStream()));


out = ​new​ DataOutputStream(socket.getOutputStream());

​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();

​//Sending server's public key


out.writeUTF(publicKey);

​//Getting the cipher text from client and decrypting it


String cipher = in.readUTF();
System.out.println(​"Cipher text from Client : "​+cipher);
System.out.println(​"Plain text from Client :
"​+serv_rsa.decrypt(Integer.parseInt(cipher), privateKey));

​//Getting plain text from Server


IT117-Jaimish Trivedi

Scanner sc = ​new​ Scanner(System.in);


System.out.print(​"Enter a plain text : "​);
​int​ plain_text = sc.nextInt();

​//Encrypting plain text and sending the cipher text


out.writeUTF(serv_rsa.encrypt(plain_text, clientPublicKey));
}​catch​(IOException i){
System.out.println(i);
}
System.out.println(​"\tClosing connection"​);

​//Closing the connection


socket.close();
in.close();
}​catch​(IOException i){
System.out.println(i);
}
}

​//Driver main
​public​ ​static​ ​void​ ​main​(String args[]){
​new​ ServerRSA(​5000​);
}
}

ClientRSA Class:

import​ java.util.*;
import​ java.net.*;
import​ java.io.*;

public​ ​class​ ​ClientRSA​{


​private​ Socket socket = ​null​;
​private​ DataInputStream input = ​null​;
​private​ DataOutputStream out = ​null​;

​//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();
}

​//Generating Public and Private keys of client


String line = ​""​;
RSA cli_rsa = ​new​ RSA(​2​);
line = cli_rsa.getPublicKeyString();
String privateKey = cli_rsa.getPrivateKeyString();
System.out.println(​"Client's public key : "​ + line);
System.out.println(​"Client's private key : "​ + privateKey);

​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);

​//Getting plain text from Client


Scanner sc = ​new​ Scanner(System.in);
System.out.print(​"Enter a plain text : "​);
​int​ plain_text = sc.nextInt();
​//Encrypting plain text and sending the cipher text
out.writeUTF(cli_rsa.encrypt(plain_text, serverPublicKey));

​//Getting the cipher text from server and decrypting it


String cipher = input.readUTF();
System.out.println(​"Cipher text from Server : "​+cipher);
System.out.println(​"Plain text from Server :
"​+cli_rsa.decrypt(Integer.parseInt(cipher), privateKey));

​//Closing the connection


input.close();
out.close();
socket.close();
}​catch​(IOException i){
System.out.println(i);
}
}
​//Driver main
​public​ ​static​ ​void​ ​main​(String args[]){
​new​ ClientRSA(​"127.0.0.1"​, ​5000​);
}
}
IT117-Jaimish Trivedi

ServerRSA Output:
IT117-Jaimish Trivedi

ClientRSA Output:

You might also like