hi. i recently implemented S-DES algo. Here is the code for key generation.

import java.math.*;
import java.security.*;

public class keyGenerator {
    
        int bitLength=10;
        int certainty=20;
        int i=0,j=0,tempInt1,tempInt2;
        long l;
        byte[] actual10Key=new byte[10];
        byte[] permuted10Key=new byte[10];
        byte[] permuted10KeyCopy=new byte[10];
        byte[] cls10Temp1=new byte[10];
        byte[] subKey8Temp=new byte[8];
        byte[] subKey1=new byte[8];
        byte[] subKey2=new byte[8];
        
        byte tempByte1;
        int [] p10={3,5,2,7,4,10,1,9,8,6};
        int [] p8={6,3,7,4,8,5,10,9};
        BigInteger bigIntKey;
        //BigInteger clsBigIntKey;
        //BigInteger tempBig1;
    
    
    
    public keyGenerator(){
    
                
        /******************************************/
        /*this block generates a random PRIME, converts 
        it to binary and stores it as an array*/
        /*******************************************/
        
        SecureRandom sr=new SecureRandom();
        bigIntKey=new BigInteger(bitLength,certainty,sr);
        int no=bigIntKey.intValue();
        String s=Integer.toBinaryString(no);
        char c[]=s.toCharArray();
               
        while(i<10){
            tempInt1=c[i];
            if(tempInt1==48)
                tempInt1-=48;
            else if(tempInt1==49)
                tempInt1-=48;
            actual10Key[i]=(byte) tempInt1;
            i++;
        }
        System.out.println("\n\n\n");
        System.out.println("actual key :  "+bigIntKey);
        System.out.print("actual 10 bit key :    ");
        for(i=0;i<10;i++){
                    System.out.print(actual10Key[i]+"  ");
        }
        
        System.out.println();
        System.out.print("permuted 10 bit key :  ");
        
        for(i=0;i<10;i++){                          //10 bit key permutation
            tempInt1=p10[i];
            permuted10Key[i]=actual10Key[tempInt1-1];
            permuted10KeyCopy[i]=permuted10Key[i];
            System.out.print(permuted10Key[i]+"  ");
        }
        System.out.println();              
        subKeyGenerator();
            
        
    }               //end of constructor
    
    
    /*********************************************/
    /*******used for circular left shift**********/
    /*********************************************/
    void cls(int noOfCls){
         
        for(i=0;i<10;i++)
            cls10Temp1[i]=permuted10Key[i];
        
        for(j=0;j<noOfCls;j++)
        {               
             tempByte1=cls10Temp1[0];
             for(i=0;i<9;i++)
                 cls10Temp1[i]=cls10Temp1[i+1];
             cls10Temp1[i]=tempByte1;
             
                        
        }
        System.out.print("cls temp1      :       ");
        for(i=0;i<10;i++){
                System.out.print(cls10Temp1[i]+"  ");
            }
    }
    
    
    /****************************************/
    /********creates two sub keys************/
    /****************************************/
    
    byte[] subKeyInitiator(int clsNo){
        cls(clsNo);
        for(i=0;i<8;i++){
            tempInt2=p8[i];
            subKey8Temp[i]=cls10Temp1[tempInt2-1];
        }
        return subKey8Temp;
    }
    
    void subKeyGenerator(){
        subKey1=subKeyInitiator(1);
        System.out.println();
        System.out.print("subkey 1       :       ");
        
        for(i=0;i<8;i++)
                System.out.print(subKey1[i]+"  ");
        System.out.println();
        subKey2=subKeyInitiator(3);
        System.out.println();
        System.out.print("subkey 2      :        ");
        for(i=0;i<8;i++)
                System.out.print(subKey2[i]+"  ");        
    
    
    }
    
    
  public static void main(String args[]) throws java.lang.ArrayIndexOutOfBoundsException{
      keyGenerator t=new keyGenerator();
      
  }
}

can anyone please suggest some good modifications so as to make the code a bit simpler to implement??? this code has gone way out of hand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.