java - Exchange crypto key through sockets -


what making chat program encrypts/decrypts messages send. far i've established basic all-to-all communication clients , have put server-to-client simple encryption aes, have given them same key. want establish key exchange algorithm allow every client have own key. have read multiple algorithms, @ first wanted establish diffie-hellman found rsa examples made more sence. have:

public static void main(string[] args) throws nosuchalgorithmexception, nosuchpaddingexception,  invalidkeyexception, illegalblocksizeexception, badpaddingexception  {  keygenerator keygenerator = keygenerator.getinstance("aes"); keygenerator.init(128); key aeskey = keygenerator.generatekey();    keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa"); keypairgenerator.initialize(1024); keypair keypair = keypairgenerator.genkeypair();  cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding");  byte[] aeskeybytes = aeskey.getencoded(); system.out.println("1. aeskeybytes= "+ bytestohex(aeskeybytes));  cipher.init(cipher.encrypt_mode, keypair.getpublic()); byte[] ciphertext = cipher.dofinal(aeskeybytes); system.out.println("2. ciphertext= "+bytestohex(ciphertext));  cipher.init(cipher.decrypt_mode, keypair.getprivate()); byte[] decryptedkeybytes = cipher.dofinal(ciphertext); system.out.println("3. decryptedkeybytes= "+bytestohex(decryptedkeybytes));   //use symmetric decrypted key secretkey newaeskey = new secretkeyspec(decryptedkeybytes, "aes"); 

basically point code ends use secretkey initialize aes cipher , go on there. question how distribute keys rsa through sockets without losing bytes or whatever clients can have unique keypairs server. there way not use keygenerator , give own keys strings, since plan on using setters user can change his/her key on whish, otherwise what's point in using keyexchange if clients end having same keys in first place? , 1 last thing purely on curiosity, possible modify rsa dh, , how different code-wise? lastly "bytestohex" method follows , provided me teacher , after tested works fine there no problems there:

public static string bytestohex(byte[] data) {      if (data==null)         return null;     else     {         int len = data.length;          string str = "";          (int i=0; i<len; i++)         {             if ((data[i]&0xff)<16){                 str = str + "0" + java.lang.integer.tohexstring(data[i]&0xff);                             }                             else{                 str = str + java.lang.integer.tohexstring(data[i]&0xff);                             }         }          return str.touppercase();     } } 

i know there might answers "look @ this" , "this example", trust me i've looked @ of them got more confused. clear don't want use files store keys or that, want client , server send each other public keys along private key create secretkey, know dh fed right i'll take can have appreciated.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -