-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
1223 lines (1037 loc) · 37.8 KB
/
Copy pathClient.java
File metadata and controls
1223 lines (1037 loc) · 37.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Integer;
import java.lang.Boolean;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
import java.nio.file.*;
import java.util.zip.*;//for zipping
import javax.crypto.*;//for crypto
import java.security.*;//for crypto
import java.security.spec.*;
import javax.crypto.spec.*;
//new bouncy castle libs
import org.bouncycastle.openpgp.PGPPrivateKey;//pgp crypto
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* Group members:
* Justin Dorman: DRMJUS001
* Zachary Bresler: BRSZAC002
* Chad Piha: PHXCHA001
* Emil Kenguerli: KNGEMI001
*/
public class Client {
private static final String SERVER_IP_ADDRESS = "localhost"; //default host address
private static String clientId;
private static String userName;
private static String hostName;
private static int portNumber = 60123; //default port number
private static DataOutputStream out;
private static DataInputStream in;
private static Socket clientSocket;
private static String message;
private static PGPSend pgpsend;
private static PGPReceive pgpreceive;
private static boolean sender;
public Client ()
{
socketSetup(); //Sets up the socket
Scanner keyboard = new Scanner(System.in);
while(true)
{
//prompts user to be sender ot receiver, then enter username
System.out.print("Do you wish to (s)end or (r)receive messages: ");
String answer = keyboard.nextLine();
System.out.print("Enter a username: ");
String username = keyboard.nextLine();
userName = username;
if(answer.equalsIgnoreCase("s")) //if sender client
{
sender = true;
pgpsend = new PGPSend(username); //creates PGP send instance - generates keys and certificate in constructor
break;
}
else if(answer.equalsIgnoreCase("r")) //if receiver client
{
sender = false;
pgpreceive = new PGPReceive(username); //create PGP receive instance - generates keys and certificate in constructor
break;
}
else { //invalid inout
System.out.println("Enter a valid response");
continue;
}
}
//Displays group interface - either create or join a chat room
groupInterface(keyboard);
//Displays chat interface - send or receive a message
while (!clientSocket.isClosed()){
System.out.println();
chatInterface(keyboard);
}
}
//sets up client socket to connect to the server on default host address and port number
public void socketSetup () {
try{
InetAddress host = InetAddress.getLocalHost();
hostName = host.getHostName();
clientSocket = new Socket(SERVER_IP_ADDRESS, portNumber);
out = new DataOutputStream(clientSocket.getOutputStream());
in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
System.out.println("Connecting to " + hostName + " on port " + portNumber);
System.out.println("Just connected to server: " + clientSocket.getRemoteSocketAddress());
System.out.println();
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Function for chat room interface - create or join a group
* Allows for input of username and password to create a new chat room or join an exisiting one
*/
public static void groupInterface(Scanner keyboard){
boolean valid = false;
while(!valid){
System.out.print("Do you wish to (c)reate or (j)oin a chat room: ");
String groupInputStatus = keyboard.nextLine();
if(groupInputStatus.equalsIgnoreCase("c")){
System.out.print("Chat room name: ");
String groupName = keyboard.nextLine();
System.out.print("Password: ");
String password = keyboard.nextLine();
valid = sendGroupName(groupName, password, true);
if(valid){
break;
}else{
System.out.println("Group already exists");
}
}else if(groupInputStatus.equalsIgnoreCase("j")){
System.out.print("Group name: ");
String groupName = keyboard.nextLine();
System.out.print("Password: ");
String password = keyboard.nextLine();
valid = sendGroupName(groupName, password, false);
if(valid){
break;
}else{
System.out.println("Incorrect group name or password");
}
}else{
System.out.println("Enter a valid response");
}
}
}
/**
* Function for chat interface
* Calls the appropriate pgp process procedures in the relevant PGP instance (send or receive)
*/
public static void chatInterface(Scanner keyboard){
System.out.println();
if(sender) //if sender client
{
System.out.println("Please enter a message to encrypt(/q to quit):");
String message = keyboard.nextLine();
if(message.equals("/q"))
{
quit();
}
else
{
// Procedures involved with sending a secure message
String plaintext = userName + ": " + message;
byte[] hash = pgpsend.generateHash(plaintext);
byte[] encryptedHash = pgpsend.encryptHash(hash);
byte[] authMessage = pgpsend.authenticatePlaintext(encryptedHash, plaintext);
byte[] compMessage = pgpsend.compressMessage(authMessage);
pgpsend.generateSecretKey();
byte[] ciphertext = pgpsend.generateCiphertext(compMessage);
pgpsend.generateIV();
pgpsend.getKUS();
byte[] encryptedKey = pgpsend.encryptSecretKey();
byte[] finCiphertext = pgpsend.generateFinalCiphertext(encryptedKey, ciphertext);
sendMessage(finCiphertext);
}
}
else //if receiver client
{
System.out.println("Hit enter when ready to receive a message (/q to quit)");
String selectedAction = keyboard.nextLine();
if(selectedAction.equals("/q"))
{
quit();
}
else
{
//check authenticity of senders certificate/public key
//if validated, unpack message, else, cease communication
if(pgpreceive.validateCertificate())
{
//procedures involved with receving a secure message
byte[] recEncryptedMessage = receiveMessage();
if(recEncryptedMessage == null) System.out.print("No messages");
else
{
byte[] encryptedKey = pgpreceive.getEncrptedKeyPart(recEncryptedMessage);
byte[] encPayload = pgpreceive.getEncryptedMessagePart(recEncryptedMessage);
pgpreceive.decryptSharedKey(encryptedKey);
byte[] compPayload = pgpreceive.decryptMessage(encPayload);
byte[] payload = pgpreceive.decompressMessage(compPayload);
byte[] signature = pgpreceive.getSignaturePart(payload);
byte[] plaintext = pgpreceive.getPlaintext(payload);
byte[] plaintextHash = pgpreceive.generatePlaintextHash(plaintext);
pgpreceive.getKUC();
byte[] recMessageHash = pgpreceive.decryptHash(signature);
pgpreceive.authenticate(recMessageHash, plaintextHash);
}
}
else //untrusted sender
{
System.out.print("Sender is not trusted, communication will be ceased");
quit();
}
}
}
}
/**
* Function for sending a message in byte[] form
*/
public static void sendMessage(byte[] finCipherText){
try{
out.writeUTF("client_out_message --true");
out.writeInt(finCipherText.length);
out.write(finCipherText);
}catch(IOException e){
System.out.println(e);
}
}
/**
* Function for receiving a message in byte[] form
*/
public static byte[] receiveMessage(){
try{
while(in.available() > 0){
String receivedMessage = in.readUTF();
switch(receivedMessage){
case "client_in_message --true":
byte[] message = null;
int msgLength = in.readInt();
if (msgLength >0)
{
message = new byte[msgLength];
in.readFully(message, 0, message.length);
}
return message;
}
}
}catch(IOException e){
System.out.println(e);
}
return null;
}
/**
* Function for setting up or joining chat room
*/
public static boolean sendGroupName(String groupName, String password, boolean newGroup){
try{
if(newGroup){
out.writeUTF("set_group_name --new");
out.writeUTF(groupName);
out.writeUTF(password);
String response = in.readUTF();
return Boolean.parseBoolean(response);
}else{
out.writeUTF("set_group_name --existing");
out.writeUTF(groupName);
out.writeUTF(password);
String response = in.readUTF();
return Boolean.parseBoolean(response);
}
}catch(IOException e){
System.out.println(e);
return false;
}
}
public static String getClientId(){
return clientId;
}
//function for closing connection
public static void quit(){
try{
clientSocket.close();
System.out.println("Connection is now closed");
}catch(IOException e){
System.out.println(e);
}
}
public static void main(String[] args)
{
Client c = new Client();
}
}
class PGPSend {
private String plaintext;
private BufferedReader inputLine = null;
private boolean closed = false; //Volatile variable?
private PrivateKey KRC; //senders private key
private PublicKey KUC; //senders public key
private SecretKey secretKey;
private SecretKeySpec k;
private PublicKey KUS;//receivers public key
private Cipher aescipher;
private byte[] finCiphertext;
private PrivateKey caPrivateKey;
/**
* Constructor that generates senders public and private key pair and certificate
*/
public PGPSend (String username)
{
generateKeys();
generateCertificate(username);
}
/**
* Generate the public/private key pair for the sender (KRC, KUC)
*/
public void generateKeys () {
try {
//create private and public keys for sender
System.out.println("\nCREATING SENDER PRIVATE AND PUBLIC KEY PAIR:");
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair keys = keyGen.generateKeyPair();
//get the key from the generator
KRC = keys.getPrivate();
KUC = keys.getPublic();
//convert to bytes
byte[] KUCArray = KUC.getEncoded();
//write the public key to a file
System.out.println("\tWriting sender's Public Key to file \"sender_public_key.txt\" \n");
FileOutputStream fos = new FileOutputStream("sender_public_key.txt");
fos.write(KUCArray);
fos.close();
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Generate the sender certificate - write to file
* Following this, generate the sender signed certificate - write to file
*/
public void generateCertificate(String username)
{
try {
System.out.println("\nCREATING SENDER CERTIFICATE:");
//convert username to bytes
byte[] identityArray = username.getBytes();
//convert sender public key to bytes
byte[] KUCArray = KUC.getEncoded();
System.out.println("\tGenerating certificate (sender username concatenated with public key)");
//Concat bytes to create certificate
byte[] certificate = new byte[identityArray.length + KUCArray.length];
System.arraycopy(identityArray, 0, certificate, 0, identityArray.length);
System.arraycopy(KUCArray, 0, certificate, identityArray.length, KUCArray.length);
System.out.println("\tWriting certificate to file \"sender_certificate.txt\"\n");
FileOutputStream fos = new FileOutputStream("sender_certificate.txt");
fos.write(certificate);
fos.close();
System.out.println("\nCREATING SENDER SIGNED CERTIFICATE:");
//sign certificate by generating hash of certificate and signing with CA private key
byte[] hash = null;
byte[] signedCertificate = null;
System.out.println("\tHashing certificate");
try {
//CREATE A HASH OF THE CERTIFICATE
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(certificate);
hash = md.digest();
int mdValue = 0;
for (int i = 0; i < hash.length; i++){
mdValue += hash[i];
}
}
catch (Exception e) {
System.err.println(e);
}
//get CA private key from textfile
try {
System.out.println("\tReading in CA private key from file \"CA_private_key.txt\"");
Path path = Paths.get("CA_private_key.txt");
byte[] pKey = Files.readAllBytes(path);
/* Generate private key. */
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
caPrivateKey = kf.generatePrivate(ks);
}
catch (Exception e) {
System.err.println(e);
}
System.out.println("\tSigning hash with CA private key");
try {
Cipher RSAcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
RSAcipher.init(Cipher.ENCRYPT_MODE, caPrivateKey);
signedCertificate = RSAcipher.doFinal(hash);
}
catch (Exception e) {
System.err.println(e);
}
//write signed certificate to file
System.out.println("\tWriting signed certificate to file \"signed_certificate.txt\" \n");
FileOutputStream fosSC = new FileOutputStream("signed_certificate.txt");
fosSC.write(signedCertificate);
fosSC.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
/*
Generate Message digest
*/
/**
* Generates message digest (hash) of Plaintext
* @param String Plaintext plaintext to be encrypted
* @return byte[] hash hash of plaintext (using SHA-256)
*/
public byte[] generateHash (String plaintext) {
System.out.println("_SETTING UP AUTHENTICATION:_");
byte[] hash = null;
byte[] signedPlaintext = null;
try {
//CREATE A HASH OF THE MESSAGE
System.out.println("\n\tCREATING MESSAGE DIGEST");
System.out.println("\t\tPlaintext: " + plaintext);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(plaintext.getBytes("UTF-8"));
hash = md.digest();
System.out.println("\t\tMessage Digest Size: " + hash.length);
System.out.println("\t\tMessage Digest: [" + new String(hash)+"]");
}
catch (Exception e) {
System.err.println(e);
}
return hash;
}
/**
* Generates the digital signature by encrypting the hash with KRC (for authenticity)
* @param byte[] hash hash of plaintext (using SHA-256)
* @return byte[] encryptedHash the digital signature (usign RSA, ECB with PKCS1Padding)
*/
public byte[] encryptHash (byte[] hash) {
System.out.println("\n\tSIGNING HASH WITH SENDER'S PRIVATE KEY:");
System.out.println("\t\tEncrypting Hash with Private Key");
//sign hash with private key
byte[] encryptedHash = null;
try {
Cipher RSAcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
RSAcipher.init(Cipher.ENCRYPT_MODE, KRC);
encryptedHash = RSAcipher.doFinal(hash);
System.out.println("\t\tEncrypted Hash Size: " + encryptedHash.length);
System.out.println("\t\tEncrypted Hash: [" + new String (encryptedHash)+"]");
}
catch (Exception e) {
System.err.println(e);
}
return encryptedHash;
}
/**
* Concatentates the digital signature to the plaintext (for authentication)
* @param byte[] encryptedHash the digital signature (usign RSA, ECB with PKCS1Padding)
* @param String plaintext the plaintext to be Encrypted
* @return byte[] authMessage the concatentated payload to be encrypted for confidentiality
*/
public byte[] authenticatePlaintext (byte[] encryptedHash, String plaintext) {
System.out.println("\n\tCONCATENATING SIGNATURE AND MESSAGE:");
byte[] authMessage = null;
try {
//concantenate hash and original message
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
//add signature
outputStream.write(encryptedHash);
//add message
outputStream.write(plaintext.getBytes("UTF-8"));
//concat
authMessage = outputStream.toByteArray();
outputStream.close();
System.out.println("\t\tAuthenticated Packet Size: " + authMessage.length);
System.out.println("\t\tAuthenticated Packet: [" + new String(authMessage) + "]");
}
catch (Exception e) {
System.err.println(e);
}
return authMessage;
}
/**
* Compresses the payload for encryption
* @param byte[] authMessage the payload to be encrypted (Digital Signature + Plaintext)
* @return byte[] compMessage the compressed payload to be encrypted
*/
public byte[] compressMessage (byte[] authMessage) {
System.out.println("\n\tCOMPRESSING AUTHENTICATED PACKET:");
byte[] compMessage = null;
try {
//zip the above
//using chunks of 1024 bytes
byte[] output = new byte[1024];
//create defalter
Deflater compress = new Deflater();
compress.setInput(authMessage);
//use byte array to avoid running out of space
ByteArrayOutputStream o = new ByteArrayOutputStream(authMessage.length);
compress.finish();
//create zip
while(!compress.finished()){
int count = compress.deflate(output);
o.write(output,0,count);
}
o.close();
compress.end();
//zipped message
compMessage = o.toByteArray();
System.out.println("\t\tCompressed Packet Size: " + compMessage.length);
System.out.println("\t\tCompressed Packet: [" + new String(compMessage) + "]");
System.out.println("\n_AUTHENTICATION COMPLETE_");
}
catch (Exception e) {
System.err.println(e);
}
return compMessage;
}
/**
* Generates the secret/shared key Ks to be used by the receiver client to decrypt the payload
*/
public void generateSecretKey () {
try {
System.out.println("\n\n_SETTING UP CONFIDENTIALITY_");
//create shared key
System.out.println("\n\tCREATING SHARED KEY:");
KeyGenerator secretKeyGen = KeyGenerator.getInstance("AES");
secretKeyGen.init(128);
//GET KEY
secretKey = secretKeyGen.generateKey();
//new key spec
k = new SecretKeySpec(secretKey.getEncoded(), "AES");
System.out.println("\t\tShared Key Size: " + secretKey.getEncoded().length);
System.out.println("\t\tShared Key: [ " + new String(secretKey.getEncoded()) +"]");
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Encrypt the compressed payload
* @param byte[] compMessage the compressed payload to be encrypted
* @return byte[] ciphertext E_(Ks){Z(DS + P)}
*/
public byte[] generateCiphertext (byte[] compMessage) {
System.out.println("\n\tENCRYPTING COMPRESSED PACKET WITH SHARED KEY:");
//create cipher for encryption and encrypt zip
byte[] ciphertext = null;
try {
aescipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
aescipher.init(Cipher.ENCRYPT_MODE, k);
ciphertext = aescipher.doFinal(compMessage);
System.out.println("\t\tEncrypted Compressed Packet Size: " + ciphertext.length);
System.out.println("\t\tEncrypted Compressed Packet: [" + new String(ciphertext) +"]");
}
catch (Exception e) {
System.err.println(e);
}
return ciphertext;
}
/**
* Generates the initiation vector for CBC mode encryption and stores it for receiver to use when decrypting the compressed payload (DS + P)
*/
public void generateIV () {
try {
System.out.println("\t\tExtracting the IV for decryption");
//get iv from cipher
byte[] iv = aescipher.getIV();
System.out.println("\t\tIV size: " + iv.length);
System.out.println("\t\tIV: [" + new String(iv) +"]");
//write iv to a file
System.out.println("\t\tWriting IV to file \"sender_iv.txt\"");
FileOutputStream fos2 = new FileOutputStream("sender_iv.txt");
fos2.write(iv);
fos2.close();
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Acquire the receiver's public key KUS
*/
public void getKUS () {
System.out.println("\n\tENCRYPTING SHARED KEY WITH RECEIVER'S PUBLIC KEY:");
try {
// get receiver public key from file
System.out.println("\t\tReading in receiver's public key from file \"receiver_public_key.txt\"");
Path path = Paths.get("receiver_public_key.txt");
byte[] SKey = Files.readAllBytes(path);
//create receiver key from bytes
KUS = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(SKey));
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Encrypts the shared/secret key Ks
* @return byte[] encryptedKey E_(KUS){Ks}
*/
public byte[] encryptSecretKey () {
System.out.println("\t\tEncrypting shared key with Receiver's Public Key");
//encrypt shared key with public key of receiver
byte[] encryptedKey = null;
try {
Cipher packet = Cipher.getInstance("RSA/ECB/PKCS1Padding");
packet.init(Cipher.ENCRYPT_MODE, KUS);
encryptedKey = packet.doFinal(secretKey.getEncoded());
System.out.println("\t\tEncrypted Shared Key Size: " + encryptedKey.length);
System.out.println("\t\tEncrypted Shared Key: [" + new String(encryptedKey) +"]");
}
catch (Exception e) {
System.err.println(e);
}
return encryptedKey;
}
/**
*Concatenates the encrypted shared/secret key with the compressed and encrypted digital signature and plaintext
*@param byte[] encryptedKey E_(KUS){Ks}
*@param byte[] ciphertext encrypted + compressed payload E_(Ks){Z(DS + P)}
*@return byte[] finCiphertext the message to be sent to the receiver
*/
public byte[] generateFinalCiphertext (byte[] encryptedKey, byte[] ciphertext) {
System.out.println("\n\tCONCATENATING ENCRYPTED SHARED KEY AND ENCRYPTED PACKAGE:");
byte[] finCiphertext = null;
try {
//concat the encrypyted shared key and the encrypted zip
ByteArrayOutputStream finalMessage = new ByteArrayOutputStream( );
finalMessage.write(encryptedKey);
finalMessage.write(ciphertext);
finCiphertext = finalMessage.toByteArray();
finalMessage.close();
System.out.println("\t\tEncrypted Packet Size: " + finCiphertext.length);
System.out.println("\t\tEncrypted Packet: [" + new String(finCiphertext) +"]");
System.out.println("\n_CONFIDENTIALITY COMPLETE_");
}
catch (Exception e) {
System.err.println(e);
}
return finCiphertext;
}
public byte[] getFinalCipherText () {
return finCiphertext;
}
}
/**
* Handles the receiving and decryption of messages sent from the sender client
*/
class PGPReceive {
private PrivateKey KRS;//receiver private key
private PublicKey KUS;//receiver public key
private PublicKey KUC;//sender public key
private SecretKey secretKey = null;
private SecretKeySpec sk = null;
//Constructor that generates keys and certificate of receiver client
public PGPReceive(String username){
generateKeys();
generateCertificate(username);
}
/**
* Generates the receiver's public/private key pair (KRS, KUS)
*/
public void generateKeys() {
try {
KRS = null; //private key
//create receiver's assymmetric keys
System.out.println("\nCREATING RECEIVER PRIVATE AND PUBLIC KEY PAIR:");
KeyPairGenerator keyGen2 = KeyPairGenerator.getInstance("RSA");
keyGen2.initialize(1024);
KeyPair serverkeys = keyGen2.generateKeyPair();
//get keys
KRS = serverkeys.getPrivate();
KUS = serverkeys.getPublic();
//Write KUS to textfile receiver_public_key.txt
byte[] KUSArray = KUS.getEncoded();
System.out.println("\tWriting receiver's Public Key to file \"receiver_public_key.txt\" \n");
FileOutputStream fos = new FileOutputStream("receiver_public_key.txt");
fos.write(KUSArray);
fos.close();
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Gets the encrypted shared key (E_(KUS){Ks})
* @param byte[] message the message sent from sender
* @return byte[] keyPart the encrypted shared key
*/
public byte[] getEncrptedKeyPart(byte[] message) {
byte[] keyPart = new byte[128];
try {
//add provider
Security.addProvider(new BouncyCastleProvider());
//do crypto stuff here
System.out.println("\n\n_UNPACKING PACKET_");
System.out.println("\n\tSPLITTING UP RECEIVED PACKET:");
//split up packet
for(int i = 0; i < 128; i++){
keyPart[i] = message[i];
}
System.out.println("\t\tEncrypted Shared Key Size: " + keyPart.length);
System.out.println("\t\tEncrypted Shared Key: ["+ new String(keyPart) +"]");
}
catch (Exception e) {
System.err.println(e);
}
return keyPart;
}
/**
* Gets the ciphertext E_(Ks){Z(DS + P)} from message
* @param byte[] message the message sent from sender
* @return byte[] crypPart the encrypted and compressed payload
*/
public byte[] getEncryptedMessagePart(byte[] message) {
byte[] crypPart = new byte[message.length-128];
try {
//we know the encrypted key is 128bits
//rest is the encrypted message
for(int j = 128, k = 0; j < message.length; j++, k++){
crypPart[k] = message[j];
}
System.out.println("\t\tEncrypted Compressed Packet Size: " + crypPart.length);
System.out.println("\t\tEncrypted Compressed Packet: ["+ new String(crypPart) +"]" );
System.out.println("\n_PACKET UNPACKED_");
}
catch (Exception e) {
System.err.println(e);
}
return crypPart;
}
/**
* Decrypts the shared key using KRS
* @param byte[] keyPart E_(KUS){Ks} (ensuring confidentiality)
*/
public void decryptSharedKey (byte[] keyPart) {
try {
//CONFIDENTIALITY
System.out.println("\n\n_ENSURING CONFIDENTIALITY_");
System.out.println("\n\tDECRYPTING SHARED KEY:");
//decrypt shared key with the private key of the receiver
byte[] decryptedKey = null;
Cipher packet = Cipher.getInstance("RSA/ECB/PKCS1Padding");
packet.init(Cipher.DECRYPT_MODE, KRS);
decryptedKey = packet.doFinal(keyPart);
System.out.println("\t\tShared Key Size: " + decryptedKey.length);
System.out.println("\t\tShared Key: [" + new String(decryptedKey) + "]" );
//reconstruct shared key
secretKey = new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "AES");
sk = new SecretKeySpec(secretKey.getEncoded(), "AES");
System.out.println("\t\tShared Key constructed");
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Decrypts the ciphertext E_(Ks){Z(DS + P)} using Ks (ensuring confidentiality)
* @param byte[] crypPart the encrypted and compressed payload
* @return byte[] decryptedPackage Z(DS + P)
*/
public byte[] decryptMessage (byte[] crypPart) {
byte[] decryptedPackage = null;
try {
System.out.println("\n\tDECRYPTING COMPRESSED MESSAGE:");
//get iv for decryption
System.out.println("\t\tReading in IV from file \"sender_iv.txt\"");
Path path2 = Paths.get("sender_iv.txt");
byte[] iv = Files.readAllBytes(path2);
System.out.println("\t\tIV Size: " + iv.length);
System.out.println("\t\tIV: [" + new String(iv) + "]" );
//we decrypt the packet with the iv and the shared key
Cipher aescipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
aescipher.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(iv));
decryptedPackage = aescipher.doFinal(crypPart);
System.out.println("\t\tCompressed Packet Size: " + decryptedPackage.length);
System.out.println("\t\tCompressed Packet: [" + new String(decryptedPackage +"]"));
System.out.println("\n_CONFIDENTIALITY ENSURED_");
}
catch (Exception e) {
System.err.println(e);
}
return decryptedPackage;
}
/**
* Decompresses payload
* @param byte[] decryptedPackage Z(DS + P)
* @return byte[] authMessage DS + P (digital signature + plaintext)
*/
public byte[] decompressMessage (byte[] decryptedPackage) {
byte[] result = new byte[1024];
byte[] authMessage = null;
try {
//AUTHENTICAION
System.out.println("\n\n_ENSURING AUTHENTICITY_");
System.out.println("\n\tDECOMPRESSING PACKAGE:");
//create inflater
Inflater decompresser = new Inflater();
decompresser.setInput(decryptedPackage, 0, decryptedPackage.length);
//read out values
ByteArrayOutputStream o2 = new ByteArrayOutputStream(decryptedPackage.length);
while(!decompresser.finished()){
int count = decompresser.inflate(result);
o2.write(result,0,count);
}
o2.close();
authMessage = o2.toByteArray();
decompresser.end();
System.out.println("\t\tUncompressed Packet Size: " + authMessage.length);
System.out.println("\t\tUncompressed Packet: [" + new String(authMessage) +"]");
}
catch (Exception e) {
System.err.println(e);
}
return authMessage;
}
/**
* Gets the digital signature DS from the authenticated message (DS + P)
* @param byte[] authMessage DS + P (digital signature + plaintext)
* @return byte[] sigPart DS
*/
public byte[] getSignaturePart (byte[] authMessage) {
System.out.println("\n\tSPLITTING UNCOMPRESSED MESSAGE:");
//authMessage is decompressed message
byte[] sigPart = new byte[128];
try {
System.out.println("\t\tSplitting off signature");
//signature is 128 bytes as we encrypted with private key
for(int i = 0; i < 128; i++){
sigPart[i] = authMessage[i];
}
}
catch (Exception e) {
System.err.println(e);
}
return sigPart;
}
/**
* Gets the plaintext P from the authenticated message (DS + P)
* @param byte[] authMessage DS + P (digital signature + plaintext)
* @return byte[] plaintext P
*/
public byte[] getPlaintext (byte[] authMessage) {
byte[] plaintext = new byte[authMessage.length-128];
try {
System.out.println("\t\tSplitting off Plaintext");
//rest is the plain text
for(int j = 128, k = 0; j < authMessage.length; j++, k++){
plaintext[k] = authMessage[j];
}
//create message
System.out.println("\t\tReconstructing Plaintext");
System.out.println("\t\tPlaintext reads: ");
System.out.println("\t\t________________________________________________________");
System.out.println("\t\t" + new String(plaintext) );
System.out.println("\t\t________________________________________________________");
System.out.println("\t\tMessage End");