通过套接字的Java加密对象序列化
我正在尝试实现一种方法,该方法通过使用套接字ObjectInputStream&&ObjectOutputStream,使用对象序列化来提供数据加密 这里的目的是将所有内容简化为字节数据数组,这些字节数据将用作加密算法的输入 下面是我编写的一个非常粗糙的代码,它只是为了测试和了解事情是如何工作的:通过套接字的Java加密对象序列化,java,sockets,encryption,serialization,Java,Sockets,Encryption,Serialization,我正在尝试实现一种方法,该方法通过使用套接字ObjectInputStream&&ObjectOutputStream,使用对象序列化来提供数据加密 这里的目的是将所有内容简化为字节数据数组,这些字节数据将用作加密算法的输入 下面是我编写的一个非常粗糙的代码,它只是为了测试和了解事情是如何工作的: List<Byte> bytes=new LinkedList<>(); try ( ByteArrayOutputStream bos=new By
List<Byte> bytes=new LinkedList<>();
try (
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
InputStream finalInputStream=new InputStream() {
int counter=0;
@Override
public int read() throws IOException {
if (counter<bytes.size()) {
return bytes.get(counter++);
}
else return -1;
}
};
OutputStream finalOutputStream=new OutputStream() {
@Override
public void write(int b) throws IOException {
bytes.add((byte) b);
}
};
BufferedOutputStream bfos=new BufferedOutputStream(finalOutputStream);
BufferedInputStream bios=new BufferedInputStream(finalInputStream);
ObjectInputStream ois=new ObjectInputStream(bios);
){
oos.writeObject(new CryptoMain());
oos.flush();
bfos.write(bos.toByteArray());
CryptoMain obj=(CryptoMain)ois.readObject();
obj.printHello();
}
catch(Exception e) {
e.printStackTrace();
}
我怎样才能得到我想要的?还有别的办法吗
谢谢。您不需要这些 您不需要ByteArrayOutputStream和List以及本地InputStream类。你每件事都做了三次。你实际上不需要它们中的任何一个 有几种简单的解决方案: . TLS。
我在看密码流。但我需要首先序列化一个对象,然后对其使用加密,反之亦然。。。密码流使用字节数组数据,我应该再次使用bytearrayoutputstream吗?您只需要新的ObjectOutputStreamnew CipherOutputStreamsocket.getOutputStream、cipher或任何最终输出流。您根本不需要字节数组、列表或ByteArrayOutputStreams。
ObjectInputStream ois=new ObjectInputStream(bios);