我正在編程基於客戶端 - 服務器的Java應用程序,我有一個問題,因爲它在客戶端和服務器中構建ObjectInputStream時掛起。ObjectInputStream與CipherInputStream凍結,掛起
客戶:
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 /輸出流。
DES.getEncryptCipher返回什麼樣的密碼?你能打印出算法和返回密碼的狀態嗎?這可能是因爲您需要一個執行填充的密碼,否則它可能正在等待N * [塊大小]字節完成計算。 *請注意,DES是不安全的,使用AES * – 2012-02-23 00:03:15
DES只是一個例子,我正在測試另一個類。主加密類是上面的,而不是DES。 – user1227115 2012-02-23 00:18:57
好的,至少對Sun/Oracle提供商來說,確實使用''AES/CBC/PKCS5Padding'作爲密碼,'AES'默認爲''AES/ECB/PKCS5Padding'「,而ECB是不安全的用於非隨機化的字節,如標題和對象。 – 2012-02-23 01:14:02