简体   繁体   English

通过Java RMI发送加密对象

[英]Sending Crypto Objects via Java RMI

I'm using the Java RMI to send objects from a Client -> to a Server. 我正在使用Java RMI将对象从客户端->发送到服务器。 Primitive objects works fine(eg String etc.) Java objects from the crypto library, throws exception. 原始对象运行良好(例如String等),来自加密库的Java对象会引发异常。 I need these Java Objects to settle on a secret key in a DiffieHellman Key Exchange Exception here: 我需要这些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

Code

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

If they're not Serializable, you can't use them in remote methods. 如果它们不可Serializable,则不能在远程方法中使用它们。

You may however be able to get them as bytes, send the bytes, and reconstitute them from the bytes at the receiver. 但是,您可能能够以字节的形式获取它们,发送这些字节,并在接收器处从这些字节重新构造它们。 Have a look at the APIs for the crypto objects of interest. 看一下感兴趣的加密对象的API。 Crypto objects in Java mostly have getEncoded() methods, and factories or constructors that can build them from byte arrays. Java中的加密对象大多具有getEncoded()方法,以及可以从字节数组构建它们的工厂或构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM