Hi,
I did TCP chat no problem and I attached cipher and It no run. What is wrong on my code.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.Security; import java.security.spec.AlgorithmParameterSpec; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.bouncycastle.jce.provider.BouncyCastleProvider; /** * * @author Lolek */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { String adresaIP; if(args.length==0) { adresaIP="127.0.0.1"; } else { adresaIP=args[0]; } PrijemVlakno prijimat = new PrijemVlakno(); OdeslatVlakno odeslat = new OdeslatVlakno(adresaIP); prijimat.server.start(); odeslat.klient.start(); } } class PrijemVlakno implements Runnable { Thread server; public PrijemVlakno() { server = new Thread(this,"Server vlakno"); } public void run() { try { Security.addProvider(new BouncyCastleProvider()); SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "AES"); AlgorithmParameterSpec iv = new IvParameterSpec("4567891234567AAA".getBytes("UTF-8")); String nameAlgorithm = "AES/ECB/PKCS7Padding"; Cipher decryption = Cipher.getInstance(nameAlgorithm); decryption.init(Cipher.DECRYPT_MODE, key); ServerSocket serverSocket = new ServerSocket(4000); Socket clientSocket = null; clientSocket = serverSocket.accept(); while(true) { CipherInputStream cip = new CipherInputStream(clientSocket.getInputStream(),decryption); BufferedReader cteni = new BufferedReader(new InputStreamReader(cip)); String radek = null; while((radek = cteni.readLine())!=null) { System.out.println(radek); System.out.flush(); } } } catch (InvalidKeyException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } } } class OdeslatVlakno implements Runnable { private String adresaIP; Thread klient; private Socket socket; public OdeslatVlakno(String ip) { this.adresaIP=ip; klient = new Thread(this,"Klient Vlakno"); } public void run() { try { try{ while (true) { try { socket = new Socket(this.adresaIP, 5000); break; } catch (IOException e) { Thread.sleep(1000); } } } catch (InterruptedException e) { return; } Security.addProvider(new BouncyCastleProvider()); SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "AES"); AlgorithmParameterSpec iv = new IvParameterSpec("4567891234567AAA".getBytes("UTF-8")); String nameAlgorithm = "AES/ECB/PKCS7Padding"; Cipher cipher = Cipher.getInstance(nameAlgorithm); cipher.init(Cipher.ENCRYPT_MODE, key); BufferedReader klavesnice = new BufferedReader(new InputStreamReader(System.in)); OutputStream vystup = socket.getOutputStream(); CipherOutputStream cip = new CipherOutputStream(vystup,cipher); BufferedWriter zapis = new BufferedWriter(new OutputStreamWriter(cip)); String radek; while ((radek = klavesnice.readLine())!=null) { zapis.write(radek); zapis.newLine(); zapis.flush(); System.out.println("Cteni radku:"+radek); } } catch (InvalidKeyException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } } }