fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.math.BigInteger;
  4.  
  5. /**
  6.  * Testing prime factor decryption
  7.  *
  8.  * @author Emmanuel
  9.  */
  10. public class Main {
  11.  
  12. /**
  13.   * Main method
  14.   *
  15.   * @param args command line arguments
  16.   */
  17. public static void main(String[] args) {
  18. // private primes and totient
  19. BigInteger p1, p2, phi;
  20. p1 = new BigInteger("67");
  21. p2 = new BigInteger("23");
  22. phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
  23. System.out.println("Prime 1: " + p1);
  24. System.out.println("Prime 2: " + p2);
  25. System.out.println("Euler's Totient: " + phi);
  26.  
  27. // prime product (modulus) and public exponent
  28. BigInteger pp, ep;
  29. pp = p1.multiply(p2);
  30. //ep = new BigInteger("65537");
  31. BigInteger f = BigInteger.valueOf(2L);
  32. ep = coprime(phi, phi.divide(f).add(f));
  33. System.out.println("Modulus: " + pp);
  34. System.out.println("Public Exponent: " + ep);
  35. System.out.println("Private Exponent: " + ep.modInverse(phi));
  36.  
  37. // test encryption
  38. for(int i = 1; i <= 100; i++) {
  39. BigInteger c = BigInteger.valueOf((long) i);
  40. System.out.println(i + " => " + c.modPow(ep, pp));
  41. }
  42. }
  43.  
  44. /**
  45.   * Calculate the next co-prime of number with minimum defined
  46.   *
  47.   * @param n the number to check for co-primes
  48.   * @param i the minimum to start checking from
  49.   * @return the next co-prime or NULL if none is found
  50.   */
  51. private static BigInteger coprime(BigInteger n, BigInteger i) {
  52. for (; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
  53. if (i.gcd(n).equals(BigInteger.ONE)) {
  54. return i;
  55. }
  56. }
  57. return null;
  58. }
  59. }
  60.  
Success #stdin #stdout 0.12s 321216KB
stdin
Standard input is empty
stdout
Prime 1: 67
Prime 2: 23
Euler's Totient: 1452
Modulus: 1541
Public Exponent: 731
Private Exponent: 1307
1 => 1
2 => 32
3 => 243
4 => 1024
5 => 43
6 => 71
7 => 1397
8 => 407
9 => 491
10 => 1376
11 => 787
12 => 731
13 => 1453
14 => 15
15 => 1203
16 => 696
17 => 596
18 => 302
19 => 1253
20 => 884
21 => 451
22 => 528
23 => 1127
24 => 277
25 => 308
26 => 266
27 => 656
28 => 480
29 => 439
30 => 1512
31 => 453
32 => 698
33 => 157
34 => 580
35 => 1513
36 => 418
37 => 498
38 => 30
39 => 190
40 => 550
41 => 739
42 => 563
43 => 125
44 => 1486
45 => 1080
46 => 621
47 => 1059
48 => 1159
49 => 703
50 => 610
51 => 1515
52 => 807
53 => 454
54 => 959
55 => 1480
56 => 1491
57 => 902
58 => 179
59 => 464
60 => 613
61 => 398
62 => 627
63 => 182
64 => 762
65 => 839
66 => 401
67 => 1072
68 => 68
69 => 1104
70 => 645
71 => 354
72 => 1048
73 => 1277
74 => 526
75 => 876
76 => 960
77 => 706
78 => 1457
79 => 1468
80 => 649
81 => 685
82 => 533
83 => 1165
84 => 1065
85 => 972
86 => 918
87 => 348
88 => 1322
89 => 930
90 => 658
91 => 344
92 => 1380
93 => 668
94 => 1527
95 => 1485
96 => 104
97 => 641
98 => 922
99 => 1167
100 => 1028