-5

its been a few years since I have done anything with Java. But I am trying to write a program from scratch that calculates S-DES encryption and decryption. I am looking at current code online just for some aide on how to set up my program. But I am not sure what the original author was trying to do...

Here is a piece of the code..again its not mine, & I'm not copying it, I'm just trying to understand what they are doing here?

public class SDESProject {


public static void main( String args[]) throws Exception
{
    Scanner keyboard = new Scanner(System.in);

        System.out.println("Please Enter the 10 Bit Key :");
        int K = Integer.parseInt(keyboard.nextLine(),2);

        SDES A = new SDES( K);
        System.out.println("Enter the 8 Bit Plaintext  : ");
        int m = Integer.parseInt(keyboard.nextLine(),2);

        System.out.print("\nKey K1: ");

        SDES.printData( A.K1, 8);
        System.out.print("\nKey K2: ");
        SDES.printData( A.K2, 8);
        m = A.encrypt( m);
        System.out.print("\nEncrypted Message: ");
        SDES.printData( m, 8);
        m = A.decrypt( m);
        System.out.print("\nDecrypted Message: ");
        SDES.printData( m, 8);

        keyboard.close();
}

}



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;

}
Wifil
  • 41
  • 6
  • see shift operator in java - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Razib Apr 04 '15 at 17:50

1 Answers1

1

<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
Binary Right Shift Operator >>. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 will give 15 which is 1111

CY5
  • 1,551
  • 17
  • 23