· 9 years ago · Nov 06, 2016, 03:00 PM
1//package netseclab3part2;
2
3/**
4 *
5 * @author Mohammed
6 */
7// Reads text from a character-input stream, buffering characters so as to
8// provide for the efficient reading of characters, arrays, and lines.
9import java.io.BufferedReader;
10// An InputStreamReader is a bridge from byte streams to character streams:
11// It reads bytes and decodes them into characters using a specified charset.
12import java.io.InputStreamReader;
13// Prints formatted representations of objects to a text-output stream.
14// It implements all of the print methods found in PrintStream.
15import java.io.PrintWriter;
16import java.net.*;
17
18// Package java.security Provides the classes and interfaces for the security
19// framework.
20import java.security.InvalidKeyException;
21import java.security.NoSuchAlgorithmException;
22// Package javax.crypto Provides the classes and interfaces for cryptographic
23// operations.
24import javax.crypto.BadPaddingException;
25import javax.crypto.Cipher;
26import javax.crypto.IllegalBlockSizeException;
27import javax.crypto.KeyGenerator;
28import javax.crypto.NoSuchPaddingException;
29import javax.crypto.SecretKey;
30
31public class Client implements Runnable {
32
33 // Reads text from a character-input stream, buffering characters so as to
34 // provide for the efficient reading of characters, arrays, and lines.
35 BufferedReader br1, br2;
36 // Prints formatted representations of objects to a text-output stream.
37 // It implements all of the print methods found in PrintStream.
38 PrintWriter pr1;
39 // It is client socket (also called just "socket").
40 // A socket is an endpoint for communication between two machines.
41 Socket socket;
42 // A thread is a thread of execution in a program.
43 // The Java Virtual Machine allows an application to have multiple
44 // threads of execution running concurrently.
45 Thread t1, t2;
46 String in = "", out = "";
47
48 public Client() {
49 try {
50 // A thread is a thread of execution in a program.
51 // The Java Virtual Machine allows an application to have multiple
52 // threads of execution running concurrently.
53 t1 = new Thread(this);
54 t2 = new Thread(this);
55 // A socket is an endpoint for communication between two machines.
56 // It requires the name of the computer and the port number to
57 // which you want to connect.
58 // create a new socket object
59 socket = new Socket("localhost", 5000);
60 t1.start();;
61 t2.start();
62 // Each catch block is an exception handler that handles the type
63 // of exception indicated by its argument.
64 // The catch block contains code that is executed if and when the
65 // exception handler is invoked
66 } catch (Exception e) {
67 }
68 }
69
70 public void run() {
71
72 try {
73 // Create a DES Key.
74 // This class provides the functionality of a secret (symmetric)
75 // key generator.
76 // getInstance: Returns a KeyGenerator object that generates
77 // secret keys for the specified algorithm.
78 KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
79 // generateKey: Generates a secret key.
80 // Returns: the new key
81 SecretKey myDesKey = keygenerator.generateKey();
82 // class Cipher: This class provides the functionality of a
83 // cryptographic cipher for encryption and decryption.
84 Cipher desCipher;
85
86 // Create the cipher
87 // DES = Data Encryption Standard.
88 // ECB = Electronic Codebook mode.
89 // Electronic Codebook (ECB) encryption mode: The message is
90 // divided into blocks, and each block is encrypted separately.
91 // PKCS5Padding = PKCS #5-style padding.
92 // if the plaintext doesn't exactly fit, it is often necessary to
93 // supply additional letters to fill out the pattern. Using
94 // nonsense letters for this purpose has a side benefit of making
95 // some kinds of cryptanalysis more difficult.
96 desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
97
98 // Initialize the cipher for encryption
99 desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
100
101 // t2 is the thread that listens to what the user types and then
102 // prints it
103 if (Thread.currentThread() == t2) {
104 do {
105 // get stream of text from screen
106 br1 = new BufferedReader(new InputStreamReader(System.in));
107 pr1 = new PrintWriter(socket.getOutputStream(), true);
108 // read line from text
109 in = br1.readLine();
110 // print line to screen
111 pr1.println(in);
112 // stop if "END" was typed
113 } while (!in.equals("END"));
114 } else {
115 // t1 is the thread that listens to what the server says and
116 // prints it
117 do {
118 // get stream from socket
119 br2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
120 // read line from text
121 out = br2.readLine();
122 // print to screen
123 System.out.println("Server says : : : " + out);
124 // sensitive information
125 // Convert String into Byte[] array format.
126 byte[] text = out.getBytes();
127 System.out.println("Text [Byte Format] : " + text);
128 System.out.println("Text : " + new String(text));
129 // Encrypt the text
130 // Make Cipher in encrypt mode, and encrypt it with
131 // Cipher.doFinal() method.
132 byte[] textEncrypted = desCipher.doFinal(text);
133
134 System.out.println("Text Encryted : " + textEncrypted);
135
136 // Initialize the same cipher for decryption
137 desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
138
139 // Decrypt the text
140 // Make Cipher in decrypt mode, and decrypt it with
141 // Cipher.doFinal() method as well.
142 byte[] textDecrypted = desCipher.doFinal(textEncrypted);
143
144 System.out.println("Text Decryted : " + new String(textDecrypted));
145
146 // stop if "END" was typed
147 } while (!out.equals("END"));
148 }
149 } catch (Exception e) {
150 }
151
152 }
153
154 public static void main(String[] args) {
155 new Client();
156 }
157}