gpt4 book ai didi

java - ObjectInputStream 与 CipherInputStream 卡住、挂起

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:46 26 4
gpt4 key购买 nike

我正在编写基于客户端-服务器的 Java 应用程序,但遇到了一个问题,因为在客户端和服务器中构造 ObjectInputStream 时它会挂起。

客户:

Socket socket = new Socket("localhost", 9999);

outCiph = new CipherOutputStream(socket.getOutputStream(), AES.getEncryptCipher("key"));
out = new ObjectOutputStream(outCiph);
inCiph = new CipherInputStream(socket.getInputStream(), AES.getDecryptCipher("key"));
in = new ObjectInputStream(inCiph);

try
{
String text = "test!";

out.writeObject(text);
out.flush();

if (out != null)
out.close();

if (in != null)
in.close();
}
catch (IOException ex)
{
System.err.println(ex.toString());
}

服务器:

ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();

outCiph = new CipherOutputStream(socket.getOutputStream(), AES.getEncryptCipher("key"));
out = new ObjectOutputStream(outCiph);
inCiph = new CipherInputStream(socket.getInputStream(), AES.getDecryptCipher("key"));
in = new ObjectInputStream(inCiph);

try
{
String rec = (String) in.readObject();
System.out.println("Received from client: " + rec);

if (out != null)
out.close();

if (in != null)
in.close();

}
catch (IOException ex)
{
System.err.println(ex.toString() + " in start()");
}
catch (ClassNotFoundException ex)
{
System.err.println(ex.toString());
}

AES:

// I'm not author of generateKey method so I've no idea if is it correct
private static byte[] generateKey(String pass) throws UnsupportedEncodingException, NoSuchAlgorithmException
{
MessageDigest sha = MessageDigest.getInstance("SHA-256");
byte[] passBytes = pass.getBytes("ASCII");
byte[] sha256Bytes = sha.digest(passBytes);


byte[] key = new byte[16];
int j = 0;
for (int i = 0; i < sha256Bytes.length; i++)
{
if (i % 2 == 0)
{
key[j] = sha256Bytes[i];
j++;
}
}
return key;
}

public static Cipher getEncryptCipher(String pass)
{
try
{
SecretKeySpec skeySpec = new SecretKeySpec(generateKey(pass), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher;
}
catch (Exception ex) // just for clarity
{
Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}

public static Cipher getDecryptCipher(String pass)
{
try
{
SecretKeySpec skeySpec = new SecretKeySpec(generateKey(pass), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
return cipher;
}
catch (Exception ex) // just for clarity
{
Logger.getLogger(AES.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}

当我不使用 CipherInput/OutputStream 时,一切正常,因此问题在某种程度上与 CipherInput/OutputStream 有关。

最佳答案

只有在发送所有信息后才需要创建ObjectInputStream,因为ObjectInputStream的构造函数会因为需要读取 header 而阻塞。

通常情况下,所有字节都已由 ObjectOutputStream 写入,但现在 CipherOutputStream 正在等待,直到它拥有完整的 16 字节 block (在 AES 的情况下),然后才发送(最后一部分) header 。也许流密码模式(CTR 或 GCM)中的 AES 在这里会更有用,因为它使用每字节加密,并且能够直接发送每个字节。

关于java - ObjectInputStream 与 CipherInputStream 卡住、挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9405077/

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