프로그래밍[Univ]/컴퓨터보안

[컴퓨터보안] Testing code about DES

Cloud Travel 2013. 6. 16. 16:18

public class DES {

 

int key[][] = new int[17][48];

 

int leftKey[] = {49, 42, 35, 28, 21, 14, 7, 0, 50, 43, 36, 29, 22,

                    15, 8, 1, 51, 44, 37, 30, 23, 16, 9, 2, 52, 45, 38, 31};

int rightKey[] = {55, 48, 41, 34, 27, 20, 13, 6, 54, 47, 40, 33,

                    26, 19, 12, 5, 53, 46, 39, 32, 25, 18, 11, 4, 24, 17, 10, 3};

 

int leftRefIndex[] = {13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18,

                        11, 3, 25, 7, 15, 6, 26, 19, 12, 1};

int rightRefIndex[] = {12, 23, 2, 8, 18, 26, 1, 11, 22, 16, 4, 19, 15,

                        20, 10, 27, 5, 24, 17, 13, 21, 7, 0, 3};

 

void shiftKey(int r){

int leftTemp1, leftTemp2 = 0;

int rightTemp1, rightTemp2 = 0;

 

if ( r == 1 ) {

leftTemp1 = leftKey[0];

rightTemp1 = rightKey[0];

} else {

leftTemp1 = leftKey[0];

rightTemp1 = rightKey[0];

leftTemp2 = leftKey[1];

rightTemp2 = rightKey[1];

}

 

for ( int i = 0 ; i < 28-r ; i++ ){

leftKey[i] = leftKey[i+r];

rightKey[i] = rightKey[i+r];

}

 

if ( r == 1 ) {

leftKey[leftKey.length-1] = leftTemp1;

rightKey[rightKey.length-1] = rightTemp1;

}else{

leftKey[leftKey.length-2] = leftTemp1;

rightKey[rightKey.length-2] = rightTemp1;

leftKey[leftKey.length-1] = leftTemp2;

rightKey[rightKey.length-1] = rightTemp2;

}

}

 

void saveKey(int index){

for ( int i = 0 ; i < leftRefIndex.length ; i++ ){

    key[index][i] = leftKey[leftRefIndex[i]];

}

for ( int i = 0 ; i < rightRefIndex.length ; i++ ){

    key[index][i+leftRefIndex.length] = rightKey[rightRefIndex[i]];

}

}

 

void printKey(){

for ( int i = 0 ; i < 16 ; i++ ){

System.out.println("Key " + (i + 1) );

for ( int j = 0 ; j < 48 ; j++ ){

    System.out.print(" " + key[i][j]);

}

System.out.println("");

}

}

 

public static void main(String[] args){

int i, r;

DES d = new DES();

 

for ( i = 1 ; i <= 16 ; i++ ){

 

if ( i == 1 || i == 2 || i == 9 || i == 16 ) r = 1;

else r = 2;

 

d.shiftKey(r);

d.saveKey(i-1);

}

d.printKey();

      }

}