Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.IOException;
- import java.net.*;
- import java.security.*;
- import java.security.spec.*;
- import javax.xml.bind.DatatypeConverter;
- import javax.crypto.*;
- import javax.crypto.spec.*;
- import java.awt.Color;
- import javax.swing.SwingConstants;
- public class LanChan extends JFrame {
- final int port = 1337;
- final int sendport = 1338;
- private MulticastSocket receive;
- private MulticastSocket send;
- private String nick, pass;
- private InetAddress group;
- private JPanel contentPane;
- private JTextField textField;
- private JLabel lblLanchanV;
- private JScrollPane scrollPane;
- private JTextArea textArea;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- LanChan frame = new LanChan();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- String encrypt(String str) {
- String encryptedValue = "";
- try {
- SecretKeyFactory f;
- f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- KeySpec ks = new PBEKeySpec(pass.toCharArray(), new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae }, 1024, 128);
- SecretKey s = f.generateSecret(ks);
- Key k = new SecretKeySpec(s.getEncoded(),"AES");
- Cipher c = Cipher.getInstance("AES");
- c.init(Cipher.ENCRYPT_MODE, k);
- byte[] encVal = c.doFinal(str.getBytes());
- encryptedValue = DatatypeConverter.printBase64Binary(encVal);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return encryptedValue;
- }
- String decrypt(String str) {
- String decryptedValue = "";
- try {
- SecretKeyFactory f;
- f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- KeySpec ks = new PBEKeySpec(pass.toCharArray(), new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae }, 1024, 128);
- SecretKey s = f.generateSecret(ks);
- Key k = new SecretKeySpec(s.getEncoded(),"AES");
- Cipher c = Cipher.getInstance("AES");
- c.init(Cipher.DECRYPT_MODE, k);
- byte[] encVal = new byte[0];
- try {
- encVal = c.doFinal(DatatypeConverter.parseBase64Binary(str));
- } catch(BadPaddingException e) {
- System.out.println("Packet from other channel received... Could not decrypt.");
- }
- decryptedValue = new String(encVal, "UTF-8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return decryptedValue;
- }
- void enter() {
- String input = textField.getText();
- textField.setText("");
- input = "<" + nick + "> " + input;
- input = encrypt(input);
- byte buffer[] = input.getBytes();
- DatagramPacket pack = new DatagramPacket(buffer, buffer.length, group, port);
- try {
- send.send(pack);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Create the frame.
- */
- public LanChan() {
- setTitle("LanChan");
- this.setMinimumSize(new Dimension(100, 100));
- nick = JOptionPane.showInputDialog(new JFrame("Nickname"), "Enter a username:");
- if (nick == null) {
- System.exit(1);
- }
- pass = JOptionPane.showInputDialog(new JFrame("Channel"), "Enter your channel:");
- if (pass == null) {
- System.exit(1);
- }
- String ip = "224.0.0.9";
- try {
- group = InetAddress.getByName(ip);
- receive = new MulticastSocket(port);
- send = new MulticastSocket(sendport);
- send.joinGroup(group);
- receive.joinGroup(group);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- new Thread(new Runnable() {
- @Override
- public void run() {
- while(true) {
- byte[] buf = new byte[1024];
- DatagramPacket p = new DatagramPacket(buf, buf.length);
- try {
- receive.receive(p);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String received = new String(p.getData());
- received = decrypt(received);
- JScrollBar vertical = scrollPane.getVerticalScrollBar();
- vertical.setValue( vertical.getMaximum() );
- textArea.append(received + "\n");
- }
- }
- }).start();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- textField = new JTextField();
- textField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- enter();
- }
- });
- contentPane.add(textField, BorderLayout.SOUTH);
- textField.setColumns(10);
- lblLanchanV = new JLabel("v0.2");
- lblLanchanV.setInheritsPopupMenu(false);
- lblLanchanV.setHorizontalAlignment(SwingConstants.TRAILING);
- contentPane.add(lblLanchanV, BorderLayout.NORTH);
- scrollPane = new JScrollPane();
- scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
- scrollPane.setAutoscrolls(true);
- contentPane.add(scrollPane, BorderLayout.CENTER);
- textArea = new JTextArea();
- textArea.setLineWrap(true);
- textArea.setAutoscrolls(false);
- textArea.setEditable(false);
- textArea.setBorder(null);
- textArea.setBackground(Color.LIGHT_GRAY);
- scrollPane.setViewportView(textArea);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement