如何在RMI密封对象上传递密钥

时间:2016-12-05 23:11:50

标签: java rmi

您好,我正在尝试为我的rmi项目添加一些安全性,我们告诉他们使用密封对象。我只是想弄清楚如何使用密钥我如何去传递客户端密钥。这就是我到目前为止所拥有的

package Server;

import application.ClientInterface;

import javax.crypto.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by alexi on 05/12/2016.
 */
public class MyFileServer extends UnicastRemoteObject implements ServerInit, Serializable {

    private static Cipher ecipher;
    private static SecretKey key;


    public  MyFileServer() throws RemoteException {
        super();
    }

    @Override
    public void importFiles(ClientInterface n, String name) throws RemoteException {
        String videoPath = "src" + File.separator + "Videos" + File.separator + name;
        try {

            key = KeyGenerator.getInstance("DES").generateKey();

            ecipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);


            ServerInit server = new MyFileServer();

            File video = new File(videoPath);
            FileInputStream in=new FileInputStream(video);
            byte [] mydata=new byte[(int)video.length()];

            SealedObject sealed = new SealedObject(mydata, ecipher);

            int mylen=in.read(mydata);
            while(mylen>0){
                n.sendData(video.getName(), sealed, mylen);
                mylen=in.read(mydata);
            }

        }catch( Exception e){
            e.printStackTrace();
        }
    }

    public static void startServer(){
        try{


            ServerInit server = new MyFileServer();

            Registry registry = LocateRegistry.createRegistry(1099);
            registry.bind("videoServer", server);
            System.out.println(registry.lookup("videoServer"));

        }catch (RemoteException e){
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

客户端

@Override
    public boolean sendData(String filename, SealedObject sealed, int len) throws RemoteException {
        try{
//            System.out.println("Here" );
//            System.setProperty("java.security.policy", "file:./<policy>.policy");
//            if (System.getSecurityManager() == null) {
//                System.setSecurityManager(new SecurityManager());
//            }

            key = KeyGenerator.getInstance("DES").generateKey();
            dcipher = Cipher.getInstance("DES");
            dcipher.init(Cipher.DECRYPT_MODE, key);
            File f=new File(filename);
            f.createNewFile();
            FileOutputStream out=new FileOutputStream(f,true);
            //byte[] data = (byte[]) sealed.getObject(dcipher);
            System.out.println("Original Object: " + sealed );

            //out.write(data,0,len);
            out.flush();
            out.close();
            System.out.println("Done writing data...");
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

0 个答案:

没有答案