· 6 years ago · Feb 26, 2019, 10:20 AM
1 MAHAVEER
2INSTITUTE OF SCIENCE AND TECHNOLOGY
3
4
5DEPARTMENT OF
6INFORMATION TECHNOLOGY
7
8
9
10
11 CRYPTOGRAPHY AND NETWORK SECURITY
12
13
14
15 LAB MANUAL
16
17
18
19
20
21PREPARED BY
22
23 G.SAHITHI
24
25 ASSISTANT PROFESSOR
26 INDEX
27
28S.NO. TOPIC PAGE NUMBER
29
301 Write a C program that contains a string (char pointer) with a 4
31 value \Hello World’. The program should XOR each character
32 in this string with 0 and displays the result.
33
342 Write a C program that contains a string (char pointer) with a 6
35 value \Hello World’. The program should AND or and XOR
36 each character in this string with 127 and display the result
37
38 Write a Java program to perform encryption and decryption
393 using the following algorithms: 8-14
40
41 a) Ceaser Cipher
42 b) Substitution Cipher
43 c) Hill Cipher
444 Write a Java program to implement the DES algorithm logic 16-18
45
465 Write a C/JAVA program to implement the BlowFish algorithm 20-21
47 logic
48
49
506 Write a C/JAVA program to implement the Rijndael algorithm 23-24
51 logic.
52
53
547 Using Java Cryptography, encrypt the text “Hello world†using 26-27
55 BlowFish. Create your own key using Java keytool.
56
57
588 Write a Java program to implement RSA Algoithm 29-30
59
60 Implement the Diffie-Hellman Key Exchange mechanism
619 using HTML and JavaScript. Consider the end user as one of 32-33
62 the parties (Alice) and the JavaScript application as other
63
64 party (bob).
65
6610 Calculate the message digest of a text using the SHA-1 35-36
67 algorithm in JAVA.
68
69
7011 Calculate the message digest of a text using the SHA-1 38-39
71 algorithm in JAVA.
72
73
74
75
76
77
78
79 ADDITIONAL PROGRAMS
80
81 12 Implement vegenere cipher substitution technique using C 41-42
82 13 Write a C program to implement Rail fence transposition technique 44-45
83 14 write a C program to implement the signature scheme named digital signature standard (Euclidean Algorithm). 47-50
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 EXPERIMENT-1
122
123AIM: Write a C program that contains a string (char pointer) with a value \Hello World’. The program should XOR each character in this string with 0 and display the result.
124
125PROGRAM:
126
127#include<stdlib.h>
128
129main()
130
131{
132
133char str[]="Hello World";
134
135char str1[11];
136
137int i,len;
138
139len=strlen(str);
140
141for(i=0;i<len;i++)
142
143{
144
145str1[i]=str[i]^0;
146
147printf("%c",str1[i]);
148
149}
150
151printf("\n");
152
153}
154
155
156
157
158 Input:
159
160Hello World
161
162 Output:
163
164Hello World
165
166
167
168
169
170 VIVA QUESTIONS
171
172
173
174
1751.what is character pointer?
176
1772.what is a string?
178
1793.what are the different string functions?
180
1814.Explain XOR operation with an example.
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 EXPERIMENT-2
220
221
222AIM: Write a C program that contains a string (char pointer) with a value \Hello World’. The program should AND or and XOR each character in this string with 127 and display the result.
223
224PROGRAM:
225
226#include <stdio.h>
227
228#include<stdlib.h>
229
230void main()
231
232{
233
234char str[]="Hello World";
235
236char str1[11];
237
238char str2[11]=str[];
239
240int i,len;
241
242len = strlen(str);
243
244for(i=0;i<len;i++)
245
246{
247
248str1[i] = str[i]&127;
249
250printf("%c",str1[i]);
251
252}
253
254printf("\n");
255
256for(i=0;i<len;i++)
257
258{
259
260str3[i] = str2[i]^127;
261
262printf("%c",str3[i]);
263
264}
265
266printf("\n");
267}
268
269
270
271Input:
272 Hello World
273
274Output:
275 Hello World
276
277 Hello World
278
279
280
281 VIVA QUESTIONS
282
283
2841.How does the XOR operation take place on the string?
285
2862.What is a Data Structure?
287
2883.Explain AND operation with example.
289
2904.What is the difference between AND and XOR operations?
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 EXPERIMENT-3
332AIM: Write a Java program to perform encryption and decryption using the following algorithms:
333
334a) Ceaser Cipher
335
336b) Substitution Cipher
337
338c) Hill Cipher
339
340 PROGRAM:
341
342 a)Ceaser Cipher
343
344import java.io.BufferedReader;
345
346import java.io.IOException;
347
348import java.io.InputStreamReader;
349
350import java.util.Scanner;
351
352public class CeaserCipher {
353
354static Scanner sc=new Scanner(System.in);
355
356static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
357
358public static void main(String[] args) throws IOException {
359
360// TODO code application logic here
361
362System.out.print("Enter any String: ");
363
364String str = br.readLine();
365
366System.out.print("\nEnter the Key: ");
367
368int key = sc.nextInt();
369
370String encrypted = encrypt(str, key);
371
372System.out.println("\nEncrypted String is: " +encrypted);
373
374String decrypted = decrypt(encrypted, key); System.out.println("\nDecrypted String is: " +decrypted); System.out.println("\n");
375
376}
377
378public static String encrypt(String str, int key)
379
380
381
382
383
384{ String encrypted = "";
385
386for(int i = 0; i < str.length(); i++) {
387
388int c = str.charAt(i);
389
390if (Character.isUpperCase(c)) {
391
392c = c + (key % 26);
393
394if (c > 'Z')
395
396c = c - 26;
397
398}
399
400else if (Character.isLowerCase(c)) {
401
402c = c + (key % 26);
403
404if (c > 'z')
405
406c = c - 26;
407
408}
409
410encrypted += (char) c;
411
412}
413
414return encrypted;
415
416}
417
418public static String decrypt(String str, int key)
419
420{ String decrypted = "";
421
422for(int i = 0; i < str.length(); i++) { int c = str.charAt(i);
423if (Character.isUpperCase(c)) { c = c - (key % 26);
424if (c < 'A')
425
426c = c + 26;
427
428}
429
430else if (Character.isLowerCase(c)) { c = c - (key % 26);
431if (c < 'a')
432
433c = c + 26;
434
435}
436
437
438
439
440
441
442
443decrypted += (char) c;
444
445}
446
447return decrypted;
448
449}
450
451}
452
453
454
455Input:
456
457 Enter any String: Hello World
458
459 Enter the Key: 5
460
461
462Output:
463 Encrypted String is: MjqqtBtwqi
464
465 Decrypted String is: Hello World
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
505b)Substitution Cipher
506
507PROGRAM:
508
509import java.io.*;
510
511import java.util.*;
512
513public class SubstitutionCipher {
514
515static Scanner sc = new Scanner(System.in);
516
517static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
518
519public static void main(String[] args) throws IOException {
520
521// TODO code application logic here String a = "abcdefghijklmnopqrstuvwxyz"; String b = "zyxwvutsrqponmlkjihgfedcba";
522
523System.out.print("Enter any string: ");
524
525String str = br.readLine();
526
527String decrypt = "";
528
529char c;
530
531for(int i=0;i<str.length();i++)
532
533{
534
535c = str.charAt(i);
536
537int j = a.indexOf(c);
538
539decrypt = decrypt+b.charAt(j);
540
541}
542
543System.out.println("The encrypted data is: " +decrypt);
544
545}
546
547}
548
549
550Input:
551 Enter any string: aceho
552
553Output:
554 The encrypted data is: zxvsl
555
556
557
558
559
560
561
562
563
564c)Hill Cipher
565
566PROGRAM:
567
568import java.io.*;
569
570import java.util.*;
571
572import java.io.*; public
573
574class HillCipher {
575
576static float[][] decrypt = new float[3][1];
577
578static float[][] a = new float[3][3]; static
579
580float[][] b = new float[3][3]; static
581
582float[][] mes = new float[3][1]; static
583
584float[][] res = new float[3][1];
585
586static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException {
587
588// TODO code application logic here getkeymes();
589
590for(int i=0;i<3;i++) for(int j=0;j<1;j++) for(int k=0;k<3;k++) { res[i][j]=res[i][j]+a[i][k]*mes[k][j]; } System.out.print("\nEncrypted string is : "); for(int i=0;i<3;i++) { System.out.print((char)(res[i][0]%26+97)); res[i][0]=res[i][0];
591
592}
593
594inverse();
595
596for(int i=0;i<3;i++)
597
598for(int j=0;j<1;j++)
599
600for(int k=0;k<3;k++) {
601
602decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }
603
604System.out.print("\nDecrypted string is : ");
605
606
607
608
609for(int i=0;i<3;i++){
610
611System.out.print((char)(decrypt[i][0]%26+97));
612
613}
614
615System.out.print("\n");
616
617}
618
619public static void getkeymes() throws IOException {
620
621System.out.println("Enter 3x3 matrix for key (It should be inversible): ");
622
623for(int i=0;i<3;i++)
624
625for(int j=0;j<3;j++)
626
627a[i][j] = sc.nextFloat();
628
629System.out.print("\nEnter a 3 letter string: ");
630
631String msg = br.readLine();
632
633for(int i=0;i<3;i++)
634
635mes[i][0] = msg.charAt(i)-97;
636
637}
638
639public static void inverse() {
640
641floatp,q;
642
643float[][] c = a;
644
645for(int i=0;i<3;i++)
646
647for(int j=0;j<3;j++) {
648
649//a[i][j]=sc.nextFloat();
650
651if(i==j)
652
653b[i][j]=1;
654
655else b[i][j]=0;
656
657}
658
659for(int k=0;k<3;k++) {
660
661for(int i=0;i<3;i++) {
662
663p = c[i][k];
664
665q = c[k][k];
666
667for(int j=0;j<3;j++) {
668
669if(i!=k) {
670
671
672
673
674
675
676
677c[i][j] = c[i][j]*q-p*c[k][j];
678
679b[i][j] = b[i][j]*q-p*b[k][j];
680
681} } } }
682
683for(int i=0;i<3;i++)
684
685for(int j=0;j<3;j++) {
686
687b[i][j] = b[i][j]/c[i][i]; }
688
689System.out.println("");
690
691System.out.println("\nInverse Matrix is : ");
692
693for(int i=0;i<3;i++) {
694
695for(int j=0;j<3;j++)
696
697System.out.print(b[i][j] + " ");
698
699System.out.print("\n"); }
700
701} }
702
703
704Input:
705
706 Enter a 3 letter string: hai
707
708Output:
709
710Encrypted string is :fdx
711
712Inverse Matrix is :
713
7140.083333336 0.41666666 -0.33333334
715
716-0.41666666 -0.083333336 0.6666667
717
7180.5833333 -0.083333336 -0.33333334
719
720Decrypted string is: hai
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738 VIVA QUESTIONS
739
7401.What is encryption?
741
7422.What is the difference between plain text and cipher text?
743
7443.What result do we get after decryption?
745
7464.What is the principle for Caesar cipher algorithm?
747
7485. What is the main difference between Substitution and Transposition techniques?
749
7506.The key matrix used in the Hill cipher technique is obtained from?
751
7527.What is the decryption formula for Hill cipher?
753
7548.Playfair cipher can encrypt how many latters of plain text at a time?
755
7569.What is the difference between Caeser cipher and Play fair cipher?
757
75810.What is meant by Block cipher?
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 EXPERIMENT-4
785
786 AIM: Write a Java program to implement the DES algorithm logic.
787
788 PROGRAM:
789
790import java.util.*;
791
792import java.io.BufferedReader;
793
794import java.io.InputStreamReader;
795
796import java.security.spec.KeySpec;
797
798import javax.crypto.Cipher;
799
800import javax.crypto.SecretKey;
801
802import javax.crypto.SecretKeyFactory;
803
804import javax.crypto.spec.DESedeKeySpec;
805
806import sun.misc.BASE64Decoder;
807
808import sun.misc.BASE64Encoder;
809
810public class DES {
811
812private static final String UNICODE_FORMAT = "UTF8";
813
814public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
815
816privateKeySpecmyKeySpec;
817
818privateSecretKeyFactorymySecretKeyFactory;
819
820private Cipher cipher;
821
822byte[] keyAsBytes;
823
824private String myEncryptionKey;
825
826private String myEncryptionScheme;
827
828SecretKey key;
829
830static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public DES() throws Exception {
831
832// TODO code application logic here myEncryptionKey
833
834= "ThisIsSecretEncryptionKey"; myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); myKeySpec
835=
836
837
838
839= new DESedeKeySpec(keyAsBytes);
840
841mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
842
843cipher = Cipher.getInstance(myEncryptionScheme);
844
845key = mySecretKeyFactory.generateSecret(myKeySpec);
846
847}
848
849public String encrypt(String unencryptedString)
850
851{ String encryptedString = null;
852
853try {
854
855cipher.init(Cipher.ENCRYPT_MODE, key);
856
857byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
858
859byte[] encryptedText = cipher.doFinal(plainText);
860
861BASE64Encoder base64encoder = new BASE64Encoder();
862
863encryptedString = base64encoder.encode(encryptedText); }
864
865catch (Exception e) {
866
867e.printStackTrace(); }
868
869returnencryptedString; }
870
871public String decrypt(String encryptedString)
872
873{ String decryptedText=null;
874
875try {
876
877cipher.init(Cipher.DECRYPT_MODE, key);
878
879BASE64Decoder base64decoder = new BASE64Decoder();
880
881byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
882
883byte[] plainText = cipher.doFinal(encryptedText); decryptedText=
884
885bytes2String(plainText); }
886
887catch (Exception e) {
888
889e.printStackTrace(); }
890
891returndecryptedText; }
892
893private static String bytes2String(byte[] bytes)
894
895{ StringBufferstringBuffer = new StringBuffer(); for (int i = 0; i <bytes.length;
896
897
898
899
900i++) { stringBuffer.append((char) bytes[i]); }
901
902returnstringBuffer.toString(); }
903
904public static void main(String args []) throws Exception
905
906{ System.out.print("Enter the string: "); DES myEncryptor= new DES();
907
908String stringToEncrypt = br.readLine();
909
910String encrypted = myEncryptor.encrypt(stringToEncrypt); String decrypted = myEncryptor.decrypt(encrypted); System.out.println("\nString To Encrypt: " +stringToEncrypt); System.out.println("\nEncrypted Value : " +encrypted); System.out.println("\nDecrypted Value : " +decrypted); System.out.println("");
911
912}
913
914}
915
916
917
918 Input:
919
920Enter the string: Welcome
921
922String To Encrypt: Welcome
923
924 Output:
925 Encrypted Value : BPQMwc0wKvg=
926
927 Decrypted Value : Welcome
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 VIVA QUESTIONS
961
9621.DES algorithm follows which encryption principle?
963
9642.What is the size of the key used in DES?
965
9663.How many rounds take place in DES algorithm?
967
9684.What is the block size taken as plain text?
969
9705.Which structure is the basis for DES algorithm?
971
9726.Where do we use S-boxes in DES?
973
9747.The key size is reduced to how many bits?
975
9768.What is the purpose of sub-key generation algorithm?
977
9789.What operation takes place in DES before round1?
979
98010.Where do we use expansion permutation?
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000 EXPERIMENT-5
1001
1002AIM: Write a C/JAVA program to implement the BlowFish algorithm logic.
1003
1004PROGRAM:
1005
1006import java.io.*;
1007
1008import java.io.FileInputStream;
1009
1010import java.io.FileOutputStream;
1011
1012import java.security.Key;
1013
1014import javax.crypto.Cipher;
1015
1016import javax.crypto.CipherOutputStream;
1017
1018import javax.crypto.KeyGenerator;
1019
1020import sun.misc.BASE64Encoder;
1021
1022public class BlowFish {
1023
1024public static void main(String[] args) throws Exception {
1025
1026// TODO code application logic here KeyGeneratorkeyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128); Key secretKey = keyGenerator.generateKey();
1027
1028Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding");
1029
1030cipherOut.init(Cipher.ENCRYPT_MODE, secretKey); BASE64Encoder
1031
1032encoder = new BASE64Encoder();
1033
1034byte iv[] = cipherOut.getIV();
1035
1036if (iv != null) {
1037
1038
1039System.out.println("Initialization Vector of the Cipher: " + encoder.encode(iv));
1040
1041FileInputStream fin = new FileInputStream("inputFile.txt");
1042
1043FileOutputStreamfout = new FileOutputStream("outputFile.txt");
1044
1045CipherOutputStreamcout = new CipherOutputStream(fout, cipherOut);
1046
1047int input = 0;
1048
1049while ((input = fin.read()) != -1) {
1050
1051
1052
1053
1054}
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066fin.close(); cout.close(); } }
1067
1068
1069
1070Input:
1071
1072 Initialization Vector of the Cipher: dI1MXzW97oQ=
1073
1074Output:
1075 Contents of inputFile.txt: Hello World
1076
1077 Contents of outputFile.txt: ùJÖ˜ NåI“
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 VIVA QUESTIONS
1128
11291.Blowfish algorithm is based on which encryption principle?
1130
11312.What are the advantages of Blowfish algorithm?
1132
11333.What is the size of the plain text block?
1134
11354.What is the key size in Blowfish?
1136
11375.How many rounds are present in this algorithm?
1138
11396.What are the basic operations used in Blowfish algorithm?
1140
11417.How many S-boxes are used in this?
1142
11438.What is K-array and P-array?
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178 EXPERIMENT-6
1179
1180AIM: Write a C/JAVA program to implement the Rijndael algorithm logic.
1181
1182PROGRAM:
1183
1184import java.security.*;
1185
1186import javax.crypto.*;
1187
1188import javax.crypto.spec.*;
1189
1190import java.io.*;
1191
1192public class AES {
1193
1194public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i;
1195
1196for (i = 0; i < buf.length; i++) {
1197
1198if (((int) buf[i] & 0xff) < 0x10)
1199
1200strbuf.append("0");
1201
1202strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); }
1203
1204return strbuf.toString(); }
1205
1206public static void main(String[] args) throws Exception { String message="AES still rocks!!";
1207
1208// Get the KeyGenerator
1209
1210KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); // 192 and 256 bits may not be available
1211// Generate the secret key specs.
1212
1213SecretKey skey = kgen.generateKey();
1214
1215byte[] raw = skey.getEncoded();
1216
1217SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
1218
1219// Instantiate the cipher
1220
1221Cipher cipher = Cipher.getInstance("AES");
1222
1223cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
1224
1225byte[] encrypted = cipher.doFinal((args.length == 0 ? message :
1226
1227
1228
1229
1230
1231
1232
1233args[0]).getBytes()); System.out.println("encrypted string: " + asHex(encrypted)); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] original = cipher.doFinal(encrypted); String originalString = new String(original);
1234System.out.println("Original string: " + originalString + " " + asHex(original));
1235
1236}
1237}
1238
1239
1240
1241 Input:
1242
1243Input your message: Hello KGRCET
1244
1245 Output:
1246 Encrypted text: 3ooo&&(*&*4r4
1247
1248Decrypted text: Hello KGRCET
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289 VIVA QUESTIONS
1290
12911.Rijndael refers to which algorithm?
1292
12932.Abbreviate AES?
1294
12953.What is the block length of AES?
1296
12974.Number of rounds for AES depends on what?
1298
12995.What operations take place in each round of AES?
1300
13016.The round 10 in encryption does not contain which step?
1302
13037.What operation takes place in mix columns?
1304
13058.The 4x4 matrix of bytes is arranged in which fashion?
1306
13079.What is the key size in AES?
1308
130910.What is the purpose of key expansion?
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327 EXPERIMENT-7
1328
1329AIM: Using Java Cryptography, encrypt the text “Hello world†using BlowFish.
1330
1331Create your own key using Java keytool.
1332
1333PROGRAM:
1334
1335import javax.crypto.Cipher;
1336
1337import javax.crypto.KeyGenerator;
1338
1339import javax.crypto.SecretKey;
1340
1341import javax.swing.JOptionPane;
1342
1343public class BlowFishCipher {
1344
1345public static void main(String[] args) throws Exception {
1346
1347// create a key generator based upon the Blowfish cipher KeyGeneratorkeygenerator = KeyGenerator.getInstance("Blowfish");
1348// create a key
1349
1350// create a cipher based upon Blowfish Cipher
1351
1352cipher = Cipher.getInstance("Blowfish");
1353
1354// initialise cipher to with secret key cipher.init(Cipher.ENCRYPT_MODE, secretkey);
1355
1356// get the text to encrypt
1357
1358String inputText = JOptionPane.showInputDialog("Input your message:
1359
1360"); // encrypt message
1361
1362byte[] encrypted = cipher.doFinal(inputText.getBytes());
1363
1364// re-initialise the cipher to be in decrypt mode cipher.init(Cipher.DECRYPT_MODE, secretkey);
1365
1366// decrypt message
1367
1368byte[] decrypted = cipher.doFinal(encrypted);
1369
1370// and display the results
1371
1372
1373
1374
1375
1376
1377
1378JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
1379
1380"\nEncrypted text: " + new String(encrypted) + "\n" +
1381
1382"\nDecrypted text: " + new String(decrypted));
1383
1384System.exit(0);
1385
1386} }
1387
1388
1389
1390Input:
1391
1392 Input your message: Hello world
1393
1394Output:
1395 Encrypted text: 3ooo&&(*&*4r4
1396
1397 Decrypted text: Hello world
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444 VIVA QUESTIONS
1445
1446
14471.RC4 uses which cryptographic principle?
1448
14492.What are the 2 parts present in RC4 algorithm?
1450
14513.What is the size of the array initialized?
1452
14534.What operation takes place in KSA?
1454
14555.What is the decryption logic used in RC4?
1456
14576.What operation takes place in PRGA?
1458
14597.XOR operation takes place on which data?
1460
14618.After initializing the array ,which operation is run on it?
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488 EXPERIMENT-8
1489
1490
1491
1492 AIM: Write a Java program to implement RSA Algoithm.
1493
1494 PROGRAM:
1495
1496import java.io.BufferedReader;
1497
1498import java.io.InputStreamReader;
1499
1500import java.math.*;
1501
1502import java.util.Random;
1503
1504import java.util.Scanner;
1505
1506public class RSA {
1507
1508static Scanner sc = new Scanner(System.in); public static void main(String[] args) {
1509
1510// TODO code application logic here System.out.print("Enter a Prime number: ");
1511
1512BigInteger p = sc.nextBigInteger(); // Here's one prime number.. System.out.print("Enter another prime number: "); BigInteger q = sc.nextBigInteger(); // ..and another. BigInteger n = p.multiply(q);
1513
1514BigInteger n2 = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
1515
1516BigInteger e = generateE(n2);
1517
1518BigInteger d = e.modInverse(n2); // Here's the multiplicative inverse
1519
1520System.out.println("Encryption keys are: " + e + ", " + n);
1521
1522System.out.println("Decryption keys are: " + d + ", " + n);
1523
1524}
1525
1526public static BigIntegergenerateE(BigIntegerfiofn) {
1527
1528int y, intGCD;
1529
1530BigInteger e;
1531
1532BigInteger gcd;
1533
1534Random x = new Random();
1535
1536do {
1537
1538
1539
1540y = x.nextInt(fiofn.intValue()-1);
1541
1542String z = Integer.toString(y);
1543
1544e = new BigInteger(z);
1545
1546gcd = fiofn.gcd(e);
1547
1548intGCD = gcd.intValue();
1549
1550}
1551
1552while(y <= 2 || intGCD != 1);
1553
1554return e;
1555
1556}
1557
1558}
1559
1560
1561Input:
1562
1563Enter a Prime number: 5
1564
1565Enter another prime number: 11
1566
1567Output:
1568
1569Encryption keys are: 33, 55
1570
1571Decryption keys are: 17, 55
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605 VIVA QUESTIONS
1606
1607
16081.What is the role of prime numbers in RSA algorithm?
1609
16102.How to calculate totient function in RSA?
1611
16123.What formula is used to find out the private key?
1613
16144.How is the public key choosen?
1615
16165.Encryption is done on basis of which formula?
1617
16186.What is the decryption operation for RSA?
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650 EXPERIMENT-9
1651
1652
1653AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript application as other party (bob).
1654
1655PROGRAM:
1656
1657import java.math.BigInteger;
1658
1659import java.security.KeyFactory;
1660
1661import java.security.KeyPair;
1662
1663import java.security.KeyPairGenerator; import java.security.SecureRandom; import javax.crypto.spec.DHParameterSpec; import javax.crypto.spec.DHPublicKeySpec; public class DiffeHellman { public final static int pValue = 47;
1664
1665public final static int gValue = 71;
1666
1667public final static int XaValue = 9;
1668
1669public final static int XbValue = 14;
1670
1671public static void main(String[] args) throws Exception { // TODO code application logic here
1672BigInteger p = new BigInteger(Integer.toString(pValue));
1673
1674BigInteger g = new BigInteger(Integer.toString(gValue));
1675
1676BigIntegerXa = new
1677
1678BigInteger(Integer.toString(XaValue)); BigIntegerXb =
1679
1680new BigInteger(Integer.toString(XbValue)); createKey();
1681
1682intbitLength = 512; // 512 bits
1683
1684SecureRandomrnd = new SecureRandom();
1685
1686p = BigInteger.probablePrime(bitLength, rnd);
1687
1688g = BigInteger.probablePrime(bitLength, rnd);
1689
1690
1691
1692
1693
1694
1695
1696createSpecificKey(p, g);
1697
1698}
1699
1700public static void createKey() throws Exception {
1701
1702KeyPairGeneratorkpg = KeyPairGenerator.getInstance("DiffieHellman"); kpg.initialize(512);
1703
1704KeyPairkp = kpg.generateKeyPair();
1705
1706KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
1707
1708DHPublicKeySpeckspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
1709
1710DHPublicKeySpec.class);
1711
1712System.out.println("Public key is: " +kspec);
1713
1714}
1715
1716public static void createSpecificKey(BigInteger p, BigInteger g) throws
1717
1718Exception { KeyPairGeneratorkpg =
1719
1720KeyPairGenerator.getInstance("DiffieHellman"); DHParameterSpecparam = new
1721
1722DHParameterSpec(p, g); kpg.initialize(param);
1723
1724KeyPairkp = kpg.generateKeyPair();
1725
1726KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
1727
1728DHPublicKeySpeckspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
1729
1730DHPublicKeySpec.class);
1731
1732System.out.println("\nPublic key is : " +kspec);
1733
1734}
1735
1736}
1737
1738
1739Output:
1740
1741Public key is: javax.crypto.spec.DHPublicKeySpec@5afd29
1742
1743Public key is: javax.crypto.spec.DHPublicKeySpec@9971ad
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757 VIVA QUESTIONS
1758
17591.What are the global public elements required for Diffie Hellman key exchange?
1760
17612.How should we choose the prime number for key generation?
1762
17633.What is meant by a primitive root?
1764
17654.How should we select the Private key?
1766
17675.Calculation of public key is done using which formula?
1768
17696.Explain a user’s key generation process?
1770
17717.How is the secrete key generated by the users?
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804 EXPERIMENT-10
1805
1806
1807AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
1808
1809PROGRAM:
1810
1811import java.security.*;
1812
1813public class SHA1 {
1814
1815public static void main(String[] a) {
1816
1817try {
1818
1819MessageDigest md = MessageDigest.getInstance("SHA1");
1820
1821System.out.println("Message digest object info: ");
1822
1823System.out.println(" Algorithm = " +md.getAlgorithm());
1824
1825System.out.println(" Provider = " +md.getProvider());
1826
1827System.out.println(" ToString = " +md.toString());
1828
1829String input = "";
1830
1831md.update(input.getBytes());
1832
1833byte[] output = md.digest();
1834
1835System.out.println();
1836
1837System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
1838
1839input = "abc";
1840
1841md.update(input.getBytes());
1842
1843output = md.digest();
1844
1845System.out.println();
1846
1847System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
1848
1849input = "abcdefghijklmnopqrstuvwxyz";
1850
1851md.update(input.getBytes());
1852
1853output = md.digest();
1854
1855System.out.println();
1856
1857System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output));
1858
1859System.out.println(""); }
1860
1861catch (Exception e) {
1862
1863
1864
1865
1866System.out.println("Exception: " +e);
1867
1868}
1869
1870}
1871
1872public static String bytesToHex(byte[] b) {
1873
1874char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
1875
1876StringBufferbuf = new StringBuffer();
1877
1878for (int j=0; j<b.length; j++) {
1879
1880buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
1881
1882buf.append(hexDigit[b[j] & 0x0f]); }
1883
1884returnbuf.toString(); }
1885
1886}
1887
1888
1889Input:
1890
1891Message digest object info:
1892
1893Algorithm = SHA1
1894
1895Provider = SUN version 1.6
1896
1897 Output:
1898 ToString = SHA1 Message Digest from SUN, <initialized> SHA1("") =
1899
1900DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 SHA1("abc") =
1901
1902A9993E364706816ABA3E25717850C26C9CD0D89D
1903
1904SHA1("abcdefghijklmnopqrstuvwxyz")=32D10C7B8CF96570CA04CE37F2A19D8424
1905
19060D3A89
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929 VIVA QUESTIONS
1930
19311.What is the length of the input plain text in SHA1?
1932
19332.Why is the padding applied to the message?
1934
19353.How many rounds are present in SHA1?
1936
19374.All the rounds of SHA1 takes place in which function?
1938
19395.What is MSA?
1940
19416.What is the operation of MSA?
1942
19437.Expalain the operation of single round?
1944
19458.What is the result of compression function?
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 EXPERIMENT-11
1980
1981 AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
1982
1983 PROGRAM:
1984
1985import java.security.*;
1986
1987public class MD5 {
1988
1989public static void main(String[] a) {
1990
1991// TODO code application logic here
1992
1993try {
1994
1995MessageDigest md = MessageDigest.getInstance("MD5");
1996
1997System.out.println("Message digest object info: ");
1998
1999System.out.println(" Algorithm = " +md.getAlgorithm());
2000
2001System.out.println(" Provider = " +md.getProvider());
2002
2003System.out.println(" ToString = " +md.toString());
2004
2005String input = "";
2006
2007md.update(input.getBytes());
2008
2009byte[] output = md.digest();
2010
2011System.out.println();
2012
2013System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));
2014
2015input = "abc";
2016
2017md.update(input.getBytes());
2018
2019output = md.digest();
2020
2021System.out.println();
2022
2023System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));
2024
2025input = "abcdefghijklmnopqrstuvwxyz";
2026
2027md.update(input.getBytes());
2028
2029output = md.digest();
2030
2031System.out.println();
2032
2033System.out.println("MD5(\"" +input+"\") = "
2034
2035+bytesToHex(output)); System.out.println("");
2036
2037}
2038
2039
2040
2041
2042
2043catch (Exception e) {
2044
2045System.out.println("Exception: " +e); }
2046
2047}
2048
2049public static String bytesToHex(byte[] b) {
2050
2051char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
2052
2053StringBufferbuf = new StringBuffer();
2054
2055for (int j=0; j<b.length; j++) {
2056
2057buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
2058
2059buf.append(hexDigit[b[j] & 0x0f]); }
2060
2061return buf.toString(); } }
2062
2063Input:
2064
2065 Message digest object info:
2066
2067Algorithm = MD5
2068
2069Provider = SUN version 1.6
2070
2071Output:
2072ToString = MD5 Message Digest from SUN, <initialized> MD5("") = D41D8CD98F00B204E9800998ECF8427E MD5("abc") = 900150983CD24FB0D6963F7D28E17F72 MD5("abcdefghijklmnopqrstuvwxyz") = C3FCD3D76192E4007DFB496CCA67E13B
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092 VIVA QUESTIONS
2093
20941.What is message digest?
2095
20962.What are the algorithms that generate message digest?
2097
20983.What is hashing?
2099
21004.Which operation generates the message digest?
2101
21025.Which logic implements message digest generation?
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130 ADDITIONAL PROGRAMS
2131 EXPERIMENT-12
2132
2133 AIM: To implement the Vigenere Cipher substitution technique using C program.
2134 PROGRAM:
2135
2136#include <stdio.h>
2137#include<conio.h>
2138#include <ctype.h>
2139#include <string.h>
2140void encipher();
2141void decipher();
2142void main()
2143{
2144int choice;
2145clrscr();
2146while(1)
2147{
2148
2149printf("\n1. Encrypt Text"); printf("\t2. Decrypt Text"); printf("\t3. Exit"); printf("\n\nEnter Your Choice : "); scanf("%d",&choice); if(choice == 3)
2150exit(0);
2151else if(choice == 1)
2152encipher();
2153else if(choice == 2)
2154decipher();
2155else
2156printf("Please Enter Valid Option.");
2157}
2158}
2159void encipher()
2160{
2161unsigned int i,j;
2162char input[50],key[10];
2163printf("\n\nEnter Plain Text: ");
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178scanf("%s",input);
2179printf("\nEnter Key Value: ");
2180scanf("%s",key);
2181printf("\nResultant Cipher Text: ");
2182for(i=0,j=0;i<strlen(input);i++,j++)
2183{
2184if(j>=strlen(key))
2185{j=0;
2186}
2187
2188printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-
218965))%26));
2190}}
2191void decipher()
2192{
2193unsigned int i,j;
2194char input[50],key[10];
2195int value;
2196printf("\n\nEnter Cipher Text: ");
2197scanf("%s",input);
2198printf("\n\nEnter the key value: ");
2199scanf("%s",key);
2200for(i=0,j=0;i<strlen(input);i++,j++)
2201{
2202if(j>=strlen(key))
2203{ j=0; }
2204value = (toupper(input[i])-64)-(toupper(key[j])-64);
2205if( value < 0)
2206{ value = value * -1;
2207
2208}
2209printf("%c",65 + (value % 26));
2210}}
2211
2212
2213Output:
2214
2215
22161.Encrypt text 2.Decrypt text 3.Exit
2217Enter your choice:1
2218Enter plain text: hai
2219Enter key value:hello
2220Resultant cipher text:OET
2221
22221.Encrypt text 2.Decrypt text 3.Exit
2223Enter your choice:2
2224Enter Cipher Text:OET
2225Enter the key value:hello
2226Hai
2227
22281.Encrypt text 2.Decrypt text 3.Exit
2229Enter your choice:3
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249 VIVA QUESTIONS
2250
22511.What is poly alphabetic cipher?
2252
22532.Why is vigenere Cipher more secured than caeser cipher?
2254
22553.What are the common features of polyalphabetic cipher?
2256
22574.What do the key determine for transformation?
2258
22595.Explain the operation of Vigenere cipher with example?
2260
22616.What logic implements this in java?
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310 EXPERIMENT-13
2311 AIM: To write a C program to implement the rail fence transposition technique.
2312 PROGRAM:
2313
2314#include<stdio.h>
2315#include<conio.h>
2316#include<string.h>
2317void main()
2318{
2319int i,j,k,l;
2320char a[20],c[20],d[20];
2321clrscr();
2322printf("\n\t\t RAIL FENCE TECHNIQUE");
2323printf("\n\nEnter the input string : ");
2324gets(a);
2325l=strlen(a);
2326
2327/*Ciphering*/
2328for(i=0,j=0;i<l;i++)
2329{
2330if(i%2==0)
2331c[j++]=a[i];
2332}
2333for(i=0;i<l;i++)
2334{
2335if(i%2==1)
2336c[j++]=a[i];
2337}
2338c[j]='\0';
2339
2340printf("\nCipher text after applying rail fence :"); printf("\n%s",c);
2341
2342/*Deciphering*/
2343if(l%2==0)
2344k=l/2;
2345else
2346k=(l/2)+1;
2347for(i=0,j=0;i<k;i++)
2348{
2349d[j]=c[i];
2350j=j+2;
2351}
2352for(i=k,j=1;i<l;i++)
2353{
2354d[j]=c[i];
2355j=j+2;
2356}
2357
2358
2359
2360
2361
2362
2363
2364
2365d[l]='\0';
2366printf("\nText after decryption : ");
2367printf("%s",d);
2368getch();
2369
2370
2371
2372
2373
2374 Input:
2375
2376RAIL FENCE TECHNIQUE
2377Enter the input string: computer science
2378
2379Output:
2380Cipher text after applying rail fence:
2381Cmue cecoptrsine
2382Text after decryption : computer science
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438 VIVA QUESTIONS
2439
24401.What are different transposition techniques?
2441
24422.What is the difference between transposition and substitution techniques?
2443
24443.How is the encryption done in Rail fence technique?
2445
24464.What makes the Railfence technique more complicated?
2447
24485.Explain it with an example?
2449
24506.Explain the logic that implements railfence.
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498 EXPERIMENT-14
2499
2500AIM: To write a C program to implement the signature scheme named digital signature standard (Euclidean Algorithm).
2501
2502 PROGRAM:
2503
2504import java.util.*;
2505import java.math.BigInteger;
2506class dsaAlg {
2507
2508final static BigInteger one = new BigInteger("1"); final static BigInteger zero = new BigInteger("0");
2509public static BigInteger getNextPrime(String ans)
2510{
2511
2512BigInteger test = new BigInteger(ans); while (!test.isProbablePrime(99)) e:
2513{
2514test = test.add(one);
2515}
2516return test;
2517}
2518public static BigInteger findQ(BigInteger n)
2519{
2520
2521BigInteger start = new BigInteger("2"); while (!n.isProbablePrime(99)) {
2522while (!((n.mod(start)).equals(zero)))
2523{
2524start = start.add(one);
2525
2526
2527}
2528n = n.divide(start);
2529}
2530return n;
2531}
2532public static BigInteger getGen(BigInteger p, BigInteger q,
2533Random r)
2534{
2535
2536BigInteger h = new BigInteger(p.bitLength(), r); h = h.mod(p);
2537return h.modPow((p.subtract(one)).divide(q), p);
2538}
2539public static void main (String[] args) throws
2540java.lang.Exception
2541{
2542Random randObj = new Random();
2543
2544BigInteger p = getNextPrime("10600"); /* approximate prime */
2545
2546
2547
2548
2549
2550BigInteger q = findQ(p.subtract(one));
2551BigInteger g = getGen(p,q,randObj); System.out.println(" \n simulation of Digital Signature Algorithm \n");
2552
2553System.out.println(" \n global public key components are:\n");
2554System.out.println("\np is: " + p);
2555System.out.println("\nq is: " + q);
2556System.out.println("\ng is: " + g);
2557
2558BigInteger x = new BigInteger(q.bitLength(), randObj); x = x.mod(q);
2559BigInteger y = g.modPow(x,p);
2560
2561BigInteger k = new BigInteger(q.bitLength(), randObj); k = k.mod(q);
2562BigInteger r = (g.modPow(k,p)).mod(q);
2563
2564BigInteger hashVal = new BigInteger(p.bitLength(), randObj);
2565BigInteger kInv = k.modInverse(q);
2566
2567BigInteger s = kInv.multiply(hashVal.add(x.multiply(r))); s = s.mod(q);
2568System.out.println("\nsecret information are:\n");
2569System.out.println("x (private) is:" + x);
2570System.out.println("k (secret) is: " + k);
2571System.out.println("y (public) is: " + y);
2572System.out.println("h (rndhash) is: " + hashVal);
2573
2574System.out.println("\n generating digital signature:\n");
2575System.out.println("r is : " + r);
2576System.out.println("s is : " + s);
2577BigInteger w = s.modInverse(q);
2578
2579BigInteger u1 = (hashVal.multiply(w)).mod(q); BigInteger u2 = (r.multiply(w)).mod(q);
2580
2581BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
2582v = (v.mod(p)).mod(q);
2583System.out.println("\nverifying digital signature
2584(checkpoints)\n:");
2585System.out.println("w is : " + w);
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596}
2597n = n.divide(start);
2598}
2599return n;
2600}
2601public static BigInteger getGen(BigInteger p, BigInteger q,
2602Random r)
2603{
2604
2605BigInteger h = new BigInteger(p.bitLength(), r); h = h.mod(p);
2606return h.modPow((p.subtract(one)).divide(q), p);
2607}
2608public static void main (String[] args) throws
2609java.lang.Exception
2610{
2611Random randObj = new Random();
2612
2613BigInteger p = getNextPrime("10600"); /* approximate prime */
2614
2615BigInteger q = findQ(p.subtract(one)); BigInteger g = getGen(p,q,randObj); System.out.println(" \n simulation of Digital Signature Algorithm \n");
2616
2617System.out.println(" \n global public key components are:\n");
2618System.out.println("\np is: " + p);
2619System.out.println("\nq is: " + q);
2620System.out.println("\ng is: " + g);
2621
2622BigInteger x = new BigInteger(q.bitLength(), randObj); x = x.mod(q);
2623BigInteger y = g.modPow(x,p);
2624
2625BigInteger k = new BigInteger(q.bitLength(), randObj); k = k.mod(q);
2626BigInteger r = (g.modPow(k,p)).mod(q);
2627
2628BigInteger hashVal = new BigInteger(p.bitLength(), randObj);
2629BigInteger kInv = k.modInverse(q);
2630
2631BigInteger s = kInv.multiply(hashVal.add(x.multiply(r))); s = s.mod(q);
2632System.out.println("\nsecret information are:\n");
2633System.out.println("x (private) is:" + x);
2634System.out.println("k (secret) is: " + k);
2635System.out.println("y (public) is: " + y);
2636System.out.println("h (rndhash) is: " + hashVal);
2637
2638System.out.println("\n generating digital signature:\n");
2639System.out.println("r is : " + r);
2640System.out.println("s is : " + s);
2641BigInteger w = s.modInverse(q);
2642
2643BigInteger u1 = (hashVal.multiply(w)).mod(q); BigInteger u2 = (r.multiply(w)).mod(q);
2644
2645BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
2646v = (v.mod(p)).mod(q);
2647System.out.println("\nverifying digital signature
2648(checkpoints)\n:");
2649System.out.println("w is : " + w);
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660System.out.println("u1 is : " + u1);
2661System.out.println("u2 is : " + u2);
2662System.out.println("v is : " + v);
2663if (v.equals(r))
2664{
2665
2666System.out.println("\nsuccess: digital signature is verified!\n " + r);
2667}
2668else
2669{
2670
2671System.out.println("\n error: incorrect digital signature\n ");
2672}
2673}
2674}
2675
2676Output:
2677
2678E:\>javac dsaAlg.java
2679E:\>java dsa Alg
2680 Simulation of digital signature standard Algorithm
2681
2682Global public key components are:
2683p is: 10601
2684q is: 53
2685g is: 5559
2686
2687secrete information are:
2688 x<private> is: 6
2689 k<secrete> is:7
2690 y<public> is:1992
2691 h<rndhash> is:10088
2692 generating digital signature:
2693r is: 42
2694s is:31
2695
2696verifying digital signature <check points>:
2697w is :12
2698u1 is:4
2699u2 is:27
2700v is :42
2701
2702success digital signature is verified!
270342
2704E:\>
2705
2706
2707
2708
2709 VIVA QUESTIONS
2710
27111.What is meant by a signature?
2712
27132.What is the difference between signature and digital signature?
2714
27153.Who is responsible for generation of digital signature?
2716
27174.What is authentication?
2718
27195.What is the role of digital signature in authentication?
2720
27216.What is the implementation logic?