gpt4 book ai didi

java - 在 Eneter 中对单个服务器和多个客户端之间的通信进行加密,每个客户端使用不同的 key

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

Eneter可以在DuplexTypedMessagesFactory中设置自定义序列化器,可用于加密客户端和服务器之间的通信。

DuplexTypedMessagesFactory sender_factory = new DuplexTypedMessagesFactory();
sender_factory.setSerializer(new EncryptingSerializer(client_private_key, server_public_key));
sender_ = sender_factory.createDuplexTypedMessageSender(MyResponse.class, MyRequest.class);

在我的应用程序中,我有单个服务器和多个客户端,但显然每个 channel 都应该使用自己的 key (或相关的 RSA key 对)单独加密。不幸的是,序列化器接口(interface)没有公开消息来源 channel 的任何标识符:

public class EncryptingSerializer implements ISerializer {
private RsaSerializer rsa_serializer_;

public EncryptingSerializer(RSAPrivateKey priv, RSAPublicKey pub) {
rsa_serializer_ = new RsaSerializer(pub, priv);
}

@Override
public <T> Object serialize(T t, Class<T> aClass) throws Exception {
return rsa_serializer_.serialize(t, aClass);
}

@Override
public <T> T deserialize(Object o, Class<T> aClass) throws Exception {
return rsa_serializer_.deserialize(o, aClass);
}
}

所以上面的代码对于客户端来说是可以的(似乎我什至可以延迟 rsa_serializer_ 的初始化,直到建立连接之后,因此例如在身份验证/配对过程中获取服务器公钥)。然而,对于服务器端,相同的序列化器对象需要服务多个通信 channel ,但每个 channel 都需要不同的 rsa_serializer_对象。

有什么办法可以解决这个问题,还是我只需要将加密推向更高的级别(发送和接收简单的 BlobRequest/BlobResponse 对象,其内容将在传递到框架之前进行加密,并在从框架获取之后进行解密)?

最佳答案

我最终在 DuplexStringMessageReceiver 之上重新实现了 DuplexTypedMessageReceiver,从外部按需获取和存储适当的序列化器:

class EncryptingTypedMessageReceiver<TResponse, TRequest> : IDuplexTypedMessageReceiver<TResponse, TRequest> {
public event EventHandler<TypedRequestReceivedEventArgs<TRequest>> MessageReceived;
public event EventHandler<ResponseReceiverEventArgs> ResponseReceiverConnected;
public event EventHandler<ResponseReceiverEventArgs> ResponseReceiverDisconnected;

private IDuplexStringMessageReceiver string_receiver_;
private Func<string, ISerializer> serializer_factory_;
private Dictionary<string, ISerializer> serializers_by_receiver_id_ = new Dictionary<string, ISerializer>();

public EncryptingTypedMessageReceiver(IDuplexStringMessageReceiver receiver, Func<string, ISerializer> serializer_factory) {
serializer_factory_ = serializer_factory;
string_receiver_ = receiver;
string_receiver_.RequestReceived += OnRequestReceived;
string_receiver_.ResponseReceiverConnected += OnResponseReceiverConnected;
string_receiver_.ResponseReceiverDisconnected += OnResponseReceiverDisconnected;
}

private void OnRequestReceived(object sender, StringRequestReceivedEventArgs e) {
TRequest typedRequest = serializers_by_receiver_id_[e.ResponseReceiverId].Deserialize<TRequest>(e.RequestMessage);
var typedE = new TypedRequestReceivedEventArgs<TRequest>(e.ResponseReceiverId, e.SenderAddress, typedRequest);
if (MessageReceived != null) {
MessageReceived(sender, typedE);
}
}

public void SendResponseMessage(string responseReceiverId, TResponse responseMessage) {
string stringMessage = (string)serializers_by_receiver_id_[responseReceiverId].Serialize(responseMessage);
string_receiver_.SendResponseMessage(responseReceiverId, stringMessage);
}

private void OnResponseReceiverConnected(object sender, ResponseReceiverEventArgs e) {
if (ResponseReceiverConnected != null)
ResponseReceiverConnected(sender, e);
serializers_by_receiver_id_.Add(e.ResponseReceiverId, serializer_factory_(e.ResponseReceiverId));
}

private void OnResponseReceiverDisconnected(object sender, ResponseReceiverEventArgs e) {
if (ResponseReceiverDisconnected != null)
ResponseReceiverDisconnected(sender, e);
serializers_by_receiver_id_.Remove(e.ResponseReceiverId);
}

public IDuplexInputChannel AttachedDuplexInputChannel {
get {
return string_receiver_.AttachedDuplexInputChannel;
}
}

public bool IsDuplexInputChannelAttached {
get {
return string_receiver_.IsDuplexInputChannelAttached;
}
}

public void AttachDuplexInputChannel(IDuplexInputChannel duplexInputChannel) {
string_receiver_.AttachDuplexInputChannel(duplexInputChannel);
}

public void DetachDuplexInputChannel() {
string_receiver_.DetachDuplexInputChannel();
}
}

关于java - 在 Eneter 中对单个服务器和多个客户端之间的通信进行加密,每个客户端使用不同的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966282/

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