gpt4 book ai didi

javax.crypto.BadPaddingException : Data must start with zero with client cannot decrypt from server

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

运行代码时出现编译错误。

Error: javax.crypto.BadPaddingException: Data must start with zero

在这一行testClient.java

byte[] newPlainText = cipher.doFinal(cipherTextFromServer);

客户端无法解密从服务器加密的消息。我的代码有什么错误吗?

//TestServer.java

public class TestServer {

public static void main(String[] args) throws Exception{
//set variables for port number, RSA key size
final int port = 3344;
final int RSAKeySize = 1024;
final String newline = "\n";
//set public key, sockets, server text, plain text
PublicKey pubKey = null;
PrivateKey priKey = null;
ServerSocket server = null;
Socket client = null;
String serverText = "Hello Client! This is an authentication message from server";
byte[] plainText = serverText.getBytes("UTF8");
//initialize RSA
try {
System.out.println("Start generating RSA key");
KeyPairGenerator RSAKeyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
RSAKeyGen.initialize(RSAKeySize,random);
KeyPair pair = RSAKeyGen.generateKeyPair();
System.out.println("Finish generating RSA key");
pubKey = pair.getPublic();
priKey = pair.getPrivate();
}catch (GeneralSecurityException e){
System.out.println(e.getLocalizedMessage() + newline);
System.out.println("Error initialising encryption. Exiting.\n");
System.exit(0);
}
//initialize cryptography, set cipherText
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("RSA cipher object and provider"+cipher.getProvider().getInfo());
System.out.println("Start Encryption for plainText");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println("Finish Encryption to cipherText: ");
BASE64Encoder base64 = new BASE64Encoder();
String encryptedValue = base64.encode(cipherText);
//String encryptedValue = new sun.misc.BASE64Encoder().encode(cipherText);
System.out.println(new String(cipherText,"UTF8"));
System.out.println("Base64");
System.out.println(encryptedValue);
//initialize socket connection
try{
server = new ServerSocket(port);
client = server.accept();
}catch(IOException e){
System.out.println("Error initialising I/O.\n");
System.exit(0);
}
//send server private key
try{
System.out.println("Send private key out");
System.out.println(DatatypeConverter.printHexBinary(priKey.getEncoded()));
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(priKey.getEncoded().length);
client.getOutputStream().write(bb.array());
client.getOutputStream().write(pubKey.getEncoded());
client.getOutputStream().flush();
}catch (IOException e){
System.out.println("I/O Error");
System.exit(0);
}
//send cipherText
ObjectOutputStream obOut = new ObjectOutputStream(client.getOutputStream());
obOut.writeObject(encryptedValue);
obOut.flush();
client.close();
}

}

//TestClient.java

public class TestClient {

public static void main(String[] args){
//throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, ClassNotFoundException,
//InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//set variable for port, socket, server public key
final int port = 3344;
Socket sock = null;
Key serverPubKey = null;
final int RSAKeySize = 1024;
final String newline = "\n";
Key priKey = null;
//setup connection by creating socket
try{
sock = new Socket(InetAddress.getLocalHost(),port);
}catch(UnknownHostException e){
System.out.println("Unknown host.");
System.exit(1);
}catch(IOException e){
System.out.println("No I/O");
System.exit(1);
}
//get public key from server
try{
byte[] lenb = new byte[4];
sock.getInputStream().read(lenb,0,4);
ByteBuffer bb = ByteBuffer.wrap(lenb);
int len = bb.getInt();
System.out.println(len);
byte[] servPubKeyBytes = new byte[len];
sock.getInputStream().read(servPubKeyBytes);
System.out.println(DatatypeConverter.printHexBinary(servPubKeyBytes));
X509EncodedKeySpec ks = new X509EncodedKeySpec(servPubKeyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
serverPubKey = kf.generatePublic(ks);
System.out.println(DatatypeConverter.printHexBinary(serverPubKey.getEncoded()));
//PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));

}catch (IOException e) {
System.out.println("Error obtaining server public key 1.");
System.exit(0);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error obtaining server public key 2.");
System.exit(0);
} catch (InvalidKeySpecException e) {
System.out.println("Error obtaining server public key 3.");
System.exit(0);
}
try {
System.out.println("Start generating RSA key");
KeyPairGenerator RSAKeyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
RSAKeyGen.initialize(RSAKeySize, random);
KeyPair pair = RSAKeyGen.generateKeyPair();
System.out.println("Finish generating RSA key");
priKey = pair.getPrivate();
}catch (GeneralSecurityException e){
System.out.println(e.getLocalizedMessage() + newline);
System.out.println("Error initialising encryption. Exiting.\n");
System.exit(0);
}
try{
//Decrypt message from server
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String message = in.readLine();
//ObjectInputStream obIn = new ObjectInputStream(sock.getInputStream());
//Object obj = obIn.readObject();
System.out.println(message);
byte[] cipherTextFromServer = new BASE64Decoder().decodeBuffer(message);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("Start decryption");
cipher.init(Cipher.DECRYPT_MODE, priKey);
byte[] newPlainText = cipher.doFinal(cipherTextFromServer);
System.out.println("Finish decryption: ");
System.out.println(new String(newPlainText,"UTF8"));
sock.close();
}catch(Exception e){
e.printStackTrace();
}
}

}

最佳答案

您的代码看起来完全损坏了。您使用服务器上生成的公钥对数据进行加密,然后使用客户端上生成的私钥对其进行解密。这永远不会起作用,因为 key 对不匹配。

为了进一步混淆问题,您似乎尝试在服务器和客户端之间发送 key ,但这看起来也被破坏了,例如

ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(priKey.getEncoded().length);
client.getOutputStream().write(bb.array());
client.getOutputStream().write(pubKey.getEncoded()); // pubKey?!!

您应该完全重新考虑您的设计。私钥通常不是集中生成并向外分发的。相反,请考虑让您的客户端生成 key 对并将其公钥发送到服务器,服务器可以使用它来加密数据。

关于javax.crypto.BadPaddingException : Data must start with zero with client cannot decrypt from server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22783294/

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