Проектирование специализированного микроконтроллера, реализующего полнофункциональную RSA схему шифрования, страница 22

    {

               p = p.subtract(BigInteger.ONE);

               BigInteger two = new BigInteger("2");

               BigInteger neg = new BigInteger("-1");

               BigInteger [] rt ={BigInteger.ZERO, BigInteger.ZERO}; // rt = {q, m}

               if (p.mod(two).compareTo(BigInteger.ZERO) != 0)

               {

                              rt[0] = neg; rt[1] = neg;

                              return rt;

               }

               BigInteger divisor = p.divide(two);

               BigInteger counter = BigInteger.ONE;

               //double maxq = (Math.log(p))/(Math.log(2));

               while (/*count <= maxq && */divisor.mod(two).compareTo(BigInteger.ZERO)==0)

               {

                              counter = counter.add(BigInteger.ONE);

                              divisor = divisor.divide(two);

               }

               rt[0] = counter; rt[1] = divisor;

               return rt;

    }

}

public class myRSA

{

    public static void main(String[] args)

    {

               BigInteger N, D, E, P, Q, M;

               // First task is to find P and Q : Primes with a specifies bit length

               //System.out.print("Enter Bit length   \t:");

        //int bitlen = System.in.readInt();

          int bitlen = 2048 ;

               MillerRabin test = new MillerRabin();

               P = test.createPrime(bitlen/2, 100);

               Q = test.createPrime(bitlen/2, 100);

               // P and Q must not be equal

               while(P.compareTo(Q) == 0)

               {

                              Q = test.createPrime(bitlen/2, 100);

               }

               System.out.println("P               \t:"+P.toString(16));

               System.out.println("Q               \t:"+Q.toString(16));

               // N is the product of P and Q

               N = P.multiply(Q);

               System.out.println("N = P*Q           \t:"+N.toString(16));

               // M is the product of (P-1) and (Q-1)

               M = (P.subtract(BigInteger.ONE)).multiply(Q.subtract(BigInteger.ONE));

               System.out.println("M = (P-1)*(Q-1)     \t:"+M.toString(16));

               // The public key E must be coprime to M

               E = new BigInteger("3");

               while(M.gcd(E).intValue() > 1)

               {

                              E = E.add(new BigInteger("2"));

               }

               System.out.println("E                \t:"+E.toString(16));

               // The private key D must satisfy DE = 1 mod M

               BigExtendedEuclid ee = new BigExtendedEuclid();

               BigInteger [] arra = ee.EE(E, M, BigInteger.ZERO, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO);

               System.out.println("gcd is :" +arra[0] + " = "+arra[1] + "E + "+arra[2]+"M");

               D = arra[1];

               while (D.compareTo(BigInteger.ZERO)<0)

               {

                              D = D.add(M);

               }

               System.out.println("D                \t:"+D.toString(16));

               //System.out.print("Enter integer to be encrypted  :");

               //BigInteger big = new BigInteger(System.in.readString());

               BigInteger big = new BigInteger("76901036849173733f27cc96762e1757c5de96983f5f4cee8032554ebc3e78928696dfdc1b348e53f657e230e16d8061f8d0f2000000000000000000000000ff", 16);

               // Encryption process: en = big^{E} mod N

               BigInteger en = big.modPow(E, N);

               System.out.println("Encrypted number \t:"+en.toString(16));

               // Decryption process: de = en^{D} mod N

               BigInteger de = en.modPow(D, N);

               System.out.println("Decrypted number \t:"+de.toString(16));

               if (big.compareTo(de) == 0)System.out.println("Success!");

               else System.out.println("Not Equal");

    }

}