我使用Java RMI从客户端发送对象 - >到服务器。 原始对象工作正常(例如字符串等) 来自加密库的Java对象抛出异常。 我需要这些Java对象来解决DiffieHellman Key Exchange中的密钥 例外情况:
Exception in thread "main" java.rmi.MarshalException: error marshalling arguments; nested exception is:
java.io.NotSerializableException: javax.crypto.spec.DHParameterSpec
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.DiffieExchange(Unknown Source)
at rmihello.Client.main(Client.java:51)
Caused by: java.io.NotSerializableException: javax.crypto.spec.DHParameterSpec
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at sun.rmi.server.UnicastRef.marshalValue(Unknown Source)
... 5 more
代码
//The Client
public class Client {
public static void main(String[] args) throws Exception {
HelloService lookup = (HelloService) Naming.lookup("rmi://localhost:5099/hello");
//Some crypto stuff
String username = console.next();
BigInteger p1024 = BigInteger.probablePrime(1024, new SecureRandom());
BigInteger g = BigInteger.valueOf(2);
DHParameterSpec dhParams = new DHParameterSpec(p1024,g);
//A Module I have that generates KeyPairs
KeyPair kp = DiffieHellmanModule.genDHKeyPair(dhParams);
PublicKey clientPubKey = kp.getPublic();
//@@@@@@@--FAILS--@@@@@@@
PublicKey serverPubKey = lookup.DiffieExchange(clientPubKey,dhParams,username);
}
}
//The interface for RMI
public interface HelloService extends Remote{
public PublicKey DiffieExchange(PublicKey clientPublicKey,
DHParameterSpec dhParams,String username) throws RemoteException;
}
//The server servant implementing the interface for RMI
public class HelloServant extends UnicastRemoteObject implements HelloService{
protected HelloServant() throws RemoteException {
super();
}
@Override
public PublicKey DiffieExchange(PublicKey clientPublicKey,
DHParameterSpec dhParams, String username) throws RemoteException {
KeyPair key = DiffieHellmanModule.genDHKeyPair(dhParams);
PublicKey serverPubKey = key.getPublic();
return serverPubKey;
}
}
//The Application server
public class ApplicationServer {
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
Registry registry = LocateRegistry.createRegistry(5099);
registry.rebind("hello", new HelloServant());
}
}
答案 0 :(得分:0)
如果他们不是Serializable,
,您就无法在远程方法中使用它们。
然而,您可以将它们作为字节,发送字节,并从接收器的字节重构它们。查看感兴趣的加密对象的API。 Java中的加密对象主要有getEncoded()
方法,以及可以从字节数组构建它们的工厂或构造函数。