Clients-Server chat with AES encryption in java -


i'm trying make chat program in java encrypted messages(you can send encrypted message , when receive message have decrypt it). found code decryption , encryption:

import java.security.key; import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; import sun.misc.*; import java.io.bufferedreader; import java.io.filereader;  public class test { private static string algorithm = "aes/cbc/pkcs5padding"; private static byte[] keyvalue=new byte[]      {'0','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};// key  // performs encryption public static string encrypt(string plaintext) throws exception {         key key = generatekey();         cipher chiper = cipher.getinstance(algorithm);         chiper.init(cipher.encrypt_mode, key);         byte[] encval = chiper.dofinal(plaintext.getbytes());         string encryptedvalue = new base64encoder().encode(encval);         return encryptedvalue; }  // performs decryption public static string decrypt(string encryptedtext) throws exception {         // generate key         key key = generatekey();         cipher chiper = cipher.getinstance(algorithm);         chiper.init(cipher.decrypt_mode, key);         byte[] decordedvalue = new base64decoder().decodebuffer(encryptedtext);         byte[] decvalue = chiper.dofinal(decordedvalue);         string decryptedvalue = new string(decvalue);         return decryptedvalue; }  //generatekey() used generate secret key aes algorithm private static key generatekey() throws exception {         key key = new secretkeyspec(keyvalue, algorithm);         return key; } 

so encryption function works well:

public void send(string text) {     try {         test test = new test();         string textts = chat.aes.test.encrypt(text);         out.println(clientgui.username + ": " + textts);         out.flush();         client_gui.tf_message.settext("");     } catch (exception e) {         e.printstacktrace();     } } 

but can't receive message, think problem in key generation, never save key used encrypt message , trying decrypt new key, if problem in how can send key? code receive function:

public void receive() {     try {         if (input.hasnext()) {             string message = input.nextline();             string dmessage = chat.aes.test.decrypt(message);             system.out.println(dmessage);             if (message.contains("#?!")) {                 string temp1 = message.substring(3);                 temp1 = temp1.replace("[", "");                 temp1 = temp1.replace("]", "");                  string[] currentusers = temp1.split(", ");                  client_gui.jl_online.setlistdata(currentusers);             } else {                 client_gui.ta_conversation.append(message + "\n");             }         }     } catch (exception e) {         e.printstacktrace();     } } 


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -