Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ecckrypt;
- import java.math.BigInteger;
- class PECC
- {
- BigInteger x,y,a,b,q;
- PECC(BigInteger x, BigInteger y, BigInteger a, BigInteger b, BigInteger q)
- {
- this.a = a;
- this.b = b;
- this.x = x;
- this.y = y;
- this.q = q;
- }
- }
- public class ECCKRYPT {
- public static BigInteger ODW(BigInteger m, BigInteger n)
- {
- BigInteger a, ap, s, sp, t, tp, r, q;
- BigInteger L1 = new BigInteger("1"), L0=new BigInteger("0");
- a = m;
- ap = n;
- s = L1;
- sp=L0;
- t=L0;
- tp = L1;
- while(ap.compareTo(L0) !=0)
- {
- q = a.divide(ap);
- r = a; a=ap; ap=r.subtract(q.multiply(ap));
- r = s; s=sp; sp=r.subtract(q.multiply(sp));
- r = t; t=tp; tp=r.subtract(q.multiply(tp));
- }
- if (t.compareTo(L0)>0) r=t;
- else r=t.add(n);
- return r;
- }
- public static PECC DOD(PECC P1, PECC P2)
- {
- BigInteger x1, x2, x3, y1, y2, y3, lam, q, dwa, trzy;
- x1 = P1.x;
- x2 = P2.x;
- x3 = new BigInteger("0");
- y1 = P1.y;
- y2 = P2.y;
- y3 = new BigInteger("0");
- dwa = new BigInteger("2");
- trzy = new BigInteger("3");
- q = P1.q;
- if (x1.compareTo(x2)!=0)
- {
- lam = x2.subtract(x1);
- lam = ODW(lam, P1.q);
- lam = lam.multiply(y2.subtract(y1));
- lam = lam.mod(q);
- x3 = lam.multiply(lam);
- x3 = (x3.subtract(x1)).subtract(x2);
- x3 = x3.mod(q);
- y3 = (x1.subtract(x3)).multiply(lam);
- y3 = y3.subtract(y1);
- y3 = y3.mod(q);
- }
- else
- {
- if (y1.compareTo(y2)==0)
- {
- lam = ODW(dwa.multiply(y1), q);
- BigInteger r;
- r = trzy.multiply(x1.multiply(x1));
- r = r.add(P1.a);
- lam = lam.multiply(r);
- x3 = lam.multiply(lam);
- x3 = x3.subtract(dwa.multiply(x1));
- x3 = x3.mod(q);
- y3 = (x1.subtract(x3)).multiply(lam);
- y3 = y3.subtract(y1);
- y3 = y3.mod(q);
- }
- }
- return new PECC (x3, y3, P1.a, P1.b, q);
- }
- public static PECC MNOZENIE(int k, PECC P)
- {
- PECC R = P;
- for (int j=0; j<k; j++)
- {
- R = DOD(R, P);
- }
- return R;
- }
- public static BigInteger SLegendrea(BigInteger a, BigInteger p) {
- BigInteger jeden = new BigInteger("1");
- BigInteger dwa = new BigInteger("2");
- return (a.modPow((p.subtract(jeden)).divide(dwa),p));
- }
- public static BigInteger pierw(BigInteger r, BigInteger q)
- {
- BigInteger L1 = new BigInteger("1");
- BigInteger L2 = new BigInteger("2");
- BigInteger r1 = L1;
- while((r1.multiply(r1)).compareTo(r)!=0)
- r1.add(L1);
- return r1;
- }
- public static void Wyswietlanie (PECC P)
- {
- System.out.print("( "+P.x+" , "+P.y+" )");
- }
- public static void main(String[] args) {
- BigInteger p, q, a, b, L1, L2, L10, r1, r2, x, y;
- int n = 5;
- p = new BigInteger("3");
- p = p.nextProbablePrime();
- L2 = new BigInteger("2");
- L1 = new BigInteger("1");
- L10 = new BigInteger("10");
- q = p.pow(n);
- q = q.add(L1);
- a = L1;
- b = L2.pow(n);
- b = b.nextProbablePrime();
- while (b.compareTo(q)>0)
- {
- b = b.divide(L2);
- b = b.nextProbablePrime();
- }
- System.out.println("Równanie krzywej eliptycznej:\n" + "(y^2=x^3+x+"+b+") mod " + q);
- /*Znalezienie pktu krzywej ECC
- ustalenie mnoznika ka
- znalezienie P do przeslania*/
- x = L1;
- do
- {
- r1 = ((x.pow(3)).add(x)).add(b);
- r1 = r1.mod(q);
- r2 = SLegendrea(r1,q);
- //System.out.println("SL="+r2+" x="+x);
- x=x.add(L1);
- } while(r2.compareTo(L1)!=0);
- System.out.println("SL=" + r2+" x="+x);
- y=pierw(r1,q);
- x.subtract(L1);
- System.out.println("x = " + x +" y="+y);
- int k=123466;
- k = (int)Math.round(k*Math.random());
- BigInteger kk=new BigInteger (Long.toString(k));
- PECC P=new PECC(x,y,a,b,q);
- P= MNOZENIE(k,P);
- System.out.println("Klucz do szyfrowania ECC :");
- Wyswietlanie(P);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement