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 laborator6;
- //import java.math.BigInteger;
- import java.math.*;
- /**
- *
- * @author student
- */
- public class Laborator6 {
- /**
- * @param args the command line arguments
- */
- private BigInteger e;
- private BigInteger n;
- private BigInteger d;
- //recursively searches for the sqr root of a in interval [left, right]
- private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,
- BigInteger right)
- {
- // fix root as the arithmetic mean of left and right
- BigInteger root = left.add(right).shiftRight(1);
- // if the root is not between [root, root+1],
- //is not an integer and root is our best integer approximation
- if(!((root.pow(2).compareTo(a) == -
- 1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
- if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root,
- right);
- if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left,
- root);
- }
- return root;
- }
- public static BigInteger SquareRoot(BigInteger a)
- {
- return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
- }
- /*
- public BigInteger Power (){
- BigInteger numar = new BigInteger("17");
- BigInteger putere = new BigInteger("-1");
- return Math.pow(numar, putere));
- }*/
- public Laborator6() {
- this.e = new BigInteger("7");
- this.n = new BigInteger("837210799");
- this.d = new BigInteger("478341751");
- }
- private BigInteger PrivateKey(BigInteger e1){
- BigInteger delta = new BigInteger("9360562500");
- BigInteger k = new BigInteger("0");
- BigInteger sum = new BigInteger("112736");
- BigInteger dcautat = new BigInteger("0");
- //se calculeaza k dupa formula (d*e-1)/n
- k = this.d.multiply(e).subtract(new BigInteger("1")).divide(this.n);
- if(this.d.multiply(e).subtract(new BigInteger("1")).mod(n).compareTo(new BigInteger("0"))!= 0)
- k = k.add(new BigInteger("1"));
- System.out.println(k);
- sum=((new BigInteger("4").multiply((this.n).add(new BigInteger("1")))).add( new BigInteger("1").substract(new BigInteger("7").multiply(this.d) ) ));
- BigInteger root1 = new BigInteger("0");
- BigInteger root2 = new BigInteger("0");
- // System.out.println("eu " + SquareRoot(new BigInteger("100")) );
- root1 = sum.add(SquareRoot(delta)).divide(new BigInteger("2"));
- root2 = sum.subtract(SquareRoot(delta)).divide(new BigInteger("2"));
- System.out.println("radacina 1 are valoaea: " + root1);
- System.out.println("radacina 2 are valoaea: " + root2);
- dcautat= (new BigInteger("17").pow(-1)).mod((this.d).multiply(this.e).subtract(new BigInteger("1").divide(k)));
- return dcautat;
- }
- public static void main(String[] args) {
- // TODO code application logic here
- Laborator6 m = new Laborator6();
- Laborator6 g = new Laborator6();
- BigInteger e1 =new BigInteger("19");
- BigInteger e2 = new BigInteger("17");
- BigInteger resultm = m.PrivateKey(e2);
- BigInteger resultg = g.PrivateKey(e1);
- System.out.println("The private key is: " + resultg);
- System.out.println("The private key is: " + resultm);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement