package aes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Lolek
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String hostname;
if (args.length==0)
{
hostname = "127.0.0.1";
}
else
{
hostname = args[0];
}
InetAddress ia = InetAddress.getByName(hostname);
OdeslatVlakno odeslat = new OdeslatVlakno(ia, 4000);
odeslat.start();
PrijimatVlakno prijimat = new PrijimatVlakno(odeslat.getSocket());
prijimat.start();
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class OdeslatVlakno extends Thread
{
private InetAddress server;
private DatagramSocket klient;
private int port;
private byte addPacket[]=new byte[3];
private String test="add";
private String key1 = "1234567891234567";
public OdeslatVlakno(InetAddress adresa,int port)
{
try {
this.server = adresa;
this.port = port;
this.klient = new DatagramSocket(5000);
this.klient.connect(adresa, port);
} catch (SocketException ex) {
Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
}
}
public DatagramSocket getSocket()
{
return this.klient;
}
@Override
public void run()
{ try {
byte[] key2 = key1.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(key2, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
//addPacket = test.getBytes();
//DatagramPacket testPacket = new DatagramPacket(addPacket, addPacket.length, server, port);
//.send(testPacket);
BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String poRadcich = cteni.readLine();
byte data[] = poRadcich.getBytes();
if (poRadcich.equals("/q")) {
break;
}
byte[] nactenaData = cipher.doFinal(data);
DatagramPacket odesilanaData = new DatagramPacket(nactenaData, nactenaData.length, server, port);
klient.send(odesilanaData);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
}
}
} 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);
}
}
}
class PrijimatVlakno extends Thread
{
DatagramSocket klient;
private byte[] prijimana = new byte[256];
private String key1 = "1234567891234567";
public PrijimatVlakno (DatagramSocket pomA)
{
this.klient=pomA;
}
@Override
public void run()
{
try {
byte[] key2 = key1.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(key2, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
while (true) {
try {
DatagramPacket prijimaci = new DatagramPacket(prijimana, prijimana.length);
klient.receive(prijimaci);
byte encrypt[]=prijimaci.getData();
byte dekodovani[]=cipher.doFinal(encrypt);
String vypis = new String(dekodovani,0,dekodovani.length);
System.out.println(vypis);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (InvalidKeyException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(PrijimatVlakno.class.getName()).log(Level.SEVERE, null, ex);
}
}
}