Advertisement
Guest User

Untitled

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