· 9 years ago · Sep 01, 2016, 04:56 PM
1import java.security.KeyPairGenerator;
2import java.security.KeyPair;
3import java.security.NoSuchAlgorithmException;
4import java.security.PublicKey;
5import java.security.PrivateKey;
6import java.security.Signature;
7import java.io.*;
8import java.util.*;
9import java.util.concurrent.TimeUnit;
10import javax.crypto.Cipher;
11import javax.crypto.KeyGenerator;
12import javax.crypto.NoSuchPaddingException;
13import javax.crypto.SecretKey;
14import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;
15
16
17class buffer {
18 int n;
19 int maxsize = 5;
20 int currentsize = 0;
21 byte[] PlaceRequistion;
22 byte[] RequistionOrder;
23 //String Requistions [][] = new String [5][2];
24 boolean valueSet = false;
25 synchronized byte[] receive()
26 {
27 while(!valueSet && currentsize<=maxsize)
28 try
29 {
30 wait();
31 }
32 catch(InterruptedException e)
33 {
34 System.out.println("InterruptedException caught");
35 }
36 System.out.println("Got: " + n);
37 this.currentsize--;
38 valueSet = false;
39 notify();
40 return PlaceRequistion;
41 }
42 synchronized byte[] reply()
43 {
44 while(!valueSet && currentsize<=maxsize)
45 try
46 {
47 wait();
48 }
49 catch(InterruptedException e)
50 {
51 System.out.println("InterruptedException caught");
52 }
53 System.out.println("Got: " + n);
54 this.currentsize--;
55 valueSet = false;
56 notify();
57 return PlaceRequistion;
58 }
59 synchronized void send(byte[] Place_Requistion, byte[] Requistion_Order, int n)
60 {
61 while(valueSet && currentsize<=maxsize)
62 try
63 {
64 wait();
65 }
66 catch(InterruptedException e)
67 {
68 System.out.println("InterruptedException caught");
69 }
70 this.n = n;
71 this.currentsize++;
72 PlaceRequistion = Place_Requistion;
73 RequistionOrder = Requistion_Order;
74 valueSet = true;
75 System.out.println("Put: " + n);
76 notify();
77 }
78}
79
80
81
82class Global{
83 public static int couter = 0;
84 public static int sendcouter = 0;
85 public static int end = 0;
86 public static SecretKey Secret_Key;
87 public static Cipher desCipher;
88}
89
90
91class ENCRYPT//Encryptor
92{
93 static byte[] Encryptor(byte s[],Cipher c,SecretKey sk) throws Exception
94 {
95 byte[] ciphertext = ENCRYPTALG.EncryptionAlgorithm(s,c,sk);
96 return ciphertext;
97 }
98}
99
100class ENCRYPTALG //Encryption Algorithm
101{
102 static byte[] EncryptionAlgorithm(byte s[],Cipher c,SecretKey sk) throws Exception
103 {
104 c.init(Cipher.ENCRYPT_MODE, sk);
105 return c.doFinal(s);
106 }
107}
108
109
110
111class DECRYPT//Decryptor
112{
113 static byte[] Decryptor(byte s[],Cipher c,SecretKey sk) throws Exception //Decryptor
114 {
115 byte[] ct = DECRYPTAL.DecryptionAlgorithm(s,c,sk);
116 return ct;
117 }
118}
119
120class DECRYPTAL //Decryption Algorithm
121{
122 static byte[] DecryptionAlgorithm(byte s[],Cipher c,SecretKey sk) throws Exception
123 {
124 c.init(Cipher.DECRYPT_MODE, sk);
125 return c.doFinal(s);
126 }
127}
128
129
130
131public class Figure1
132{
133 public static void main(String[] unused) throws Exception
134 {
135 buffer buff1 = new buffer();
136 buffer buff2 = new buffer();
137 KeyGenerator keygen = KeyGenerator.getInstance("DES");
138 Global.Secret_Key = keygen.generateKey();
139 Global.desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
140
141 System.out.println("How many to send: ");
142 Scanner in = new Scanner(System.in);
143 int input = in.nextInt()+1;//Get input from the customer
144
145 aCustomerComponent Customer= new aCustomerComponent(buff1, input);
146 aSecuritySenderCoordinator SenderCoordinator = new aSecuritySenderCoordinator(buff1, input, buff2);
147 aSecurityReceiverCoordinator ReceiverCoordinator = new aSecurityReceiverCoordinator(buff2, Global.desCipher, Global.Secret_Key);
148
149 Customer.t_pro.join();
150 SenderCoordinator.t_SendCoordinator.join();
151 }
152}
153
154class aCustomerComponent implements Runnable//<<application component>> aCustomerComponent
155{
156 String PlaceRequisition, RequisitionOrder;
157 buffer b;
158 int supinput;
159 Thread t_pro;//pro to represent producer or suppler
160 aCustomerComponent(buffer b, int input)
161 {
162 supinput = input;
163 this.b = b;
164 t_pro = new Thread(this, "Customer");
165 t_pro.start();
166 }
167 public void run()
168 {
169 int i = 0;
170 while(b.n<supinput) //supinput is number user entered
171 {
172 i++;
173 PlaceRequisition = "Place Requisition "+i;
174 RequisitionOrder = "Requisition Order Number: "+i;
175
176 b.send(PlaceRequisition.getBytes(), RequisitionOrder.getBytes(), i);//Putting customer infor in buffer
177 }
178 }
179}
180
181
182class aSecuritySenderCoordinator implements Runnable
183{
184 buffer b1;
185 buffer b2;
186 Thread t_SendCoordinator;
187 int supinput;
188 byte[] Current_PlaceRequistion;
189 byte[] Current_RequisitionOrder;
190
191 public aSecuritySenderCoordinator(buffer buff1, int input, buffer buff2) throws Exception
192 {
193 supinput=input;
194 this.b1 = buff1;
195 this.b2=buff2;
196 t_SendCoordinator = new Thread(this, "SenderCoordinator");
197 t_SendCoordinator.start();
198 }
199 public void run()
200 {
201 while(b1.n<supinput)
202 {
203 //System.out.println("++++++++:"+b1.n+" and "+supinput);
204 Current_PlaceRequistion = b1.receive();
205 Current_RequisitionOrder = b1.RequistionOrder;
206 try {
207 Global.sendcouter++;
208
209 byte[] PlaceRequistion = ENCRYPT.Encryptor(Current_PlaceRequistion,Global.desCipher,Global.Secret_Key);
210 byte[] RequisitionOrder = ENCRYPT.Encryptor(Current_RequisitionOrder,Global.desCipher,Global.Secret_Key);
211
212 new aSynchronousMCWithReplySender(this.b2, PlaceRequistion, RequisitionOrder);
213
214
215
216 } catch (NoSuchAlgorithmException e) {
217 // TODO Auto-generated catch block
218 e.printStackTrace();
219 } catch (NoSuchPaddingException e) {
220 // TODO Auto-generated catch block
221 e.printStackTrace();
222 } catch (Exception e) {
223 // TODO Auto-generated catch block
224 e.printStackTrace();
225 }
226
227 }
228 }
229}
230
231class aSynchronousMCWithReplySender
232{
233 aSynchronousMCWithReplySender(buffer b2, byte[] PlaceRequistion, byte[] RequisitionOrder) throws Exception
234 {
235 Network send=new Network();
236 send.report(b2,PlaceRequistion,RequisitionOrder);
237 }
238}
239
240class Network
241{
242 public void report(buffer b2, byte[] PlaceRequistion, byte[] RequisitionOrder)throws Exception
243 {
244 new aSynchronousMCWithReplyReceiver(b2,PlaceRequistion,RequisitionOrder);
245 }
246}
247
248
249class aSynchronousMCWithReplyReceiver {
250
251 byte[] PlaceRequistion;
252 byte[] RequisitionOrder;
253 Cipher desCipher;
254 SecretKey Secret_Key;
255 buffer b2;
256
257 aSynchronousMCWithReplyReceiver( buffer buff2, byte []Place_Requistion, byte []Requisition_Order) throws Exception{
258 this.b2 = buff2;
259 PlaceRequistion = Place_Requistion;
260 RequisitionOrder = Requisition_Order;
261 desCipher = Global.desCipher;
262 Secret_Key = Global.Secret_Key;
263 Global.couter++;
264
265 b2.send(PlaceRequistion, RequisitionOrder, Global.couter); //Entering received data in to the buffer
266
267 new aSecurityReceiverCoordinator(b2, desCipher,Secret_Key);
268 }
269}
270
271
272class aSecurityReceiverCoordinator implements Runnable
273{
274 Thread aSecurityReceiverCoordinator;
275 buffer b2;
276 byte[] Place_Requistion;
277 byte[] Requisition_Order;
278 Cipher desCipher;
279 SecretKey Secret_Key;
280
281 public aSecurityReceiverCoordinator(buffer buff2)
282 {
283 this.b2 = buff2;
284 }
285 public aSecurityReceiverCoordinator(buffer buff2,
286 Cipher desCipher0, SecretKey desKey0) throws Exception
287 {
288 if(Global.couter>0)
289 {
290 this.b2 = buff2;
291 Place_Requistion=b2.receive();//Getting available messages from the buffer
292 Requisition_Order=b2.RequistionOrder;
293 desCipher = desCipher0;
294 Secret_Key = desKey0;
295 aSecurityReceiverCoordinator = new Thread(this, "SecurityReceiverCoordinator");
296 aSecurityReceiverCoordinator.start();
297 }
298 }
299 public void run()
300 {
301 try {
302 byte[] ct1 = DECRYPT.Decryptor(Place_Requistion,desCipher,Secret_Key); //ERROR HAPPENS HERE
303 String PlaceRequistion = new String(ct1);
304 byte[] ct2 = DECRYPT.Decryptor(Requisition_Order,desCipher,Secret_Key); //ERROR HAPPENS HERE
305 String RequisitionOrder = new String(ct2);
306
307
308 RequisitionSever send=new RequisitionSever();//To reply
309
310 send.SeverReceived(PlaceRequistion, RequisitionOrder);
311 } catch (Exception e) {
312 // TODO Auto-generated catch block
313 e.printStackTrace();
314 }
315 }
316}
317
318class RequisitionSever//<<application component>> RequisitionSever
319{
320 public void SeverReceived(String PlaceRequistion, String RequisitionOrder) throws Exception
321 {
322 System.out.println("Data after decryption is :"+PlaceRequistion+". "+RequisitionOrder);
323 String msg = "Reply form a Receiver: success";
324 }
325}