Results 1 to 3 of 3
Thread: TCP stream cypher
- 05-18-2009, 11:41 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
TCP stream cypher
Hallo,
I try do cypher in TCP. I did before UDP and I used block cypher AES and no problem. But now I need streamCypher and i don't know how can i do this things.
My code:
Java Code: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("11111111112222222222333333333344444444455555555566666666667777777777888888888899999999991111111111222222222233333333".getBytes("UTF-8"), "DES"); Cipher decryption = Cipher.getInstance("SKIPJACK/ECB/PKCS7Padding","BC"); 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 (NoSuchProviderException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } 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); } } }
- 05-20-2009, 08:22 AM #2
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
I rebuild this program. Aplication work but. Message doesn´t send and after recive. Messsage sed to another same program but different ports... I'm desperate. It the last one program what I have to do. Please help me. Thank you.
Java 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.InvalidAlgorithmParameterException; 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; CipherInputStream cis; public PrijemVlakno() { server = new Thread(this,"Server vlakno"); } public void run() { try { byte[] iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07,0x72,0x6F, 0x5A}; AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); Security.addProvider(new BouncyCastleProvider()); SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "Salsa20"); Cipher decryption = Cipher.getInstance("Salsa20"); decryption.init(Cipher.DECRYPT_MODE, key,paramSpec); ServerSocket serverSocket = new ServerSocket(4000); Socket clientSocket = null; clientSocket = serverSocket.accept(); while(true) { cis = new CipherInputStream(clientSocket.getInputStream(),decryption); BufferedReader cteni = new BufferedReader(new InputStreamReader(cis)); String radek = null; while((radek = cteni.readLine())!=null) { System.out.println(radek); System.out.flush(); } } } catch (InvalidAlgorithmParameterException ex) { Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex); } 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; } byte[] iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07,0x72,0x6F, 0x5A}; AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); Security.addProvider(new BouncyCastleProvider()); SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "Salsa20"); Cipher cipher = Cipher.getInstance("Salsa20"); 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 line; while ((line = klavesnice.readLine())!= null) { zapis.write(line); zapis.newLine(); zapis.flush(); System.out.println("Cteni radku:"+line); } } 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); } } }
- 05-20-2009, 10:08 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Cypher 1.1.8.2
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-24-2008, 06:36 PM -
Cypher 1.1.8 beta
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-19-2008, 05:07 PM -
Cypher 1.1.5 beta
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-10-2008, 04:24 PM -
Cypher 1.1.0 beta
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-05-2008, 04:30 PM -
Cypher 1.0.8 beta
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-03-2008, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks