daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.crypto.spec.SecretKeySpec;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.io.PrintWriter;
  6. import java.math.BigInteger;
  7. import java.net.InetAddress;
  8. import java.net.Socket;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.Arrays;
  12. import java.util.Base64;
  13. import java.util.Random;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.spec.IvParameterSpec;
  16. import javax.crypto.spec.SecretKeySpec;
  17.  
  18. public class Client {
  19.  
  20.     private int port;
  21.  
  22.     private BigInteger p;
  23.     private BigInteger g;
  24.     private BigInteger secret;
  25.  
  26.     private static BufferedReader in = null;
  27.     private static PrintWriter out = null;
  28.  
  29.     public Client(int port, BigInteger p, BigInteger g, BigInteger secret){
  30.  
  31.         this.port = port;
  32.         this.p = p;
  33.         this.g = g;
  34.         this.secret = secret;
  35.     }
  36.  
  37.     public void run(){
  38.  
  39.         for(;;){
  40.  
  41.             try{
  42.  
  43.                 InetAddress addr = InetAddress.getByName("127.0.0.1") ;
  44.  
  45.                 Socket socket = new Socket(addr, port);
  46.  
  47.                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  48.                 out = new PrintWriter(new java.io.BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  49.  
  50.                 BigInteger diffHellA = g.modPow(secret, p);
  51.                 System.out.println("Computed A Value: " + diffHellA);
  52.  
  53.                 out.println("**DHA**" + diffHellA + "****");
  54.  
  55.                 String diffHellBString = in.readLine().trim();
  56.  
  57.                 BigInteger diffHellB = new BigInteger(
  58.                         diffHellBString.substring(7, diffHellBString.length() - 4));
  59.  
  60.                 BigInteger dHKey = diffHellB.modPow(secret, p);
  61.                 System.out.println("Shared Diffie Hellman key computed: " + dHKey);
  62.  
  63.                 String nonce = generateRandomString(4);
  64.  
  65.                 String dhPadded = String.format("%012d", dHKey);
  66.  
  67.                 String base = nonce + dhPadded;
  68.  
  69.                 byte[] aesBytes = sha256(base);
  70.                 byte[] newArray = Arrays.copyOfRange(aesBytes, 0, 16);
  71.                 System.out.println("128 bit key: " + Base64.getEncoder().encodeToString(newArray));
  72.                 SecretKeySpec aesKey = new SecretKeySpec(newArray, "AES");
  73.  
  74.                 out.println("**NONCE**" + nonce + "****");
  75.                 out.println("**REQ****");
  76.  
  77.                 String response = in.readLine().trim();
  78.                 String encryptedHash = response.substring(7, response.length() - 4);
  79.  
  80.                 String file = decrypt(encryptedHash, base.getBytes(), aesKey);
  81.                 String fileTrim = file.substring(5, file.length());
  82.  
  83.                 String fileHash = Base64.getEncoder().encodeToString(sha256(fileTrim));
  84.  
  85.                 byte[] encryptedFileHash = encrypt(fileHash, base.getBytes(), aesKey);
  86.  
  87.                 out.println("**VERIFY**" + fileHash + "****");
  88.  
  89.                 System.out.println(new String(encryptedFileHash).length());
  90.  
  91.                 String verified = in.readLine().trim();
  92.                 System.out.println(verified);
  93.  
  94.                 socket.close();
  95.                 break;
  96.  
  97.             }catch(Exception e){
  98.  
  99.                 e.printStackTrace(System.out);
  100.             }
  101.         }
  102.     }
  103.  
  104.     public byte[] sha256(String key) throws NoSuchAlgorithmException{
  105.  
  106.         MessageDigest md = MessageDigest.getInstance("MD5");
  107.  
  108.         md.update(key.getBytes());
  109.         return md.digest();
  110.     }
  111.  
  112.     public static String generateRandomString(int len){
  113.  
  114.         String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  115.         Random random = new Random();
  116.  
  117.         StringBuilder sb = new StringBuilder();
  118.  
  119.         alphabet.charAt(random.nextInt(alphabet.length()));
  120.  
  121.         for (int i = 0; i < len; i++) {
  122.             sb.append(alphabet.charAt(random.nextInt(alphabet.length())));
  123.         }
  124.  
  125.         return sb.toString();
  126.     }
  127.  
  128.     public static String decrypt(String encrypted, byte[] bytesIv, SecretKeySpec key){
  129.  
  130.         try {
  131.  
  132.             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  133.             IvParameterSpec iv = new IvParameterSpec(bytesIv);
  134.             cipher.init(2, key, iv);
  135.             byte[] toDec = Base64.getDecoder().decode(encrypted);
  136.  
  137.             return new String(cipher.doFinal(toDec));
  138.         }
  139.         catch (Exception e) {
  140.  
  141.             System.out.println("Error while decrypting: " + e.toString());
  142.         }
  143.  
  144.         return null;
  145.     }
  146.  
  147.     public static byte[] encrypt(String plain, byte[] bytesIv, SecretKeySpec key){
  148.  
  149.         try {
  150.  
  151.             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  152.  
  153.             IvParameterSpec iv = new IvParameterSpec(bytesIv);
  154.             cipher.init(1, key, iv);
  155.  
  156.             return cipher.doFinal(plain.getBytes("UTF-8"));
  157.         }
  158.  
  159.         catch (Exception e) {
  160.  
  161.             System.out.println("Error while encrypting: " + e.toString());
  162.         }
  163.  
  164.         return null;
  165.     }
  166.  
  167.     public static void main(String[] args){
  168.  
  169.         int port = 8080;
  170.         BigInteger p = new BigInteger("53");
  171.         BigInteger g = new BigInteger("27");
  172.         BigInteger secret = new BigInteger("15");
  173.  
  174.  
  175.         Client c = new Client(port, p, g, secret);
  176.         c.run();
  177.     }
  178.  
  179. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top