Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 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 ecckrypt;
  7. import java.math.BigInteger;
  8.  
  9. /**
  10. *
  11. * @author Student
  12. */
  13. class PECC
  14. {
  15. BigInteger x,y,a,b,q;
  16. PECC(BigInteger x, BigInteger y, BigInteger a, BigInteger b, BigInteger q)
  17. {
  18. this.a = a;
  19. this.b = b;
  20. this.x = x;
  21. this.y = y;
  22. this.q = q;
  23. }
  24. }
  25. public class ECCKRYPT {
  26.  
  27.  
  28. /**
  29. * @param args the command line arguments
  30. */
  31. public static BigInteger ODW(BigInteger m, BigInteger n)
  32. {
  33. BigInteger a, ap, s, sp, t, tp, r, q;
  34. BigInteger L1 = new BigInteger("1"), L0=new BigInteger("0");
  35. a = m;
  36. ap = n;
  37. s = L1;
  38. sp=L0;
  39. t=L0;
  40. tp = L1;
  41.  
  42. while(ap.compareTo(L0) !=0)
  43. {
  44. q = a.divide(ap);
  45. r = a; a=ap; ap=r.subtract(q.multiply(ap));
  46. r = s; s=sp; sp=r.subtract(q.multiply(sp));
  47. r = t; t=tp; tp=r.subtract(q.multiply(tp));
  48. }
  49. if (t.compareTo(L0)>0) r=t;
  50. else r=t.add(n);
  51. return r;
  52. }
  53.  
  54. public static PECC DOD(PECC P1, PECC P2)
  55. {
  56. BigInteger x1, x2, x3, y1, y2, y3, lam, q, dwa, trzy;
  57. x1 = P1.x;
  58. x2 = P2.x;
  59. x3 = new BigInteger("0");
  60.  
  61. y1 = P1.y;
  62. y2 = P2.y;
  63. y3 = new BigInteger("0");
  64.  
  65. dwa = new BigInteger("2");
  66. trzy = new BigInteger("3");
  67. q = P1.q;
  68.  
  69.  
  70. if (x1.compareTo(x2)!=0)
  71. {
  72. lam = x2.subtract(x1);
  73. lam = ODW(lam, P1.q);
  74. lam = lam.multiply(y2.subtract(y1));
  75. lam = lam.mod(q);
  76. x3 = lam.multiply(lam);
  77. x3 = (x3.subtract(x1)).subtract(x2);
  78. x3 = x3.mod(q);
  79. y3 = (x1.subtract(x3)).multiply(lam);
  80. y3 = y3.subtract(y1);
  81. y3 = y3.mod(q);
  82.  
  83. }
  84.  
  85. else
  86. {
  87. if (y1.compareTo(y2)==0)
  88. {
  89. lam = ODW(dwa.multiply(y1), q);
  90. BigInteger r;
  91. r = trzy.multiply(x1.multiply(x1));
  92.  
  93. r = r.add(P1.a);
  94. lam = lam.multiply(r);
  95. x3 = lam.multiply(lam);
  96. x3 = x3.subtract(dwa.multiply(x1));
  97. x3 = x3.mod(q);
  98. y3 = (x1.subtract(x3)).multiply(lam);
  99. y3 = y3.subtract(y1);
  100. y3 = y3.mod(q);
  101.  
  102. }
  103.  
  104. }
  105. return new PECC (x3, y3, P1.a, P1.b, q);
  106.  
  107. }
  108. public static PECC MNOZENIE(BigInteger k, PECC P)
  109. {
  110. BigInteger i = new BigInteger("1");
  111. BigInteger jeden = i;
  112. PECC R = P;
  113. while(i.compareTo(k) < 0)
  114. {
  115. R = DOD(R, P);
  116. i.add(jeden);
  117. }
  118. return R;
  119. }
  120.  
  121. public static BigInteger SLegendrea(BigInteger a, BigInteger p) {
  122. BigInteger jeden = new BigInteger("1");
  123. BigInteger dwa = new BigInteger("2");
  124.  
  125. return (a.modPow((p.subtract(jeden)).divide(dwa),p));
  126. }
  127.  
  128. public static BigInteger pierw(BigInteger r, BigInteger q){
  129. BigInteger L1 = new BigInteger("1");
  130. BigInteger L2 = new BigInteger("2");
  131. BigInteger r1 = r.divide(L2);
  132. while((r1.multiply(r1)).compareTo(r)>0)
  133. r1.subtract(L1);
  134. return r1;
  135. }
  136. void Wyswietlanie (PECC P)
  137. {
  138. System.out.print("( "+P.x+" , "+P.y+" )");
  139. }
  140.  
  141. public static void main(String[] args) {
  142.  
  143.  
  144. BigInteger p, q, a, b, L1, L2, L10, x, r1, r2, y;
  145. int n = 177;
  146.  
  147.  
  148. p = new BigInteger("3");
  149. p = p.nextProbablePrime();
  150.  
  151. L2 = new BigInteger("2");
  152. L1 = new BigInteger("1");
  153. L10 = new BigInteger("10");
  154. q = p.pow(n);
  155. a = L1;
  156. b = L2;
  157. b = b.nextProbablePrime();
  158. while (b.compareTo(q)>0)
  159. {
  160. b = b.divide(L2);
  161. b = b.nextProbablePrime();
  162. }
  163.  
  164. System.out.println("Rownanie krzywej eleptycznej:\n" +
  165. "(y^2=x^3+x"+b+") mod "+q);
  166.  
  167. do {
  168. x = q.divide(L10);
  169. r1 = ((x.pow(3)).add(x)).add(b);
  170. r1 = r1.mod(q);
  171. r2 = SLegendrea(r1,q);
  172. } while (r2.compareTo(L1)!=0);
  173. y = pierw(r1,q);
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement