gpt4 book ai didi

java - Java密码和socket流的三个问题

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

我的三个问题如下:

  • 我不知道如何填充字节并使其正常工作而不出现错误:javax.crypto.BadPaddingException:给定的最终 block 未正确填充

    <
  • 如果我将其切换为 NoPadding,我必须使用 8 的倍数的字符串

  • 最后也是最重要的是,我无法让服务器正确解密它。我在 CipherClient.java 中证明解密在通过套接字发送之前就有效。

使用 asHex,我可以证明 CipherServer.java 获得相同的字节,但在解密消息时,它无法正确解密。

我错过了什么吗?我不明白客户端如何正确解密它,但一旦通过流,服务器就无法解密。

我一整天都在互联网上查找示例,但我无法弄清楚。在有人说之前,我知道使用 DES 并不明智,但是,这只是针对一个项目,其中需要 DES。

CIPHERCLIENT.java

import java.io.*;
import java.net.*;
import java.security.*;

import javax.crypto.*;

public class CipherClient
{public static void main(String[] args) throws Exception
{
try {
//Starts socket
String host = "localhost";
int port = 8001;
Socket s = new Socket(host, port);

//Generate a DES key.
KeyGenerator keygen = KeyGenerator.getInstance("DES");
keygen.init(56, new SecureRandom());
SecretKey key = keygen.generateKey();

//Store the key in a file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
oos.writeObject(key);
oos.close();

//Start Cipher Instance and cipher the message
//Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
c.init(Cipher.ENCRYPT_MODE,key);

//Get string and encrypted version
//String message = "The quick brown fox jumps over the lazy dog.";
String message = "12345678";
byte[] encryptedMsg = c.doFinal(message.getBytes("UTF-8"));

System.out.println("Client - Message: " + message);
System.out.println("Client - Encrypted: " + CipherServer.asHex(encryptedMsg));


//TEST DECRYPT W/ KEY FILE W/O SERVER (WORKS!)
//-------------------------------

//Read key from file test
ObjectInputStream file = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
SecretKey key2 = (SecretKey) file.readObject();
System.out.println("Key Used: " + file.toString());
file.close();

//Decrypt Test
c.init(Cipher.DECRYPT_MODE,key2);
byte[] plainText = c.doFinal(encryptedMsg);
System.out.println("Decrypted Message: " + new String(plainText));


//Open stream to cipher server
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.writeInt(encryptedMsg.length);
os.write(encryptedMsg);

os.flush();
os.close();

//Close socket
s.close();

}catch (Exception e) {
e.printStackTrace();
}
}
}

CIPHERSERVER.java

import java.io.*;
import java.net.*;
import java.security.NoSuchAlgorithmException;

import javax.crypto.*;


public class CipherServer
{
public static void main(String[] args) throws Exception
{
//Start socket server
int port = 8001;
ServerSocket s = new ServerSocket();
s.setReuseAddress(true);
s.bind(new InetSocketAddress(port));
Socket client = s.accept();

CipherServer server = new CipherServer();
server.decryptMessage(client.getInputStream());

s.close();
}


public void decryptMessage(InputStream inStream) throws IOException, NoSuchAlgorithmException
{
try {

//Create the Data input stream from the socket
DataInputStream in = new DataInputStream(inStream);

//Get the key
ObjectInputStream file = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
SecretKey key = (SecretKey) file.readObject();
System.out.println("Key Used: " + file.toString());
file.close();

//Initiate the cipher
//Cipher d = Cipher.getInstance("DES/ECB/PKCS5Padding");
Cipher d = Cipher.getInstance("DES/ECB/NoPadding");
d.init(Cipher.DECRYPT_MODE,key);

int len = in.readInt();
byte[] encryptedMsg = new byte[len];
in.readFully(encryptedMsg);

System.out.println("Server - Msg Length: " + len);
System.out.println("Server - Encrypted: " + asHex(encryptedMsg));


//String demsg = new String(d.doFinal(encryptedMsg), "UTF-8");
//System.out.println("Decrypted Message: " + demsg);
// -Print out the decrypt String to see if it matches the orignal message.
byte[] plainText = d.doFinal(encryptedMsg);
System.out.println("Decrypted Message: " + new String(plainText, "UTF-8"));


} catch (Exception e) {
e.printStackTrace();
}
}

//Function to make the bytes printable (hex format)
public static String asHex(byte buf[]) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
}

最佳答案

在创建 key 文件之前创建到服务器的套接字,并且服务器可以正确读取上次运行的 key 。

尝试将 Socket s = new Socket(host, port); 移至关闭 key 文件之后。

事件如下:

    Client                 Server
----------------- ------------------
Connect to server
Accept connection
Read key file
(block on receive)
Generate Key
Write Key File
Send message
Receive message
etc.

关于java - Java密码和socket流的三个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899078/

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