I have two classes, Alice and Bob and I'm using them to practise RSA encryption by sending Alice's public key to Bob who uses it to encrypt a message which is then sent as ciphertext back to Alice for decryption. I keep getting a BindEcxeption error but even when I try to change the port numbers.
This is the Alice code:
import java.security.SecureRandom;
import java.net.*;
import java.io.*;
public class Alice {
public static void main (String[] args) throws IOException {
@SuppressWarnings("resource")
int bitLength = 512;
SecureRandom randomNum = new SecureRandom();
int certainty = 50; // 1 - 1/2(50) certainty
//Calculating p, q and n(public key)
BigInteger p = new BigInteger(bitLength, certainty, randomNum);
BigInteger q = new BigInteger(bitLength, certainty, randomNum);
BigInteger n = calculateMod(p,q);
//Calculating phiN
BigInteger phiN = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
//Calculating e
BigInteger e = new BigInteger("65537");
//Calculate private key
BigInteger d = e.modInverse(phiN);
//TCP Socket creation and sending of data
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(49999);
Socket socket = serverSocket.accept();
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println(n);
pw.flush();
System.out.println("Connected");
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedReader = new BufferedReader(in);
String sent = bufferedReader.readLine();
BigInteger sentCipher = new BigInteger(sent);
//Decryption
BigInteger decryptedMessage = sentCipher.modPow(d, n);
System.out.println("Number message sent was " + decryptedMessage);
}
private static BigInteger calculateMod(BigInteger p, BigInteger q) {
BigInteger n = p.multiply(q);
return n;
}
}
This is the Bob code:
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Bob {
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
//Taking user message input
System.out.println("Enter number message: ");
BigInteger message = scanner.nextBigInteger();
//Calculating e
BigInteger e = new BigInteger("65537");
//TCP Socket creation
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(49999);
Socket socket = serverSocket.accept();
System.out.println("Connected");
InputStreamReader in = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedReader = new BufferedReader(in);
String str = bufferedReader.readLine();
BigInteger publicKey = new BigInteger(str);
//Encryption
BigInteger ciphertext = message.modPow(e, publicKey);
//Sending ciphertext
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println(ciphertext);
pw.flush();
}
}
This is the error message:
**Exception in thread "main" java.net.BindException: Address already in use: bind
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:479)
at java.base/sun.nio.ch.Net.bind(Net.java:468)
at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:643)
at java.base/java.net.ServerSocket.bind(ServerSocket.java:396)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:282)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:173)
at Bob.main(Bob.java:20)**
I'm very new to this so I'm sorry if the issue is obvious. Any ideas?