Advertisement
Guest User

Laborator6 SSC

a guest
Nov 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package laborator6;
  7.  
  8. //import java.math.BigInteger;
  9. import java.math.*;
  10.  
  11. /**
  12. *
  13. * @author student
  14. */
  15. public class Laborator6 {
  16.  
  17. /**
  18. * @param args the command line arguments
  19. */
  20. private BigInteger e;
  21. private BigInteger n;
  22. private BigInteger d;
  23.  
  24. //recursively searches for the sqr root of a in interval [left, right]
  25. private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,
  26. BigInteger right)
  27. {
  28. // fix root as the arithmetic mean of left and right
  29. BigInteger root = left.add(right).shiftRight(1);
  30. // if the root is not between [root, root+1],
  31. //is not an integer and root is our best integer approximation
  32. if(!((root.pow(2).compareTo(a) == -
  33. 1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
  34. if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root,
  35. right);
  36. if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left,
  37. root);
  38. }
  39. return root;
  40. }
  41.  
  42. public static BigInteger SquareRoot(BigInteger a)
  43. {
  44. return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
  45. }
  46. /*
  47. public BigInteger Power (){
  48.  
  49. BigInteger numar = new BigInteger("17");
  50. BigInteger putere = new BigInteger("-1");
  51. return Math.pow(numar, putere));
  52.  
  53. }*/
  54.  
  55.  
  56. public Laborator6() {
  57. this.e = new BigInteger("7");
  58. this.n = new BigInteger("837210799");
  59. this.d = new BigInteger("478341751");
  60. }
  61. private BigInteger PrivateKey(BigInteger e1){
  62. BigInteger delta = new BigInteger("9360562500");
  63. BigInteger k = new BigInteger("0");
  64. BigInteger sum = new BigInteger("112736");
  65. BigInteger dcautat = new BigInteger("0");
  66.  
  67. //se calculeaza k dupa formula (d*e-1)/n
  68. k = this.d.multiply(e).subtract(new BigInteger("1")).divide(this.n);
  69. if(this.d.multiply(e).subtract(new BigInteger("1")).mod(n).compareTo(new BigInteger("0"))!= 0)
  70. k = k.add(new BigInteger("1"));
  71.  
  72. System.out.println(k);
  73. sum=((new BigInteger("4").multiply((this.n).add(new BigInteger("1")))).add( new BigInteger("1").substract(new BigInteger("7").multiply(this.d) ) ));
  74.  
  75. BigInteger root1 = new BigInteger("0");
  76. BigInteger root2 = new BigInteger("0");
  77.  
  78. // System.out.println("eu " + SquareRoot(new BigInteger("100")) );
  79. root1 = sum.add(SquareRoot(delta)).divide(new BigInteger("2"));
  80. root2 = sum.subtract(SquareRoot(delta)).divide(new BigInteger("2"));
  81.  
  82. System.out.println("radacina 1 are valoaea: " + root1);
  83. System.out.println("radacina 2 are valoaea: " + root2);
  84.  
  85. dcautat= (new BigInteger("17").pow(-1)).mod((this.d).multiply(this.e).subtract(new BigInteger("1").divide(k)));
  86. return dcautat;
  87. }
  88. public static void main(String[] args) {
  89. // TODO code application logic here
  90. Laborator6 m = new Laborator6();
  91. Laborator6 g = new Laborator6();
  92. BigInteger e1 =new BigInteger("19");
  93. BigInteger e2 = new BigInteger("17");
  94. BigInteger resultm = m.PrivateKey(e2);
  95. BigInteger resultg = g.PrivateKey(e1);
  96. System.out.println("The private key is: " + resultg);
  97. System.out.println("The private key is: " + resultm);
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement