gpt4 book ai didi

java - diffie hellman 加密公钥给出错误的值

转载 作者:行者123 更新时间:2023-12-02 01:10:12 26 4
gpt4 key购买 nike

所以我正在尝试进行客户端-服务器 diffie hellman 加密。我使用这个:serverPublicKey = (int)Math.pow(generator,serverPrivateKey)%prime;来生成服务器的公钥,这个:clientPublicKey = (int)Math.pow(generator,clientPrivateKey) )%prime; 生成客户端的公钥。

当我将服务器的公钥发送给客户端时,一切都很好。通常给出一个 2 位整数(例如 34)作为结果。但是,当我每次将客户端的公钥发送到服务器时,都会得到像这样的大数字,结果如下:167772160

这是我的代码

服务器端

int clientPublicKey;
int serverPrivateKey;
int serverPublicKey;
int prime = rand.nextInt(100);
int generator = rand.nextInt(20);
//sending generator and prime to client
IntToClient.writeInt(prime);//1
IntToClient.writeInt(generator);//2
System.out.println("sent to client Prime :"+prime);
System.out.println("sent to client Generator :"+generator);
System.out.println("Server's private key: ");
serverPrivateKey = sc.nextInt();
//server's public key
serverPublicKey = (int)Math.pow(generator,serverPrivateKey)%prime;
System.out.println("Server sends in public --> "+ serverPublicKey);
IntToClient.writeInt(serverPublicKey);//3
clientPublicKey = IntFromClient.readInt();//4
System.out.println("Client sent public key --> "+ clientPublicKey);

客户端

int prime;
int generator;
int clientPrivateKey;
int clientPublicKey;
int serverPublicKey;

//get random prime and generator from server
prime = IntFromServer.readInt();//1
generator = IntFromServer.readInt();//2
System.out.println("Prime :"+prime);
System.out.println("Generator :"+generator);

//read server's public key
serverPublicKey = IntFromServer.readInt();//3
System.out.println("Server sent public key --> "+ serverPublicKey);

//clients private key
System.out.println("Client's private key: ");
clientPrivateKey = sc.nextInt();
//client's public key
clientPublicKey = (int)Math.pow(generator,clientPrivateKey)%prime;

System.out.println("Client sends in public --> "+ clientPublicKey);
IntToServer.writeInt(clientPublicKey);//4`

所以当我运行它时,我从服务器得到这个

发送给客户端 Prime :44
发送到客户端生成器:3
服务器的私钥:
45
服务器公开发送 --> 23
客户端发送公钥 --> 167772160

这是来自客户

素数:44
发电机:3
服务器发送公钥 --> 23
客户的私钥:
87
客户端公开发送 --> 23

最佳答案

如果您通过管道发送值(Socket?NamedPipe?SerialPort?):

IntToServer.writeInt(clientPublicKey);

...服务器读取另一个值而不是发送的值:

ClientFromClient.readInt();

...那么显然你的管道没有很好地实现。显示的所有加密计算都是无用的,也不需要解决此问题。

关于java - diffie hellman 加密公钥给出错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59510991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com