Cryptography-Digest Digest #383, Volume #11      Tue, 21 Mar 00 21:13:01 EST

Contents:
  Re: DES Decryption Problem (Chuah Seong Ping)

----------------------------------------------------------------------------

Date: Wed, 22 Mar 2000 09:41:33 +0800
From: Chuah Seong Ping <[EMAIL PROTECTED]>
Subject: Re: DES Decryption Problem

This is a multi-part message in MIME format.
==============C3983F32AD62A675A324C600
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Can you help me to debug the bug in my program?
I can't find it ,I had tried it very much?
I am here attached my program for references.
Thank you very much
James

James Muir wrote:

> > I suspect your encryption process has a bug. You should check it
> > against test vectors. Check that:
> >
> > DES_encrypt ( 4e6f772069732074 ) = 3fa40e8a984d4815
>
> when the key is K = 0123456789abcdef
>
> -James
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

==============C3983F32AD62A675A324C600
Content-Type: text/plain; charset=us-ascii;
 name="des.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="des.java"

/*This program will encrypt a message into binary representations from
  
  the input file pro.txt into cip.txt in binary forms using Electronic Code Book 
  
  mode (ECB) for DES encryption*/ 


import java.io.*;
import java.lang.*;
import java.util.*;

public class des
{    
  static int i,j,k,q,f,w,m,n,x,p,e,h,bitomit,y,bitsomit;
  static long xor,xxor;
  static String str64,s,l,r,str48,str6,str32,cipher,cipherstr,str8,ciph;
  static char[] bit = new char[64];
  static char[] cipherchar = new char[64];
  static String []L = new String[17];
  static String []R = new String[17];
  static String []B = new String[8];
  static String []Bsub = new String[8];
  static String []subkeys = new String[17];
  static StringBuffer s5 = new StringBuffer();
  static ArrayList d = new ArrayList();
  static char []PB = new char[32];
  static char []Re = new char[48];
  
  static encryption en = new encryption();
  static Key permutedkey = new Key();
  
 public static void main(String args[])         
       throws Exception 
 {      
    int []IP = 
{58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,
                    
56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,
                                37,29,21,13,5,63,55,47,39,31,23,15,7};
    int []EP = {32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,
                    19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1}; 
        int []P =  
{16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,
                    30,6,22,11,4,25};
    int []FP = 
{40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,
                    
5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,
                                10,50,18,58,26,33,1,41,9,49,17,57,25};
        ArrayList c = new ArrayList();
    //OutputStream f1 = new FileOutputStream("cipher.txt");
    FileWriter fo = new FileWriter("cip.txt");
  
    
    subkeys = permutedkey.binarykey();
        c = en.blocks64bits();
        
        Object br[] = c.toArray();
        
    for(i=0; i<br.length; ++i)
    {  
                
                str64 =(String.valueOf(br[i])).toString();
                //System.out.print("\n64bits string is:" + str64 + "   ");
            char ch64bits[] = str64.toCharArray();          //starts Initial 
Permutation             
                                                       //On 64bits binarymesseges
                for (q=0; q<64; ++q)
             {     
                   /*if(q==24)
                    bit[q] = ch64bits[IP[q]-64];                        
                   else*/
                bit[q] = ch64bits[IP[q]-1];                
         }
           
           String s = new String(bit);
           //System.out.print(" Message after IP :" + s); 
           l = s.substring(0,32);   
       r = s.substring(32,64);
           L[0] = l;              
       R[0] = r;
           //System.out.print("\nLeft side of IP : \n"  + L[0] + "  ");
           //System.out.print("\nRight side of IP : \n"  + R[0] + "  ");
           
           for (w=1; w<=16; ++w)
       {                              
                  
                  /*System.out.print("\nLeft side of IP :\t L[" + (w-1) + "] :" + 
L[w-1] + "  ");
                  System.out.println("\n");
              System.out.print("\nRight side of IP :\t R[" + (w-1) + "] :" + R[w-1] + 
"  ");
          System.out.println("\n"); */
                  char ch32bits[] = R[w-1].toCharArray();    //expand the 32bits of 
right half R[w-1]
                                                            //into 48bits before 
Exclusive-or with subkeys             
          
                  for (k=0; k<48; ++k)                                  
          {
                   /*if((k==0)||(k==46))
                    Re[k] = ch32bits[EP[k]-32];                 
                   else*/
                Re[k] = ch32bits[EP[k]-1]; 
          }
                  
                  String u = new String(Re);
                   //System.out.print("\nAfter Expansion to 48 " + u + "\n");
                  try{
                      xor = (Long.parseLong(u,2))^(Long.parseLong(subkeys[w-1],2));
                          }catch(NumberFormatException e){}; 
                  str48 = Long.toBinaryString(xor);
                   //System.out.print("\nafter xor with subkeys : \n"  + str48 + "  ");
                          
                  if((str48.length())!=48)
                  {                                         //insert the bits "0" that 
are
                    bitomit = 48 - (str48.length());       //ommited during 
Exclusive-Or
                        for (p=0; p<bitomit; ++p)              //with 48bits subkeys.
                        {
                          StringBuffer s1 = new StringBuffer(str48);
                          str48 = s1.insert(0, "0").toString();  
            } 
            //System.out.print("\nafter xor with subkeys : \n"  + str48 + "  ");
                          
                        B = en.blocks6bits(str48);         //function that break the 
Exclusive-Or of 48bits into 
                                                          // 8 block of 6bits each.
                        //for (int ei=0; ei<8; ++ei)
                          //System.out.print("B[" + ei + "]" + "  " + B[ei] + "  ");

                        Bsub = en.blocks4bits(B);         //replace 6bits with 4bits 
by using substitution SBox
                        //for (int eh=0; eh<8; ++eh)
               //System.out.print("Bsub[" + eh + "]" + "  " + Bsub[eh] + "  ");

                        String Bstr="";
                        for (e=0; e<8; ++e)                    //concatenate B[1] 
through B[8]
                         Bstr = Bstr.concat(Bsub[e]);      
                          //System.out.print("Bstr is :" + Bstr + "\n ");
             

                        char B32bits[] = Bstr.toCharArray(); 
                    
                        for (y=0; y<32; ++y)        
             {
                      /*if(y==20)                      //permute the concatenation of  
                       PB[y] = B32bits[P[y]-32];                //B[1] through B[8]    
 
                      else*/
                   PB[y] = B32bits[P[y]-1]; 
             } 
                    String t = new String(PB); 
                    //System.out.print("after pemutation is :" + t + "  ");
                //System.out.print("\nLeft side of IP :\t L[" + (w-1) + "] :" + L[w-1] 
+ "  ");   
                        
                        try{                                                           
         //Exclusive -Or with L[w-1]
                         xxor = (Long.parseLong(t,2))^(Long.parseLong(L[w-1],2));
                             //System.out.print("XXOR is : " + xxor);              
                           }catch(NumberFormatException e){}; 
                    str32 = Long.toBinaryString(xxor);
                    //System.out.print("after xor with L[w-1] is :" + str32 + "  ");
                        
                        if((str32.length())!=32)
                     {                                         //insert the bits "0" 
that are
                       bitsomit = 32 - (str32.length());       //ommited during 
Exclusive-Or
                           for (n=0;n<bitsomit; ++n)              //with 32bits L[w-1].
                            {
                              StringBuffer s4 = new StringBuffer(str32);
                              str32 = s4.insert(0,"0").toString();  
                } 
               //System.out.print("after xor with L[w-1] is :" + str32 + "  ");

                          R[w] = str32;
                          L[w] = R[w-1];
             }
                         else
                         {
                          //System.out.print("after xor with L[w-1] is :" + str32 + "  
");

                          R[w] = str32;
                          L[w] = R[w-1];
                         }
          }
                  else
                  {  
                      B = en.blocks6bits(str48);  //break the Exclusive-Or of 48bits 
into 6bits
                      //for (int ei=0; ei<8; ++ei)
                            //System.out.print("B[" + ei + "]" + "  " + B[ei] + "  ");

                      Bsub = en.blocks4bits(B);        //replace B[i] with 
substitution SBox
                      //for (int eh=0; eh<8; ++eh)
                //System.out.print("Bsub[" + eh + "]" + "  " + Bsub[eh] + "  ");

                      String Bstr="";
                      for (h=0; h<8; ++h)
                        Bstr = Bstr.concat(Bsub[h]);
                            //fo.write(Bstr);
                                
                      //System.out.print("Bstr is :" + Bstr + "\n ");

                      char B32bits[] = Bstr.toCharArray(); 
                      for (y=0; y<32; ++y)        
               {
                         /*if(y==20)                        //permute the 
concatenation of
                          PB[y] = B32bits[P[y]-32];             //B[1] through B[8]    
 
                         else*/
                      PB[y] = B32bits[P[y]-1]; 
               }
                  
                       String t = new String(PB); 
                  
                        //System.out.print("after pemutation is :" + t + "  ");

                       try{                                          //Exclusive -Or 
with L[w-1]
                            xxor = (Long.parseLong(t,2))^(Long.parseLong(L[w-1],2));
                                // System.out.print("XXOR is : " + xxor);              
 
                                  }catch(NumberFormatException e){}; 
                       str32 = Long.toBinaryString(xxor);
                  
                       if((str32.length())!=32)
                        {                                         //insert the bits 
"0" that are
                          bitsomit = 32 - (str32.length());       //ommited during 
Exclusive-Or
                              for (n=0;n<bitsomit; ++n)              //with 32bits 
L[w-1]
                               {
                                  StringBuffer s4 = new StringBuffer(str32);
                                  str32 = s4.insert(0, "0").toString();  
                   } 
                               //System.out.print("after xor with L[w-1] is :" + str32 
+ "  ");

                               R[w] = str32;
                               L[w] = R[w-1];
                }
                                else
                                {
                           //System.out.print("after xor with L[w-1] is :" + str32 + " 
 ");
                   R[w] = str32;
                           L[w] = R[w-1];
                }
          }
                  
           }
          cipher = R[16].concat(L[16]);
       //System.out.print("after R[16] concatenate with L[16] :" + cipher + "  ");
          char cipher64bit[] = cipher.toCharArray();
      for (f=0; f<64; ++f)
      {
           /*if((f==6))
                cipherchar[f] = cipher64bit[FP[f]-64];                  
           else*/
             cipherchar[f] = cipher64bit[FP[f]-1];
          }
      String cipherblock64 = new String(cipherchar);
           //System.out.print("after FP pemutation is :" + cipherblock64 + "  ");
          d.add(cipherblock64); 
    }
  
  Object cpr[] = d.toArray();  
  for (x=0; x<cpr.length; ++x)
   cipherstr = s5.append(cpr[x]).toString();   
   ArrayList ei = new ArrayList();
   i = 0; 
   j = 8;
   while(j != (cipherstr.length()))    
    {   
           ciph = cipherstr.substring(i,j);
           if(ciph!= "00000000")
           ei.add(ciph);                                                          
           i = j;
           j = i + 8;
       if(j == (cipherstr.length()))
            {
          ciph = cipherstr.substring(i,j);
                  if(ciph!= "00000000")
              ei.add(ciph);
            }       
    } 
        Object pl[] = ei.toArray();
        for(int p=0; p<((pl.length)-4); ++p)
         {
          str8 =(String.valueOf(pl[p])).toString();     
          int asc = Integer.parseInt(str8,2);  
          fo.write(asc);
     }   
  //byte buf[] = cipherstr.getBytes();
  //f1.write(buf);
  fo.close();
 // fo.close();
 }
}                       
==============C3983F32AD62A675A324C600
Content-Type: text/plain; charset=us-ascii;
 name="encryption.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="encryption.java"

import java.io.*;
import java.lang.*;
import java.util.*;


public class encryption                   
{                                          
  static private int i,j,k,col,row,m;
  static private String binarymessage,binarymesj,strch,str48,str6,st;
  static encryption en = new encryption();
  static public int padded;
  
  public static String readbinarymessage() 
      throws IOException
   {
    String inLine,message,strch;
    //String filename = tf.getText.trim();
    FileReader fr = new FileReader("pro.txt"); 
    BufferedReader infile = new BufferedReader(fr);
        StringBuffer strbuf = new StringBuffer();
    StringBuffer sbuf = new StringBuffer();
        
        //FileWriter fo = new FileWriter("originalmessage1.txt");
    ArrayList a = new ArrayList();
    
      
         
                 do{         
             inLine = infile.readLine();
                         st = strbuf.append(inLine).toString();            //get 
message from user
           }while(inLine!= null);
         
                 char z[] = st.toCharArray();
         
         for(i= 0;i<z.length;++i)
          { 
            message = Integer.toBinaryString(z[i]);  //convert messages to binary 
string 
                        if((message.length())!=8)
                        {
                         StringBuffer s8 = new StringBuffer(message);
                         message = s8.insert(0,"0").toString();
                        }
                        a.add(message);                         
          }                      

         Object ar[] = a.toArray();                 
                
                 for(j=0; j<ar.length; ++j)
                  binarymessage = sbuf.append(ar[j]).toString(); //stores the binary 
string
                 
                // System.out.print(" Message in binary is :" + binarymessage); 
                 
                 int size = binarymessage.length();
         
                 if((size%64)!=0)
                  {
           padded = (64 - (size%64));         //padding the bits which is less than 64 
bits                                         
                   for(j=0;j<padded;++j)
                    binarymessage = sbuf.append("0").toString();
          } 
                  /*fo.write(binarymessage);
                  fo.close();*/
                //System.out.print(" Message in binary is :" + binarymessage); 
                fr.close();
      return binarymessage;       
   }

  
  public static ArrayList blocks64bits()
    throws Exception  
  {
    
        ArrayList b = new ArrayList();
        
    binarymesj = en.readbinarymessage();
    
        int  i = 0; j = 64;     
    while(j != (binarymesj.length()))    
    {   
         strch = binarymesj.substring(i,j);
         //System.out.print(" strch is " + strch);//stores the binarymessages into 
         b.add(strch);                           //blocks of 64bits data.
         i = j;
         j = i + 64;
     if(j == (binarymesj.length()))
          {
        strch = binarymesj.substring(i,j);
                //System.out.print(" strch is " + strch);
            b.add(strch);
          }       
    }   
     return b;
  }
  
  public static String[] blocks6bits(String str48bits)
  {
         String []B = new String[8];
         
         j=0; k=6;i=0;
         while (k!=(str48bits.length()))
          {
                 str6 = str48bits.substring(j,k);   //break the Exclusive-Or of 48bits
                 B[i++] = str6;                    //into 6 bits each in B[i].
                 j = k;
                 k = j + 6;
                 if(k==(str48bits.length()))
                  {
                     str6 = str48bits.substring(j,k);
                     B[i++] = str6;
                  }
          }
         return B;
  }
  
  public static String[] blocks4bits(String []B)
  {
    char[] binrow = new char[2];
        String []birows = new String[8];
        int srowcol,bitomit,bitomits;
    String bicol,str4;
    int [][][]sb = new int[8][4][16];
    int []numrow = new int[8];
        int []numcol = new int[8];
        int []numsec = new int[8];
    
        sb = en.sbox();
    
        for(i=0;i<8;++i )
    {              
          char ch6bits[] = B[i].toCharArray();
          bicol = B[i].substring(1,5);              //Takes the 1st through 4th bits
           k=0;j=0;
          while(j!= 2)                             //as 4bits value
           {   
                binrow[j++]= ch6bits[k];
            k= k + 5;
       }
          String birow = new String(binrow);            //Takes the 0 and 5th bits 
                birows[i] = birow;                             //as 2 bits value. 
      try{
              row = Integer.parseInt(birows[i],2);                            
              col = Integer.parseInt(bicol,2);
                 }catch(NumberFormatException e){}; 
          numrow[i] = row;
          numcol[i] = col;
          numsec[i] = i;
          srowcol  = sb[numsec[i]][numrow[i]][numcol[i]];
          str4 = Integer.toBinaryString(srowcol); 
          if((str4.length())!=4)
           {                                       //insert the bits "0" that are
             bitomits = 4 - (str4.length());       //ommited during converting to 
binary
             for (m=0;m<bitomits; ++m)             
              {
               StringBuffer s2 = new StringBuffer(str4);
               str4 = s2.insert(0, "0").toString();  
          }
         B[i] = str4;                     
           }
          B[i] = str4; 
        }
        return B;
  }

  public static int[][][] sbox() 
   {
                int [][][]sb = new int[8][4][16];
            int i,j,k;
                int []a = {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7};
                int []b = {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8};
                int []c = {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0};
                int []d = {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13};
                int []e = {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10};
                int []f = {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5};
                int []g = {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15};
                int []h = {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9};
                int []i1 = {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8};
                int []j1 = {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1};
                int []k1 = {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7};
                int []l = {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12};
                int []p = {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15};
                int []q = {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9};
                int []r = {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4};
                int []s = {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14};
                int []t = {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9};
                int []u = {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6};
                int []v = {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14};
                int []w = {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3};
                int []x = {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11};
                int []y = {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8};
                int []z = {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6};
                int []a1 = {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13};
                int []b1 = {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1};
                int []c1 = {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6};
                int []d1 = {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2};
                int []e1 = {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12};
                int []f1 = {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7};
                int []g1 = {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2};
                int []h1 = {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8};
                int []m1 = {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};
       

           for (i=1; i<=8; ++i)
            {
            for (j=1;j<=4; ++j)
                 {                                //Creates Substitution box,S-BOX
         for (k=1;k<=16; ++k)            //Stores in sb[i][j][k]
              {
                   if((i==1)&&(j==1))                  
                  sb[i-1][j-1][k-1] = a[k-1];
                   
                   else if((i==1)&&(j==2))             //stores according to the number
                      sb[i-1][j-1][k-1] = b[k-1];      // of boxes,rows and columns 
               
                   else if((i==1)&&(j==3))
                      sb[i-1][j-1][k-1] = c[k-1];
                   
                   else if((i==1)&&(j==4))
                      sb[i-1][j-1][k-1] = d[k-1];
           
                   else if((i==2)&&(j==1))
                      sb[i-1][j-1][k-1] = e[k-1];
                   
                   else if((i==2)&&(j==2))
                      sb[i-1][j-1][k-1] = f[k-1];
                   
                   else if((i==2)&&(j==3))
                      sb[i-1][j-1][k-1] = g[k-1];
                   
                   else if((i==2)&&(j==4))
                      sb[i-1][j-1][k-1] = h[k-1];
                   
                   else if((i==3)&&(j==1))
                      sb[i-1][j-1][k-1] = i1[k-1];
                   
                   else if((i==3)&&(j==2))
                      sb[i-1][j-1][k-1] = j1[k-1];
                   
                   else if((i==3)&&(j==3))
                      sb[i-1][j-1][k-1] = k1[k-1];
                   
                   else if((i==3)&&(j==4))
                      sb[i-1][j-1][k-1] = l[k-1];
                   
                   else if((i==4)&&(j==1))
                      sb[i-1][j-1][k-1] = p[k-1];
                   
                   else if((i==4)&&(j==2))
                      sb[i-1][j-1][k-1] = q[k-1];
                   
                   else if((i==4)&&(j==3))
                      sb[i-1][j-1][k-1] = r[k-1];
                   
                   else if((i==4)&&(j==4))
                      sb[i-1][j-1][k-1] = s[k-1];
           
                   else if((i==5)&&(j==1))
                      sb[i-1][j-1][k-1] = t[k-1];
                   
                   else if((i==5)&&(j==2))
                      sb[i-1][j-1][k-1] = u[k-1];
                   
                   else if((i==5)&&(j==3))
                      sb[i-1][j-1][k-1] = v[k-1];
                   
                   else if((i==5)&&(j==4))
                      sb[i-1][j-1][k-1] = w[k-1];

                   else if((i==6)&&(j==1))
                      sb[i-1][j-1][k-1] = x[k-1];
                   
                   else if((i==6)&&(j==2))
                      sb[i-1][j-1][k-1] = y[k-1];
                   
                   else if((i==6)&&(j==3))
                      sb[i-1][j-1][k-1] = z[k-1];
                   
                   else if((i==6)&&(j==4))
                      sb[i-1][j-1][k-1] = a1[k-1];
                   
                   else if((i==7)&&(j==1))
                      sb[i-1][j-1][k-1] = b1[k-1];
                   
                   else if((i==7)&&(j==2))
                      sb[i-1][j-1][k-1] = c1[k-1];
                   
                   else if((i==7)&&(j==3))
                      sb[i-1][j-1][k-1] = d1[k-1];
                   
                   else if((i==7)&&(j==4))
                      sb[i-1][j-1][k-1] = e1[k-1];
                   
                   else if((i==8)&&(j==1))
                      sb[i-1][j-1][k-1] = f1[k-1];
                   
                   else if((i==8)&&(j==2))
                      sb[i-1][j-1][k-1] = g1[k-1];
                   
                   else if((i==8)&&(j==3))
                      sb[i-1][j-1][k-1] = h1[k-1];
                   
                   else if((i==8)&&(j==4))
                      sb[i-1][j-1][k-1] = m1[k-1];
                  }
             }
            }

     return sb;
        } 
}

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
==============C3983F32AD62A675A324C600
Content-Type: text/plain; charset=us-ascii;
 name="Key.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Key.java"

import java.io.*;
import java.lang.*;
import java.util.*;
 
public class Key
{
  static private String binary;
  static private String tmp;
  static private int[] K = new int[48];
  static private char[] h = new char[48];

   public static String[] binarykey()
   {
      int i,j;
      int[] C = 
{57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36};
      int[] D = 
{63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};
      int[] R = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
      int[] PC = {14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2};
      int[] PD = 
{41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};

      String s;

      String []t = new String[16];
         
      
      Key ky = new Key();

      s = ky.readkey();

      char d[] = s.toCharArray();
                                        //Generates 16 subkeys for each round in binary
      for(i=0 ;i<16; i++)
       {                          
                                   
         C = ky.shift(R[i],C);

         D = ky.shift(R[i],D);

         ky.permute(C,K,PC,0);

         ky.permute(D,K,PD,1);

         for(j=0;j<48;++j)
                   h[j] = d[K[j]];
                                
         String m = new String(h);

         t[i] = m;     
       }
           
       
     return t;
   }

  public static String readkey()

  {
      String str;

      ArrayList al = new ArrayList();

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      StringBuffer stb = new StringBuffer();

      int i,j;


    try{
          System.out.print("\nPlease enter your encryption key : ");

          str = br.readLine();
          
          char c[] = str.toCharArray();

          for(i = 0; i<c.length; ++i)
          {                                         //get the key from user and
             //if(Character.isDigit(c[i]))           //convert into binary 
representation
             //{                                    //of original key.
              
              binary = Integer.toBinaryString(c[i]);
               if((binary.length())!=8)
                       {                                          //insert the bits 
"0" that are
                          int bito = 8 - (binary.length());       //ommited during 
converting to binary
                              for (int to=0; to<bito; ++to)           //to maintain 
8bits
                               {
                                  StringBuffer s1r = new StringBuffer(binary);
                                  binary = s1r.insert(0, "0").toString();  
                   }                                
                 al.add(binary);
                                }
                                else
                                al.add(binary);
             //}
          }
         
         Object ia[] = al.toArray(); 
                
         for(j=0;j<ia.length; ++j)

          tmp = stb.append(ia[j]).toString();
                 
                      
       } catch(IOException e){};

     return tmp;
      
  }

  public static void permute(int[] in, int[] out, int[] p, int f)

  {
        int l;

       if (f == 0)
       
          l = 0;                               //permute to get 16 subkeys with 48bits 
long      
       else       
         
          l = 24;
       
        for(int i=0; i<24; i++)
             out[l++] = in[p[i]-1 -f*28];
       
  }


  public static int[] shift(int n, int[] v)

  {
    int k;
    int i;
    int [] temp = new int[28];
    int [] q = new int[28];


    for(k=0; k<n; k++)
    {                                         //left shift the keys either 1 or 2 
shift to 
      for (i=0; i<28;i++)                    //the left 1 or 2 position. 
      {
        if(k==1)
        {
          v[i] = temp[(i+29)%28];

          q[i] = v[i];
        }

        else

        {
          temp[i] = v[(i+29)%28];

          q[i] = temp[i];

        }

      }  
     
    }

     return q;
  }
  
}




==============C3983F32AD62A675A324C600
Content-Type: text/plain; charset=us-ascii;
 name="pro.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="pro.txt"

0123456789abcde7
==============C3983F32AD62A675A324C600==


------------------------------


** FOR YOUR REFERENCE **

The service address, to which questions about the list itself and requests
to be added to or deleted from it should be directed, is:

    Internet: [EMAIL PROTECTED]

You can send mail to the entire list (and sci.crypt) via:

    Internet: [EMAIL PROTECTED]

End of Cryptography-Digest Digest
******************************

Reply via email to