Skip to content
Snippets Groups Projects
Select Git revision
  • a4d7462361099a5b72efa22758cd76f50dcf2cee
  • 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

ApplicationMain.java

Blame
  • MySharedKey.java 1.32 KiB
    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);
            }
        }
    }