Friday 1 April 2016

Dual Generalized Rebalanced - RSA Java Implementation

Rebalanced RSA is one of the best variant of RSA key generation algorithm & 
Using Dual Generalized Rebalanced - RSA,  there is very less (could be said as 0) probability to crack Key(Public Key & Private Key) using Brute Force attack.
Here is Java implementation of it.

For more details about Dual RSA:




import java.math.BigInteger;
import java.security.SecureRandom;

/*
 * 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.
 */

/**
 *
 * @author 311
 */
public class RebalancedRSA {
    
    public static void main(String[] args) {
        
   
    int ne=50,nd=75,nk=25,n=200;//ne<n and n/2-ne = nd - nk
    BigInteger e = new BigInteger(ne,new SecureRandom());
    BigInteger temp = new BigInteger("65536");
    BigInteger two = new BigInteger("2");
    if(e.mod(two).equals(BigInteger.ZERO))
    {
        //System.out.println("e if " + e);
        e = e.add(BigInteger.ONE);
    }
    //System.out.println("e " + e);
    int k = (n/2 - ne)/nk;
    BigInteger pa[] = new BigInteger[k];
    for(int i=1;i<=k-1;i++) // we are starting array index from 1
    {
        pa[i] = new BigInteger(n/2-ne,new SecureRandom());
    }
    BigInteger paa = new BigInteger(n/2-ne,new SecureRandom());
    
   while(true)
   {
    paa = new BigInteger(n/2-ne,new SecureRandom());
    temp = paa.mod(two);
    boolean b;
    b = temp.equals(BigInteger.ZERO);
    if(!b){
        paa = paa.add(BigInteger.ONE);
    }
    temp = paa.gcd(e);
    //   System.out.println("temp " + temp);
    if((temp.equals(BigInteger.ONE)))
        break;
   }
    /*  System.out.println("e = " + e);
        System.out.println("paa = "+ paa);
        System.out.println("gcd " +paa.gcd(e));*/
    BigInteger kp1;
        BigInteger pb,dp,p1,p2;
   // while(true)
    //{
    while(true)
    {
        kp1 = new BigInteger(nk,new SecureRandom());
        temp = kp1.gcd(e);
        if((temp.equals(BigInteger.ONE)))
            break;
    }
   // System.out.println("kp1 = " + kp1 + " " + nk );
    while(true)
    {
    int i=0;
   
        pb = new BigInteger(ne,new SecureRandom());
        if(pb.compareTo(e) == -1) // if pb < e then continue 
            continue;
        temp = e.multiply(two);
        if(pb.compareTo(temp)==1) // if pb > 2e then continut
            continue;
        // e < pb < 2e is satisfied
        temp = pb.multiply(kp1);
        temp = temp.multiply(paa); //kp1*pa*pb+1
        temp = temp.add(BigInteger.ONE);
        dp = temp.divide(e);
        BigInteger t1,t2;
        t1 = kp1.multiply(paa); //t1 = kp1*pa
        t2 = t1.multiply(two);  //t2 = 2*kp1*pa
        if(t1.compareTo(dp)==-1) //if t1 < dp
        {
            if(t2.compareTo(dp)==1)  // & dp < 2t1 == correct dp
            {
                
            }
        }
        else
        {
            continue;
        }
        
        p1 = paa.multiply(pb);
        p1 = p1.add(BigInteger.ONE);
        if(p1.isProbablePrime(1))
        {
           
        }
        else
        {
            continue;
        }
        int flag=0;
        p2 = new BigInteger("1");
        for(i=1;i<=k-1;i++)
        {
            temp = paa.multiply(pb);
            temp = temp.multiply(kp1);
            //System.out.println("temp = " + temp);
            //System.out.println("pa[0] = " + pa[i]);
            temp = temp.divide(pa[i]);
            p2 = temp.add(BigInteger.ONE);
            //System.out.println("p2 " + p2);
            if(p2.isProbablePrime(1))
            {
                //System.out.println("NO break");
            }
            else
            {
                flag = 1;
              //  System.out.println("break");
                break;
            }
        }
        if(flag==0)
            break;
    }
        
    
    
    
    
    
    
    
    BigInteger qa[] = new BigInteger[k];
    for(int i=1;i<=k-1;i++) // we are starting array index from 1
    {
        qa[i] = new BigInteger(n/2-ne,new SecureRandom());
    }
    BigInteger qaa = new BigInteger(n/2-ne,new SecureRandom());
    
   while(true)
   {
    qaa = new BigInteger(n/2-ne,new SecureRandom());
    temp = qaa.mod(two);
    boolean b;
    b = temp.equals(BigInteger.ZERO);
    if(!b){
        qaa = qaa.add(BigInteger.ONE);
    }
    temp = qaa.gcd(e);
    //   System.out.println("temp " + temp);
    if((temp.equals(BigInteger.ONE)))
        break;
   }
    /*  System.out.println("e = " + e);
        System.out.println("paa = "+ qaa);
        System.out.println("gcd " +qaa.gcd(e));*/
    BigInteger kq1;
        BigInteger qb,dq,q1,q2;
   // while(true)
    //{
    while(true)
    {
        kq1 = new BigInteger(nk,new SecureRandom());
        temp = kq1.gcd(e);
        if((temp.equals(BigInteger.ONE)))
            break;
    }
   // System.out.println("kq1 = " + kq1 + " " + nk );
    while(true)
    {
    int i=0;
   
        qb = new BigInteger(ne,new SecureRandom());
        if(qb.compareTo(e) == -1) // if pb < e then continue 
            continue;
        temp = e.multiply(two);
        if(qb.compareTo(temp)==1) // if pb > 2e then continut
            continue;
        // e < qb < 2e is satisfied
        temp = qb.multiply(kq1);
        temp = temp.multiply(qaa); //kp1*pa*pb+1
        temp = temp.add(BigInteger.ONE);
        dq = temp.divide(e);
        BigInteger t1,t2;
        t1 = kq1.multiply(qaa); //t1 = kp1*pa
        t2 = t1.multiply(two);  //t2 = 2*kp1*pa
        if(t1.compareTo(dq)==-1) //if t1 < dp
        {
            if(t2.compareTo(dq)==1)  // & dp < 2t1 == correct dp
            {
                
            }
        }
        else
        {
            continue;
        }
        
        q1 = qaa.multiply(qb);
        q1 = q1.add(BigInteger.ONE);
        if(q1.isProbablePrime(1))
        {
           
        }
        else
        {
            continue;
        }
        int flag=0;
        q2 = new BigInteger("1");
        for(i=1;i<=k-1;i++)
        {
            temp = qaa.multiply(qb);
            temp = temp.multiply(kq1);
            //System.out.println("temp = " + temp);
            //System.out.println("qa[0] = " + qa[i]);
            temp = temp.divide(qa[i]);
            q2 = temp.add(BigInteger.ONE);
            //System.out.println("q2 " + q2);
            if(q2.isProbablePrime(1))
            {
                //System.out.println("NO break");
            }
            else
            {
                flag = 1;
               // System.out.println("break");
                break;
            }
        }
        if(flag==0)
            break;
    }
    
    
       
   BigInteger N1,N2,kp2,kq2;
   N1 = p1.multiply(q1);
   N2 = p2.multiply(q2);
   kp1 = pa[1];
   kq1 = pa[1];
   
        System.out.println("N1 = " + N1);
        System.out.println("N2 = " + N2);
        System.out.println("e  = " + e);
        
         System.out.println("dp = " + dp);
        System.out.println("dq = " + dq);
        System.out.println("p1 = " + p1);
        System.out.println("q1 = " + q1);
        System.out.println("p2 = " + p2);
        System.out.println("q2 = " + q2);
        
        
        
   
   
    
    }
    
    
    
    
    
       
    
 }
     
    
    
    
    
    
    
    
    
    

 

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...