如何解决我的tcp连接程序中的bindexception错误

juzqafwq  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(191)

我有两个类,alice和bob,我用它们来练习rsa加密,把alice的公钥发送给bob,bob用它来加密一条消息,然后把它作为密文发送给alice解密。我不断得到一个bindecxeption错误,但即使我试图改变端口号。
这是爱丽丝密码:

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;
    }

}

这是bob代码:

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

这是错误消息:


**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)**

我对这个很陌生,所以如果问题很明显,我很抱歉。有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题