0

我正在使用 Java RMI 将对象从客户端 -> 发送到服务器。原始对象工作正常(例如字符串等)来自加密库的 Java 对象,抛出异常。我需要这些 Java 对象来确定 DiffieHellman 密钥交换异常中的密钥:

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());
    }
}
4

1 回答 1

1

如果不是Serializable,,则不能在远程方法中使用它们。

但是,您可以将它们作为字节获取,发送字节,并从接收器的字节中重构它们。查看感兴趣的加密对象的 API。Java 中的加密对象大多具有getEncoded()可以从字节数组构建它们的方法、工厂或构造函数。

于 2014-11-10T00:19:49.767 回答