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

[컴퓨터보안] Testing code about A5/1

Cloud Travel 2013. 6. 16. 16:24

public class A5_1 {

byte registerX[] = {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};

byte registerY[] = {1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1};

byte registerZ[] = {1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0};

 

int counter = 0;

 

byte findMajority(byte num1, byte num2, byte num3){

    byte temp[] = {num1, num2, num3};

 

    byte zero = 0;

    byte one = 0;

 

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

        if ( temp[i] == 0 ) zero++;

        else one++;

    }

 

    if ( zero > one ) return 0;

    else return 1;

}


// overload

byte xor(byte num1, byte num2){

    byte temp[] = {num1, num2};

 

    int one = 0;

 

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

        if ( temp[i] == 1 ) one++;    

    }

 

    if ( one % 2 == 0 ) return 0;

    else return 1;

}

 

byte xor(byte num1, byte num2, byte num3){

    byte temp[] = {num1, num2, num3};

 

    int one = 0;

 

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

        if ( temp[i] == 1 ) one++;

    }

 

    if ( one % 2 == 0 ) return 0;

    else return 1;

}

 

byte xor(byte num1, byte num2, byte num3, byte num4){

    byte temp[] = {num1, num2, num3, num4};

 

    int one = 0;

     

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

        if ( temp[i] == 1 ) one++;

    }

 

    if ( one % 2 == 0 ) return 0;

    else return 1;

}

 

void makeKeyStream(){

    byte maj = findMajority(registerX[8], registerY[10], registerZ[10]);

 

    if ( registerX[8] == maj ){

        byte t = xor(registerX[13],registerX[16],registerX[17],registerX[18]);

        for ( int i = registerX.length-1 ; i > 0 ; i-- ){

            registerX[i] = registerX[i-1];

        }

        registerX[0] = t;

    }

    if ( registerY[10] == maj ){

        byte t = xor(registerY[20], registerY[21]);

        for ( int i = registerY.length-1 ; i > 0 ; i-- ){

            registerY[i] = registerY[i-1];

        }

        registerY[0] = t;

    }

    if ( registerZ[10] == maj){

        byte t = xor(registerZ[7], registerZ[20], registerZ[21], registerZ[22]);

        for ( int i = registerZ.length-1 ; i > 0 ; i-- ){

            registerZ[i] = registerZ[i-1];

        }

        registerZ[0] = t;

    }

 

    System.out.print(xor(registerX[18], registerY[21], registerZ[22]) + " ");

    counter++;

    if ( counter % 16 == 0 ) System.out.println();

}

 

void printRegister(){

    System.out.println("Register X");

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

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

    }

    System.out.println("\nRegister Y");

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

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

    }

    System.out.println("\nRegister Z");

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

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

    }

}

 

public static void main(String[] args){

    A5_1 test = new A5_1();

 

    System.out.println("Make 32bit key stream");

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

        test.makeKeyStream();

    }

    System.out.println("\nStatus of register after make key stream");

    test.printRegister();

}

}