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

[컴퓨터보안] Testing code about RC4

Cloud Travel 2013. 6. 16. 16:20

public class RC4 {

 

int s[] = new int [256];

int k[] = new int [256];

int keyLength

 

RC4(int[] key){

    keyLength = key.length

 

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

        s[i] = i;

        k[i] = key[i % keyLength];

    }

 

    int j = 0;    

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

        j = ( j + s[i] + k[i] ) % 256;

        int temp = s[i];

        s[i] = s[j];

        s[j] = temp;

    }

}

 

void keyStream(int length){

    int j = 0, i = 0;

 

    for ( int loop = 0 ; loop < length ; loop++ ){

        i = (i+1) % 256;

        j = (j+s[i]) % 256;

 

        int temp = s[i];

        s[i] = s[j];

        s[j] = temp;

 

        /* print key stream

        int t = ( s[i] + s[j] ) % 256;

        System.out.print(s[t]+ " ");

        */

    }

}

 

void printS(){

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

System.out.format("%02x", s[i]);

System.out.print(" ");

if ( (i+1) % 16 == 0 ) System.out.println();

}

}


public static void main(String[] args){

 

int key[] = {0x1A, 0x2B, 0x3c, 0x4d, 0x5e, 0x6f, 0x77};

 

System.out.println("RESET ");

RC4 reset = new RC4(key);

reset.printS();

 

System.out.println("\n\nLooping 100");

RC4 oneHundred = new RC4(key);

oneHundred.keyStream(100);

oneHundred.printS();

 

System.out.println("\n\nLooping 1000");

RC4 oneThousand = new RC4(key);

oneThousand.keyStream(1000);

oneThousand.printS();

}

}