· 7 years ago · Oct 25, 2018, 04:42 PM
1EX 1:CAESER CIPHER
2
3import java.util.Scanner;
4
5public class CaesarCipher
6{
7 public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
8
9 public static String encrypt(String plainText, int shiftKey)
10 {
11 plainText = plainText.toLowerCase();
12 String cipherText = "";
13 for (int i = 0; i < plainText.length(); i++)
14 {
15 int charPosition = ALPHABET.indexOf(plainText.charAt(i));
16 int keyVal = (shiftKey + charPosition) % 26;
17 char replaceVal = ALPHABET.charAt(keyVal);
18 cipherText += replaceVal;
19 }
20 return cipherText;
21 }
22
23 public static String decrypt(String cipherText, int shiftKey)
24 {
25 cipherText = cipherText.toLowerCase();
26 String plainText = "";
27 for (int i = 0; i < cipherText.length(); i++)
28 {
29 int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
30 int keyVal = (charPosition - shiftKey) % 26;
31 if (keyVal < 0)
32 {
33 keyVal = ALPHABET.length() + keyVal;
34 }
35 char replaceVal = ALPHABET.charAt(keyVal);
36 plainText += replaceVal;
37 }
38 return plainText;
39 }
40
41 public static void main(String[] args)
42 {
43 Scanner sc = new Scanner(System.in);
44 System.out.println("Enter the String for Encryption: ");
45 String message = new String();
46 message = sc.next();
47 System.out.println(encrypt(message, 3));
48 System.out.println(decrypt(encrypt(message, 3), 3));
49 sc.close();
50 }
51}
52OP:
53ENTER THE STRING FOR ENCRYPT:
54AUS
55DXV
56AUS
57
58EX2:PLAYFAIR CIPHER
59
60import java.util.Scanner;
61
62public class PlayfairCipherEncryption
63{
64 private String KeyWord = new String();
65 private String Key = new String();
66 private char matrix_arr[][] = new char[5][5];
67
68 public void setKey(String k)
69 {
70 String K_adjust = new String();
71 boolean flag = false;
72 K_adjust = K_adjust + k.charAt(0);
73 for (int i = 1; i < k.length(); i++)
74 {
75 for (int j = 0; j < K_adjust.length(); j++)
76 {
77 if (k.charAt(i) == K_adjust.charAt(j))
78 {
79 flag = true;
80 }
81 }
82 if (flag == false)
83 K_adjust = K_adjust + k.charAt(i);
84 flag = false;
85 }
86 KeyWord = K_adjust;
87 }
88
89 public void KeyGen()
90 {
91 boolean flag = true;
92 char current;
93 Key = KeyWord;
94 for (int i = 0; i < 26; i++)
95 {
96 current = (char) (i + 97);
97 if (current == 'j')
98 continue;
99 for (int j = 0; j < KeyWord.length(); j++)
100 {
101 if (current == KeyWord.charAt(j))
102 {
103 flag = false;
104 break;
105 }
106 }
107 if (flag)
108 Key = Key + current;
109 flag = true;
110 }
111 System.out.println(Key);
112 matrix();
113 }
114
115 private void matrix()
116 {
117 int counter = 0;
118 for (int i = 0; i < 5; i++)
119 {
120 for (int j = 0; j < 5; j++)
121 {
122 matrix_arr[i][j] = Key.charAt(counter);
123 System.out.print(matrix_arr[i][j] + " ");
124 counter++;
125 }
126 System.out.println();
127 }
128 }
129
130 private String format(String old_text)
131 {
132 int i = 0;
133 int len = 0;
134 String text = new String();
135 len = old_text.length();
136 for (int tmp = 0; tmp < len; tmp++)
137 {
138 if (old_text.charAt(tmp) == 'j')
139 {
140 text = text + 'i';
141 }
142 else
143 text = text + old_text.charAt(tmp);
144 }
145 len = text.length();
146 for (i = 0; i < len; i = i + 2)
147 {
148 if (text.charAt(i + 1) == text.charAt(i))
149 {
150 text = text.substring(0, i + 1) + 'x' + text.substring(i + 1);
151 }
152 }
153 return text;
154 }
155
156 private String[] Divid2Pairs(String new_string)
157 {
158 String Original = format(new_string);
159 int size = Original.length();
160 if (size % 2 != 0)
161 {
162 size++;
163 Original = Original + 'x';
164 }
165 String x[] = new String[size / 2];
166 int counter = 0;
167 for (int i = 0; i < size / 2; i++)
168 {
169 x[i] = Original.substring(counter, counter + 2);
170 counter = counter + 2;
171 }
172 return x;
173 }
174
175 public int[] GetDiminsions(char letter)
176 {
177 int[] key = new int[2];
178 if (letter == 'j')
179 letter = 'i';
180 for (int i = 0; i < 5; i++)
181 {
182 for (int j = 0; j < 5; j++)
183 {
184 if (matrix_arr[i][j] == letter)
185 {
186 key[0] = i;
187 key[1] = j;
188 break;
189 }
190 }
191 }
192 return key;
193 }
194
195 public String encryptMessage(String Source)
196 {
197 String src_arr[] = Divid2Pairs(Source);
198 String Code = new String();
199 char one;
200 char two;
201 int part1[] = new int[2];
202 int part2[] = new int[2];
203 for (int i = 0; i < src_arr.length; i++)
204 {
205 one = src_arr[i].charAt(0);
206 two = src_arr[i].charAt(1);
207 part1 = GetDiminsions(one);
208 part2 = GetDiminsions(two);
209 if (part1[0] == part2[0])
210 {
211 if (part1[1] < 4)
212 part1[1]++;
213 else
214 part1[1] = 0;
215 if (part2[1] < 4)
216 part2[1]++;
217 else
218 part2[1] = 0;
219 }
220 else if (part1[1] == part2[1])
221 {
222 if (part1[0] < 4)
223 part1[0]++;
224 else
225 part1[0] = 0;
226 if (part2[0] < 4)
227 part2[0]++;
228 else
229 part2[0] = 0;
230 }
231 else
232 {
233 int temp = part1[1];
234 part1[1] = part2[1];
235 part2[1] = temp;
236 }
237 Code = Code + matrix_arr[part1[0]][part1[1]]
238 + matrix_arr[part2[0]][part2[1]];
239 }
240 return Code;
241 }
242
243 public static void main(String[] args)
244 {
245 PlayfairCipherEncryption x = new PlayfairCipherEncryption();
246 Scanner sc = new Scanner(System.in);
247 System.out.println("Enter a keyword:");
248 String keyword = sc.next();
249 x.setKey(keyword);
250 x.KeyGen();
251 System.out
252 .println("Enter word to encrypt: (Make sure length of message is even)");
253 String key_input = sc.next();
254 if (key_input.length() % 2 == 0)
255 {
256 System.out.println("Encryption: " + x.encryptMessage(key_input));
257 }
258 else
259 {
260 System.out.println("Message length should be even");
261 }
262 sc.close();
263 }
264}
265
266OP:
267ENTER A KEYWORD:MONARCHY
268ENTER THE KEY WORD TO ENCRYPT :
269BALLON
270ENCRYPTION : ibsupmaw
271
272EX3: hill cipher
273
274import java.util.*;
275class hillCipher
276{
277 /* 3x3 key matrix for 3 characters at once */ public static int[][] keymat = new int[][]
278 { { 1, 2, 1 }, { 2, 3, 2 }, { 2, 2, 1 } };
279 /* key inverse matrix */
280 public static int[][] invkeymat = new int[][]
281 { { -1, 0, 1 }, { 2, -1, 0 }, { -2, 2, -1 } };
282 public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static String encode(char a, char b, char c)
283{
284 String ret = ""; int x,y, z;
285 int posa = (int)a - 65;
286 int posb = (int)b - 65; int posc = (int)c - 65;
287 x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0]; y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1]; z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2]; a = key.charAt(x%26);
288 b = key.charAt(y%26); c = key.charAt(z%26); ret = "" + a + b + c; return ret;
289}
290
291 private static String decode(char a, char b, char c)
292 {
293 String ret = ""; int x,y,z;
294 int posa = (int)a - 65; int posb = (int)b - 65; int posc = (int)c - 65;
295 x = posa * invkeymat[0][0]+ posb * invkeymat[1][0] + posc * invkeymat[2][0]; y = posa * invkeymat[0][1]+ posb * invkeymat[1][1] + posc * invkeymat[2][1]; z = posa * invkeymat[0][2]+ posb * invkeymat[1][2] + posc * invkeymat[2][2]; a = key.charAt((x%26<0) ? (26+x%26) : (x%26));
296 b = key.charAt((y%26<0) ? (26+y%26) : (y%26)); c = key.charAt((z%26<0) ? (26+z%26) : (z%26)); ret = "" + a + b + c;
297 return ret;
298 }
299 public static void main (String[] args) throws java.lang.Exception
300 {
301 String msg; String enc = ""; String dec = ""; int n;
302 msg = ("SecurityLaboratory"); System.out.println("simulation of Hill Cipher"); System.out.println("input message : " + msg); msg = msg.toUpperCase();
303 msg = msg.replaceAll("\\s", ""); /* remove spaces */ n = msg.length() % 3;
304/* append padding text X */ if (n != 0)
305 {
306 for(int i = 1; i<= (3-n);i++)
307 {msg+= 'X';
308 }}
309 System.out.println("padded message : " + msg);
310 char[] pdchars = msg.toCharArray(); for (int i=0; i < msg.length(); i+=3)
311 {
312 enc += encode(pdchars[i], pdchars[i+1], pdchars[i+2]);
313 }
314 System.out.println("encoded message : " + enc); char[] dechars = enc.toCharArray();
315 for (int i=0; i< enc.length(); i+=3)
316 {
317 dec += decode(dechars[i], dechars[i+1], dechars[i+2]);
318 }
319 System.out.println("decoded message : " + dec);} }
320
321EX 4: VIGNERE CIPHER
322import java.util.ArrayList;
323import java.util.Scanner;
324public class vignere {
325 public static void main(String[] args) {
326 Scanner sc=new Scanner(System.in);
327 char al[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
328 int k=0,p=0;
329 String res="",res1="";
330 System.out.println("enter the key");
331 String key=sc.nextLine();
332 System.out.println("enter the plain text");
333 StringBuilder msg=new StringBuilder(sc.next());
334 char arr[]=key.toCharArray();
335 int n=arr.length;
336 ArrayList<Character> keyarr=new ArrayList<Character>();
337 if((msg.length()+1)%2==0) {
338 for(int i=0;i<msg.length();i++) {
339 keyarr.add(arr[i%n]);
340 }
341 }else {
342 msg.append('x');
343 for(int i=0;i<msg.length();i++) {
344 keyarr.add(arr[i%n]); } }
345
346 for(int i=0;i<msg.length();i++) {
347 char c=msg.charAt(i);
348 char c1=keyarr.get(i);
349 for(int j=0;j<al.length;j++) {
350 if(c==al[j]) {
351 p=j;
352 }
353 if(c1==al[j]) {
354 k=j;
355 }
356 }
357 int num=(k+p)%26;
358 res+=al[num];
359 }
360 System.out.println("Encrypted text= "+res);
361 for(int i=0;i<res.length();i++) {
362 char c=res.charAt(i);
363 char c1=keyarr.get(i);
364 for(int j=0;j<al.length;j++) {
365 if(c==al[j]) {
366 p=j;
367 }
368 if(c1==al[j]) {
369 k=j;
370 }
371 }
372 int num=Math.abs((k-p))%26;
373 res1+=al[num];
374 }
375 System.out.println("Decrypted Text= "+res1);
376 }
377}
378
379EX 5: RAIL FENCE CIPHER
380
381import java.util.Scanner;
382public class railfence {
383 public static void main(String[] args) {
384 // TODO code application logic here
385 Scanner sc=new Scanner(System.in);
386 String s1="";
387 String s2="";
388 System.out.println("Enter the PlainText");
389 String pt=sc.nextLine();
390 char[] ch=pt.toCharArray();
391 for(int i=0;i<pt.length();i++){
392 if(i%2==0)
393 s1+=Character.toString(ch[i]);
394 else
395 s2+=Character.toString(ch[i]); }
396 System.out.println("Encrypted Text:" +s1+s2);
397 System.out.print("Decrypted Text:");
398 if(pt.length()%2==0){
399 for(int i=0;i<(pt.length()/2);i++){
400 System.out.print(s1.charAt(i));
401 System.out.print(s2.charAt(i));
402 } }
403
404
405 else
406 {
407 for(int i=0;i<(pt.length()/2)+1;i++){
408 System.out.print(s1.charAt(i));
409 if(i==(pt.length()/2))
410 break;
411 System.out.print(s2.charAt(i));
412 }
413 }
414 }
415}
416
417EX 6: DES
418
419import javax.swing.*;
420import java.security.SecureRandom;
421import javax.crypto.Cipher;
422import javax.crypto.KeyGenerator;
423import javax.crypto.SecretKey;
424import javax.crypto.spec.SecretKeySpec;
425import java.util.Random ;
426import java.util.Scanner;
427public class DES1 {
428 byte[] skey = new byte[1000];
429 String skeyString;
430 static byte[] raw;
431
432 String inputMessage,encryptedData,decryptedMessage;
433 public DES1() {
434 try {
435 generateSymmetricKey();
436 Scanner sc=new Scanner(System.in);
437 System.out.println("Enter message to encrypt");
438 inputMessage=sc.next();
439 byte[] ibyte = inputMessage.getBytes();
440 byte[] ebyte=encrypt(raw, ibyte);
441 String encryptedData = new String(ebyte);
442 System.out.println("Encrypted message "+encryptedData);
443 byte[] dbyte= decrypt(raw,ebyte);
444 String decryptedMessage = new String(dbyte);
445 System.out.println("Decrypted message "+decryptedMessage);
446 }
447 catch(Exception e) {
448 System.out.println(e);
449 }
450 }
451 void generateSymmetricKey() {
452 try {
453 Random r = new Random();
454 int num = r.nextInt(10000);
455 String knum = String.valueOf(num);
456 byte[] knumb = knum.getBytes();
457 skey=getRawKey(knumb);
458 skeyString = new String(skey);
459 }
460 catch(Exception e) {
461 System.out.println(e);
462 }
463 }
464 private static byte[] getRawKey(byte[] seed) throws Exception {
465 KeyGenerator kgen = KeyGenerator.getInstance("DES");
466 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
467 sr.setSeed(seed);
468 kgen.init(56, sr);
469 SecretKey skey = kgen.generateKey();
470 raw = skey.getEncoded();
471 return raw;
472 }
473 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
474 SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
475 Cipher cipher = Cipher.getInstance("DES");
476 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
477 byte[] encrypted = cipher.doFinal(clear);
478 return encrypted;
479 }
480 private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
481 SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
482 Cipher cipher = Cipher.getInstance("DES");
483
484 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
485 byte[] decrypted = cipher.doFinal(encrypted);
486 return decrypted;
487 }
488 public static void main(String args[]) {
489 DES1 des = new DES1();
490 }
491}
492
493
494
495EX7: RSA
496
497import java.util.Scanner;
498public class rsa {
499 public static void main(String args[])
500 {
501 Scanner sc=new Scanner(System.in);
502 int p,q,n,z,d=0,e,i;
503 System.out.println("Enter the number to be encrypted and decrypted");
504 int msg=sc.nextInt();
505
506 double c,msgback;
507 System.out.println("Enter 1st prime number p");
508 p=sc.nextInt();
509 System.out.println("Enter 2nd prime number q");
510 q=sc.nextInt();
511
512 n=p*q;
513 z=(p-1)*(q-1);
514 System.out.println("the value of z = "+z);
515 for(e=2;e<z;e++)
516 {
517 if(gcd(e,z)==1)
518 {
519 break;
520 }
521 }
522 System.out.println("the value of e = "+e);
523 for(i=1;;i++){
524 if((e*i)%z==1){
525 d=i;
526 break;
527 }
528 }
529 System.out.println("the value of d = "+d);
530 c=(Math.pow(msg,e))%n;
531 System.out.println("Encrypted message is : -");
532 System.out.println(c);
533 msgback=(Math.pow(c,d))%n;
534 System.out.println("Derypted message is : -");
535 System.out.println(msgback);
536 }
537 static int gcd(int e, int z)
538 {
539 if(e==0)
540 return z;
541 else
542
543 return gcd(z%e,e);
544 }
545}
546
547EX 8:DIFFIE HELLMAN KEY
548
549import java.util.*;
550class diffieHellmanAlg
551{
552 public static void main (String[] args) throws java.lang.Exception
553 {
554 int p = 11;
555 int g = 2;
556 int x = 9;
557
558 int y = 4;
559 double aliceSends = (Math.pow(g,x))%p;
560 double bobComputes = (Math.pow(aliceSends,y))%p;
561 double bobSends = (Math.pow(g,y))%p;
562 double aliceComputes = (Math.pow(bobSends,x))%p;
563 double sharedSecret = (Math.pow(g,(x*y)))%p;
564 System.out.println("simulation of Diffie-Hellman key exchange algorithm");
565 System.out.println("aliceSends : " + aliceSends);
566 System.out.println("bobComputes : " + bobComputes);
567 System.out.println("bobSends : " + bobSends);
568 System.out.println("aliceComputes : " + aliceComputes);
569 System.out.println("sharedSecret : " + sharedSecret);
570
571 if ((aliceComputes == sharedSecret) && (aliceComputes == bobComputes))
572 System.out.println("success: shared secrets matches! " + sharedSecret);
573 else
574 System.out.println("error: shared secrets does not match");
575 }
576}
577
578EX9: MD5
579
580import java.math.BigInteger;
581import java.security.MessageDigest;
582import java.security.NoSuchAlgorithmException;
583import java.util.Scanner;
584public class md55 {
585 public static String getMd5(String input)
586 {
587 try {
588 MessageDigest md = MessageDigest.getInstance("MD5");
589 byte[] messageDigest = md.digest(input.getBytes());
590 BigInteger no = new BigInteger(1, messageDigest);
591 String hashtext = no.toString(16);
592 while (hashtext.length() < 32) {
593 hashtext = "0" + hashtext;
594 }
595 return hashtext;
596 }
597 catch (NoSuchAlgorithmException e) {
598 throw new RuntimeException(e);
599 }
600 }
601 public static void main(String args[]) throws NoSuchAlgorithmException
602 {
603 String s ="";
604 System.out.println("enter your string");
605 Scanner sc=new Scanner(System.in);
606 s=sc.next();
607 System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
608 }
609}
610
611EX 10:SHA 1
612
613import java.security.*;
614public class SHA1 {
615 public static void main(String[] a) {
616 try
617 {
618 MessageDigest md = MessageDigest.getInstance("SHA1");
619 System.out.println("Message digest object info: ");
620 System.out.println(" Algorithm = " +md.getAlgorithm());
621 System.out.println(" Provider = " +md.getProvider());
622 System.out.println(" ToString = " +md.toString());
623 String input = ""; md.update(input.getBytes()); byte[] output = md.digest();
624 System.out.println();
625 System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
626 input = "abc"; md.update(input.getBytes()); output = md.digest(); System.out.println();
627 System.out.println("SHA1(\" "+input+"\") = " +bytesToHex(output));
628 input = "abcdefghijklmnopqrstuvwxyz"; md.update(input.getBytes());
629 output = md.digest();
630 System.out.println();
631 System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output)); System.out.println("");
632 }
633 catch (Exception e)
634 {
635 System.out.println("Exception: " +e);
636 }
637 }
638 public static String bytesToHex(byte[] b)
639 {
640 char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
641 StringBuffer buf = new StringBuffer();
642 for (int j=0; j<b.length; j++) {
643 buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
644 buf.append(hexDigit[b[j] & 0x0f]);
645 }
646 return buf.toString();
647 }
648
649}
650
651EX:11 DIGITAL SIGNATURE SCHEME
652
653import java.util.*;
654import java.math.BigInteger;
655class dss
656{
657 final static BigInteger one = new BigInteger("1");
658 final static BigInteger zero = new BigInteger("0");
659 public static BigInteger getNextPrime(String ans)
660 {
661 BigInteger test = new BigInteger(ans); while (!test.isProbablePrime(99))
662 {
663 test = test.add(one);
664 }
665 return test;
666 }
667 public static BigInteger findQ(BigInteger n)
668 {
669 BigInteger start = new BigInteger("2");
670 while (!n.isProbablePrime(99))
671 {
672 while (!((n.mod(start)).equals(zero)))
673 {
674 start = start.add(one);
675 }
676 n = n.divide(start);
677 }
678 return n;
679 }
680 public static BigInteger getGen(BigInteger p, BigInteger q,Random r)
681 {
682
683 BigInteger h = new BigInteger(p.bitLength(), r);
684 h = h.mod(p);
685 return h.modPow((p.subtract(one)).divide(q), p);
686 }
687 public static void main (String[] args) throws java.lang.Exception
688 {
689 Random randObj = new Random();
690 BigInteger p = getNextPrime("10600");
691 BigInteger q = findQ(p.subtract(one));
692 BigInteger g = getGen(p,q,randObj);
693 System.out.println("global public key components are:");
694 System.out.println("p is: " + p);
695 System.out.println("q is: " + q);
696 System.out.println("g is: " + g);
697 BigInteger x = new BigInteger(q.bitLength(), randObj); x = x.mod(q);
698 BigInteger y = g.modPow(x,p);
699 BigInteger k = new BigInteger(q.bitLength(), randObj); k = k.mod(q);
700 BigInteger r = (g.modPow(k,p)).mod(q);
701 BigInteger hashVal = new BigInteger(p.bitLength(), randObj);
702 BigInteger kInv = k.modInverse(q);
703 BigInteger s = kInv.multiply(hashVal.add(x.multiply(r)));
704 s = s.mod(q);
705 System.out.println("secret information are:");
706 System.out.println("x (private) is: " + x);
707 System.out.println("k (secret) is: " + k);
708 System.out.println("y (public) is: " + y);
709 System.out.println("h (rndhash) is: " + hashVal);
710 System.out.println("generating digital signature:");
711 System.out.println("r is : " + r);
712 System.out.println("s is : " + s);
713 BigInteger w = s.modInverse(q);
714 BigInteger u1 = (hashVal.multiply(w)).mod(q);
715 BigInteger u2 = (r.multiply(w)).mod(q);
716 BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
717 v = (v.mod(p)).mod(q);
718 System.out.println("verifying digital signature (checkpoints):");
719 System.out.println("w is : " + w);
720 System.out.println("u1 is : " + u1);
721 System.out.println("u2 is : " + u2);
722 System.out.println("v is : " + v);
723 if (v.equals(r))
724 {
725
726 System.out.println("success: digital signature is verified! " + r);
727 }
728 else
729 {
730 System.out.println("error: incorrect digital signature");
731 }
732 }
733}
734
735EX :12 PRIMITIVE ROOT
736
737import java.util.ArrayList;
738import java.util.Scanner;
739import java.util.TreeSet;
740
741public class Diffie {
742 public static void main(String[] args) {
743 Scanner sc=new Scanner(System.in);
744 int xa,xb,ya,yb,p,ppr,k1,k2;
745 System.out.println("Enter A's Private Key");
746 xa=sc.nextInt();
747 System.out.println("Enter B's Private key");
748 xb=sc.nextInt();
749 System.out.println("Enter a prime Number");
750 p=sc.nextInt();
751 ArrayList<Integer> al = new ArrayList <Integer>();
752 int a[] = new int[p-1];
753 for(int i = p-2;i>=0;i--){
754 int k = i+1;
755 for(int j = 1;j<=p-1;j++){
756 int x = (int)(Math.pow(k,j))%p;
757
758 al.add(x);
759 }
760
761 }
762
763 int k =0;
764 ArrayList <Integer> pr = new ArrayList<Integer>();
765 int pows[][] = new int[p-1][p-1];
766 for(int i=0;i<p-1;i++){
767 for( int j=0;j<p-1;j++){
768 pows[i][j] = al.get(k++);
769 }
770 }
771 TreeSet<Integer> hl = new TreeSet<Integer>();
772 for(int i=0;i<10;i++){
773
774
775 int q = pows[i][0];
776 for( int j=0;j<p-1;j++){
777 hl.add(pows[i][j]);
778
779
780 }
781 if(hl.size()==p-1)
782 pr.add(q);
783 hl.clear();
784
785 } // System.out.print(pr.get(0));
786
787
788 //System.out.println("Enter a Primitive Root of a prime number");
789 ppr=pr.get(0);
790 ya= (int)Math.pow(ppr,xa)%p;
791 yb=(int)Math.pow(ppr,xb)%p;
792 k1=(int)Math.pow(ya,xb)%p;
793 k2=(int)Math.pow(yb,xa)%p;
794 System.out.println(k1);
795 System.out.println(k2);
796 if(k1==k2){
797 System.out.println("Success");
798 }
799 else
800 System.out.println("Check");
801
802 }
803 }