gpt4 book ai didi

java - Java 中的 Socket 和 ObjectInputStream - Android

转载 作者:行者123 更新时间:2023-12-01 12:37:20 25 4
gpt4 key购买 nike

我在使用 Java 的 Cipher 类时遇到 Socket 和 ObjectInputStream 问题。我使用一个 Android 客户端,在 Socket 上写入一个 ObjectOutputStream,以及一个从同一个 Socket 读取此 ObjectInputStream 的 Java 客户端。这是客户端/服务器代码

客户端

[代码]

 public static void functionRegistration(String usr, String pwd) throws UnknownHostException, IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{

Socket socket = new Socket(SERVER_ADDRESS_STRING, PORT_NO);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

socket.setSoTimeout(DEFAULT_TIMEOUT);

if(!socket.isConnected()){
System.out.println("[!] [Client] Connection problem!");
socket.close();
return;
}

//Diffie-Hellman
BigInteger shared_key = DiffieHellmanExchangeClient(socket, br, bw);
byte[] hash = ObjectHash.getByteHashCode(shared_key, SECURE_HASH_TYPE.SHA384);

//Extract IV and cipherKey
byte[] IV = new byte[16];
byte[] cipherKey = new byte[32];

int i, limit;

for(i = 0; i < IV.length; i++)
IV[i] = hash[i];

limit = i;

for(; i < hash.length; i++)
cipherKey[i - limit] = hash[i];

//Send username
bw.write(usr);
bw.write("\r\n");
bw.flush();


ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

//Hash password
String passwordHash = new String(ObjectHash.getByteHashCode(pwd, SECURE_HASH_TYPE.SHA512));

//Cipher password
String encryptedPasswordHash = new String(cipherMessage(passwordHash, cipherKey));

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivparameters = new IvParameterSpec(IV);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(cipherKey, "AES"), ivparameters);


oos.writeObject(new SealedObject(encryptedPasswordHash, cipher));
oos.flush();


if(br.readLine().compareTo("ACK") == 0)
Log.d("ACK", "ACK_RECEIVED");

else
Log.d("ACK","Something was wrong");

br.close();
bw.close();
socket.close();
}

[\代码]

服务器

[代码]

  private void getRegistrationUser() throws IOException, InvalidKeyException, InvalidAlgorithmParameterException{
String username = br.readLine();

System.out.println("[+] [Server - Thread " + Thread.currentThread().getId() + "] Username received");

//SHA384 of shared key
byte[] hash = ObjectHash.getByteHashCode(shared_key, SECURE_HASH_TYPE.SHA384);

byte[] IV = new byte[16];
byte[] cipherKey = new byte[32];

int j, limit;

for(j = 0; j < IV.length; j++)
IV[j] = hash[j];

limit = j;

for(; j < hash.length; j++)
cipherKey[j - limit] = hash[j];

try{

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivparameters = new IvParameterSpec(IV);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(cipherKey, "AES"), ivparameters);

ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
String encryptedHashPassword = (String)((SealedObject)ois.readObject()).getObject(cipher);

String decryptedHashPassword = decipherMessage(encryptedHashPassword, cipherKey);

ois.close();

sendACK();

}
catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
}

[\代码]

代码中的 cipherMessage 和 decipherMessage 两个函数使用 Twofish Cipher 使用各自的 key 加密和解密数据

问题是:我在调试阶段注意到服务器在 newObjectInputStream 上阻塞,并且无法读取客户端写入的对象

如何解决我的问题?

最佳答案

您不能在同一个套接字上使用多个缓冲流。他们会互相窃取数据。一切都使用对象流。

关于java - Java 中的 Socket 和 ObjectInputStream - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469241/

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