Advertisement
Guest User

Untitled

a guest
Oct 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. package org.thatmadhacker.finlaychat;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.PrintWriter;
  6. import java.net.InetAddress;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.security.KeyPair;
  10. import java.security.PrivateKey;
  11. import java.security.PublicKey;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Scanner;
  15.  
  16. import javax.crypto.SecretKey;
  17.  
  18. import com.thatmadhacker.utils.crypto.algo.ASymetric;
  19. import com.thatmadhacker.utils.crypto.algo.Symetric;
  20. import com.thatmadhacker.utils.misc.BASE64;
  21.  
  22. public class FinlayChat {
  23.  
  24.     public static final File USER_FILE = new File("user.profile");
  25.    
  26.     public static final int PORT = 7183;
  27.    
  28.     public static List<InetAddress> clients = new ArrayList<InetAddress>();
  29.    
  30.     public static List<String> messages = new ArrayList<String>();
  31.    
  32.     @SuppressWarnings("resource")
  33.     public static void main(String[] args) throws Exception{
  34.        
  35.         Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  36.             @Override
  37.             public void run() {
  38.                 try {
  39.                     UPnP.removePortMapping(PORT);
  40.                 }catch(Exception e) {
  41.                     e.printStackTrace();
  42.                 }
  43.             }
  44.         }));
  45.         UPnP.mapPort(PORT);
  46.         new Thread(new Runnable() {
  47.             @Override
  48.             public void run() {
  49.                 try {
  50.                     ServerSocket ss = new ServerSocket(PORT);
  51.                     while(!ss.isClosed()) {
  52.                         try {
  53.                             Socket s = ss.accept();
  54.                             Scanner scan = new Scanner(s.getInputStream());
  55.                             if(!scan.nextLine().equals("UPDATE")) {
  56.                                 messages.add(scan.nextLine());
  57.                             }else {
  58.                                 PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  59.                                 for(String message : messages) {
  60.                                     out.println(message);
  61.                                 }
  62.                                 out.close();
  63.                             }
  64.                             scan.close();
  65.                         }catch(Exception e) {
  66.                            
  67.                         }
  68.                     }
  69.                     ss.close();
  70.                 }catch(Exception e) {
  71.                     e.printStackTrace();
  72.                     System.exit(1);
  73.                 }
  74.             }
  75.         }).start();
  76.         new Thread(new Runnable() {
  77.  
  78.             @Override
  79.             public void run() {
  80.                 while(true) {
  81.                     try {
  82.                         updateMessages();
  83.                        
  84.                         Thread.sleep(5000);
  85.                     }catch(Exception e) {
  86.                         e.printStackTrace();
  87.                     }
  88.                 }
  89.             }
  90.            
  91.         }).start();
  92.         Scanner in = new Scanner(System.in);
  93.         KeyPair pair;
  94.         if(USER_FILE.exists()) {
  95.             Scanner s = new Scanner(USER_FILE);
  96.             String priv = s.nextLine();
  97.             s.close();
  98.             System.out.println("Enter decryption password!");
  99.             String password = in.nextLine();
  100.             SecretKey key = Symetric.genKey(password,"test123", 128,"AES");
  101.             System.out.println("Attempting to decrypt private key!");
  102.             String decryptedPriv = "";
  103.             try {
  104.                 decryptedPriv = Symetric.decrypt(priv, key, "AES");
  105.                 System.out.println("Successfully decrypted private key!");
  106.             }catch(Exception e) {
  107.                 System.out.println("WRONG PASSWORD!");
  108.                 System.exit(1);
  109.             }
  110.             PrivateKey privKey = ASymetric.getPrivateKeyFromByteArray(BASE64.decode(decryptedPriv.replaceAll("&l", System.lineSeparator())), "RSA");
  111.             PublicKey pubKey = ASymetric.getPublicKeyFromRSAPrivateKey(privKey);
  112.             pair = new KeyPair(pubKey,privKey);
  113.            
  114.            
  115.            
  116.         }else {
  117.             USER_FILE.createNewFile();
  118.             System.out.println("Enter a passcode to encrypt your key with!");
  119.             String password = in.nextLine();
  120.             SecretKey key = Symetric.genKey(password,"test123", 128,"AES");
  121.            
  122.             pair = ASymetric.genKeys("RSA",2048);
  123.            
  124.             String privKey = BASE64.encode(pair.getPrivate().getEncoded()).replaceAll(System.lineSeparator(), "&l");
  125.            
  126.             privKey = Symetric.encrypt(privKey, key, "AES");
  127.            
  128.             PrintWriter out = new PrintWriter(new FileWriter(USER_FILE));
  129.             out.print(privKey);
  130.             out.close();
  131.         }
  132.        
  133.         String username = BASE64.encode(pair.getPublic().getEncoded()).replaceAll(System.lineSeparator(), "&l");
  134.        
  135.         updateMessages();
  136.        
  137.        
  138.     }
  139.    
  140.     public static void updateMessages() throws Exception {
  141.         for(InetAddress i : clients) {
  142.             Socket s = new Socket(i,PORT);
  143.             PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  144.             out.println("UPDATE");
  145.             messages.clear();
  146.             Scanner in = new Scanner(s.getInputStream());
  147.             String m = in.nextLine();
  148.             while(!m.equals("{END}")) {
  149.                 messages.add(m);
  150.                 m = in.nextLine();
  151.             }
  152.             in.close();
  153.             s.close();
  154.         }
  155.     }
  156.    
  157.     public static void sendMessage(String to, String from, KeyPair pair, String message) throws Exception{
  158.         PublicKey key = ASymetric.getPublicKeyFromByteArray(BASE64.decode(to.replaceAll("&l", System.lineSeparator())),"RSA");
  159.        
  160.         String encryptedMessage = ASymetric.encrypt(message, key, "RSA");
  161.        
  162.         String finalMessage = "";
  163.         finalMessage += "from:"+from+"&to:"+to;
  164.         String signedMessage = ASymetric.sign(encryptedMessage,pair.getPrivate(),"RSA");
  165.         finalMessage += "&message:"+signedMessage;
  166.        
  167.         transmit(finalMessage);
  168.        
  169.     }
  170.    
  171.     private static void transmit(String message) throws Exception{
  172.         for(InetAddress i : clients) {
  173.             Socket s = new Socket(i,PORT);
  174.             PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  175.             out.println("PUT");
  176.             out.println(message);
  177.             out.close();
  178.             s.close();
  179.         }
  180.     }
  181.    
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement