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.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.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
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) {
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 nactenyText[] = new byte[256];
private byte sifrovanyText[] = new byte[256];
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 {
BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in));
while (true) {
Security.addProvider(new BouncyCastleProvider());
SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes(), "AES");
AlgorithmParameterSpec algoritmus = new IvParameterSpec("1234567891234567".getBytes());
String jmenoAlgoritmu = "AES/CBC/PKCS7Padding";
Cipher sifrovani = Cipher.getInstance(jmenoAlgoritmu);
sifrovani.init(Cipher.ENCRYPT_MODE, key, algoritmus);
String poRadcich = cteni.readLine();
nactenyText = poRadcich.getBytes();
sifrovanyText = sifrovani.doFinal(nactenyText);
System.out.println("Delka sifrovaneho:"+sifrovanyText.length);
if (poRadcich.equals("/q")) {
break;
}
DatagramPacket odesilanaData = new DatagramPacket(sifrovanyText, sifrovanyText.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 (InvalidAlgorithmParameterException 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 byte desifrovanyText[];
private byte text[]=new byte[256];
private byte desText[] ;
public PrijimatVlakno (DatagramSocket pomA)
{
this.klient=pomA;
}
@Override
public void run()
{
try {
while (true) {
Security.addProvider(new BouncyCastleProvider());
SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes(), "AES");
AlgorithmParameterSpec algoritmus = new IvParameterSpec("1234567891234567".getBytes());
String jmenoAlgoritmu = "AES/CBC/PKCS7Padding";
Cipher desifrovani = Cipher.getInstance(jmenoAlgoritmu);
desifrovani.init(Cipher.DECRYPT_MODE, key, algoritmus);
DatagramPacket prijimaci = new DatagramPacket(prijimana, prijimana.length);
klient.receive(prijimaci);
System.out.println("Prijmuta delka:"+prijimaci.getLength());
text = prijimaci.getData();
String sifrovanyText = new String(text,0,prijimaci.getLength());
desText = sifrovanyText.getBytes();
System.out.println("K desifrovani:"+desText.length);
desifrovanyText = desifrovani.doFinal(desText);
String vypis = new String(desifrovanyText, 0, desifrovanyText.length);
System.out.println("Prijaty text:"+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 (InvalidAlgorithmParameterException 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);
}
}
}