Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: SDES algorithm

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default SDES algorithm

    Hi, i'm new to java programming ,i had obtained a source code from the web which is about the Simple DES encryption.The problem is i cannot run the application properly even it was successfully compiled.The application does not prompt out any input text option instead of few lines of statement.Please help me to run the codes.

    class SDES
     
    {
     
      public int K1, K2;
     
     
     
      public static final int P10[] = { 3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
     
      public static final int P10max = 10;
     
     
     
      public static final int P8[] = { 6, 3, 7, 4, 8, 5, 10, 9};
     
      public static final int P8max = 10;
     
     
     
      public static final int P4[] = { 2, 4, 3, 1};
     
      public static final int P4max = 4;
     
     
     
      public static final int IP[] = { 2, 6, 3, 1, 4, 8, 5, 7};
     
      public static final int IPmax = 8;
     
     
     
      public static final int IPI[] = { 4, 1, 3, 5, 7, 2, 8, 6};
     
      public static final int IPImax = 8;
     
     
     
      public static final int EP[] = { 4, 1, 2, 3, 2, 3, 4, 1};
     
      public static final int EPmax = 4;
     
     
     
      public static final int S0[][] = {
     
        { 1, 0, 3, 2},
     
        { 3, 2, 1, 0},
     
        { 0, 2, 1, 3},
     
        { 3, 1, 3, 2}
     
      };
     
     
     
      public static final int S1[][] = {
     
        { 0, 1, 2, 3},
     
        { 2, 0, 1, 3},
     
        { 3, 0, 1, 2},
     
        { 2, 1, 0, 3}
     
      };
     
     
     
      public static int permute( int x, int p[], int pmax)
     
      {
     
        int y = 0;
     
     
     
        for( int i = 0; i < p.length; ++i) {
     
          y <<= 1;
     
          y |= (x >> (pmax - p[i])) & 1;
     
        }
     
     
     
        return y;
     
      }
     
     
     
      public static int F( int R, int K)
     
      {
     
        int t = permute( R, EP, EPmax) ^ K;
     
        int t0 = (t >> 4) & 0xF;
     
        int t1 = t & 0xF;
     
     
     
        t0 = S0[ ((t0 & 0x8) >> 2) | (t0 & 1) ][ (t0 >> 1) & 0x3 ];
     
        t1 = S1[ ((t1 & 0x8) >> 2) | (t1 & 1) ][ (t1 >> 1) & 0x3 ];
     
     
     
        t = permute( (t0 << 2) | t1, P4, P4max);
     
     
     
        return t;
     
      }
     
     
     
      public static int fK( int m, int K)
     
      {
     
        int L = (m >> 4) & 0xF;
     
        int R = m & 0xF;
     
     
     
        return ((L ^ F(R,K)) << 4) | R;
     
      }
     
     
     
      public static int SW( int x)
     
      {
     
        return ((x & 0xF) << 4) | ((x >> 4) & 0xF);
     
      }
     
     
     
      public byte encrypt( int m)
     
      {
     
        m = permute( m, IP, IPmax);
     
        m = fK( m, K1);
     
        m = SW( m);
     
        m = fK( m, K2);
     
        m = permute( m, IPI, IPImax);
     
     
     
        return (byte) m;
     
      }
     
     
     
      public byte decrypt( int m)
     
      {
     
        m = permute( m, IP, IPmax);
     
        m = fK( m, K2);
     
        m = SW( m);
     
        m = fK( m, K1);
     
        m = permute( m, IPI, IPImax);
     
     
     
        return (byte) m;
     
      }
     
     
     
      public static void printb( int x, int n)
     
      {
     
        int mask = 1 << (n-1);
     
     
     
        while( mask > 0) {
     
          System.out.print( ((x & mask) == 0) ? '0' : '1');
     
          mask >>= 1;
     
        }
     
      }
     
     
     
      public SDES( int K)
     
      {
     
        K = permute( K, P10, P10max);
     
     
     
        int t1 = (K >> 5) & 0x1F;
     
        int t2 = K & 0x1F;
     
     
     
        t1 = ((t1 & 0xF) << 1) | ((t1 & 0x10) >> 4);
     
        t2 = ((t2 & 0xF) << 1) | ((t2 & 0x10) >> 4);
     
     
     
        K1 = permute( (t1 << 5) | t2, P8, P8max);
     
     
     
        t1 = ((t1 & 0x7) << 2) | ((t1 & 0x18) >> 3);
     
        t2 = ((t2 & 0x7) << 2) | ((t2 & 0x18) >> 3);
     
     
     
        K2 = permute( (t1 << 5) | t2, P8, P8max);
     
      }
     
     
     
    }
     
     
     
    public class SKT_SDES
     
    {
     
      public static void main( String args[]) throws Exception
     
      {
     
     
        if( args.length != 2)
     
        {
     
          System.err.println( "Usage: SKT_SDES key(10 bit) plaintext(8 bit)");
     
          System.exit(1);
     
        }
     
     
     
        int K = Integer.parseInt( args[0], 2);
     
        SDES A = new SDES( K);
     
        int m = Integer.parseInt( args[1], 2);
     
     
     
        System.out.print("Key K1: ");
     
        SDES.printb( A.K1, 8);
     
        System.out.print("\nKey K2: ");
     
        SDES.printb( A.K2, 8);
     
        m = A.encrypt( m);
     
        System.out.print("\nEncrypted Message: ");
     
        SDES.printb( m, 8);
     
        m = A.decrypt( m);
     
        System.out.print("\nDecrypted Message: ");
     
        SDES.printb( m, 8);
     
      }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: SDES algorithm

    Can you execute the program in a command prompt, copy the output on the screen and paste it here?
    Add your comments to what you post showing what is wrong with the output and what you want the output to be.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SDES algorithm

    The result from the command prompt is :

    Usage: SKT_DES key(10 bit) plaintext(8bit)
    Attached Images Attached Images

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: SDES algorithm

    How did you execute the program? Your image does not show the command that was entered.
    Open a command prompt window, enter the java command with your classname and then copy and paste the full contents here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Look in the source code you posted for the error message you see on the screen. Read the comments in the code near where the error message comes from for the reasons you get the error message.

Similar Threads

  1. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  2. Q:BackTracking Algorithm
    By Cross`17 in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 17th, 2010, 11:33 PM
  3. Genetic algorithm
    By rpsaranya in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 5th, 2010, 11:00 PM
  4. I have algorithm problem
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2009, 08:11 PM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM