Skip to content
Snippets Groups Projects
Select Git revision
  • 48f09be2913eabb3dbbf6dee968160b62482fcfd
  • master default protected
  • hai
  • isaac
  • CheHao
  • Eldar
  • mpriymak
  • master-before-merging-with-hai
  • master-before-merging-with-isaac
  • rmi-working-before-merging-with-isaac
  • all-code-merged-by-hai-v1
11 results

EncryptionUpdate.java

Blame
  • Forked from Ho Dac Hai / COMP90015-DSAss2-InfinityMonkeys-remaster
    Source project has a limited visibility.
    EncryptionUpdate.java 2.23 KiB
    package client;
    
    import GUI.StartScreen;
    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;
    
        public SecretKey getSharedSecretKey() {
            return sharedSecretKey;
        }
    
        private SecretKey sharedSecretKey;
        private Cipher myCipher;
    
        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) {
                StartScreen.showErrorMessage("Shared key error");
                //e.printStackTrace();
            } catch (NoSuchProviderException e) {
                StartScreen.showErrorMessage("Shared key error");
                //e.printStackTrace();
            } catch (NoSuchPaddingException | InvalidKeyException e) {
                StartScreen.showErrorMessage("Shared key error");
                //e.printStackTrace();
            } catch (BadPaddingException e) {
                StartScreen.showErrorMessage("Shared key error");
                //e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                StartScreen.showErrorMessage("Shared key error");
                //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){
                StartScreen.showErrorMessage("Encryption Update error");
                //e.printStackTrace();
            }
        }
    }