Skip to content

Commit

Permalink
Add RSA Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrohitsuthar committed May 27, 2021
1 parent 740385f commit 7812c64
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
74 changes: 74 additions & 0 deletions ICS/Assignment4/RSAClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.Random;

public class RSAClient
{

public static int getPublicKey(int phi)
{
Random r = new Random();
int result = r.nextInt(phi-2) + 2;
while (BigInteger.valueOf(phi).gcd(BigInteger.valueOf(result)).intValue() != 1) {
result = r.nextInt(phi-2) + 2;
}
return result;
}

public static int getPrivateKey(int phi, int e)
{
int k = 1;
double d = (double) (k * phi + 1) / (double) e;
while (Math.floor(d) != d)
{
k = k + 1;
d = (double) (k * phi + 1) / (double) e;
}
return (int)d;
}

public static void main(String[] args) throws UnknownHostException, IOException
{
Socket soc = new Socket("127.0.0.1",8088);
System.out.println("Connected to "+soc.getRemoteSocketAddress());
DataInputStream dis = new DataInputStream(soc.getInputStream());
DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
Scanner sc = new Scanner(System.in);

System.out.print("Enter first prime number (P): ");
int p = sc.nextInt();
System.out.print("Enter second prime number (Q): ");
int q = sc.nextInt();

int n = p * q;
System.out.println("n: " + n);

int phi = (p - 1) * (q - 1);
System.out.println("Ø(n): " + phi);

int e = getPublicKey(phi);
int d = getPrivateKey(phi, e);

dos.writeInt(n);
dos.writeInt(e);

System.out.println("Public key {e,n} = { " + e + ", " + n + " }");
System.out.println("Private key {d,n} = { " + d + ", " + n + " }");

System.out.println("Waiting for Ciphertext....");
int cipherText = dis.readInt();
System.out.println("Received Ciphertext : "+cipherText);
BigInteger plainText = BigInteger.valueOf(cipherText).pow(d).mod(BigInteger.valueOf(n));

System.out.println("Decrypted Plain Text: "+plainText);

soc.close();
sc.close();

}
}
38 changes: 38 additions & 0 deletions ICS/Assignment4/RSAServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class RSAServer
{
public static void main(String[] args) throws IOException
{
ServerSocket server = new ServerSocket(8088);
System.out.println("Waiting for connection on port " + server.getLocalPort());
Socket soc = server.accept();
System.out.println("Accepted connection from " + soc.getRemoteSocketAddress());
DataInputStream dis = new DataInputStream(soc.getInputStream());
DataOutputStream dos = new DataOutputStream(soc.getOutputStream());

Scanner sc = new Scanner(System.in);
int n = dis.readInt();
int e = dis.readInt();
System.out.println("n = " + n + " \ne = " + e);

System.out.println("Public key {e,n} = { " + e + ", " + n + " }");

System.out.print("Enter plaintext for encryption (Numeric Form): ");
int plainText = sc.nextInt();

BigInteger cipherText = BigInteger.valueOf(plainText).pow(e).mod(BigInteger.valueOf(n));
System.out.println("Generated Ciphertext : " + cipherText);
dos.writeInt(cipherText.intValue());

soc.close();
server.close();
sc.close();
}
}

0 comments on commit 7812c64

Please sign in to comment.