Skip to content
Snippets Groups Projects
Commit ad7174e1 authored by Hai HoDac's avatar Hai HoDac
Browse files

Merged encryption function 2

parent 0a87bda0
Branches
Tags
No related merge requests found
package client;
import remote.IDrawingUpdate;
import remote.IEncryptionUpdate;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.security.*;
public class EncryptionUpdate extends UnicastRemoteObject implements IEncryptionUpdate, Serializable {
private PrivateKey priv;
private PublicKey pub;
private SecretKey sharedSecretKey;
public PublicKey getPub() {
return pub;
}
public void setSharedKey(byte[] encryptedSharedSecretKey){
try {
Cipher cipher = Cipher.getInstance("RSA", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, priv);
sharedSecretKey = new SecretKeySpec(cipher.doFinal(encryptedSharedSecretKey), "AES");
System.out.println("Client holds this shared key: "+sharedSecretKey);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (NoSuchPaddingException | InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
EncryptionUpdate() throws RemoteException {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SunJSSE");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
this.priv = pair.getPrivate();
this.pub = pair.getPublic();
}
catch (Exception e){
e.printStackTrace();
}
}
}
package remote;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.security.PublicKey;
public interface IEncryptionUpdate extends Remote {
public PublicKey getPub() throws RemoteException;
public void setSharedKey(byte[] encryptedSharedSecretKey) throws RemoteException;
}
package server;
import remote.IEncryptionUpdate;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.rmi.RemoteException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.Base64;
public class MySharedKey {
private byte[] encryptedSharedSecretKey;
private IEncryptionUpdate encryptionUpdate;
private SecretKey sharedSecretKey;
public MySharedKey (IEncryptionUpdate encryptionUpdate) throws RemoteException {
this.encryptionUpdate = encryptionUpdate;
this.sharedSecretKey = new SecretKeySpec(new byte[16], "AES");
System.out.println(sharedSecretKey);
this.encryptedSharedSecretKey = wrapKey(encryptionUpdate.getPub());
System.out.println("Shared key on server:" + encryptedSharedSecretKey);
encryptionUpdate.setSharedKey(encryptedSharedSecretKey);
}
private byte[] wrapKey(PublicKey clientPubKey){
try {
Cipher c = Cipher.getInstance("RSA", "SunJCE");
c.init(Cipher.WRAP_MODE, clientPubKey);
byte[] result2 = c.wrap(sharedSecretKey);
return result2;
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException(
e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment