# RC4 Algorithmus



## ProggerSchaf (22. Feb 2009)

Huhu Leute ich hab ein Problem, mein RC4 funktioniert nicht so wie ich weill, es kommt was anderes als bei CrypTool raus obwohl laut Wiki mein Code richtig sein müsste:


```
/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Algorithmus();
	}

	short[] sbox = new short[256];
	short[] key = {0xac,0xfa, 0x0c,00,11,22,33,44};
	
	private void initalizeSandbox(){
		int i = 0;
		int j = 0;
		for(i = 0; i < 256; i++){
			sbox[i] = (short) (i);
		}		
		i = 0;
		
		for(i = 0; i < 256; i++){
			j = (j + sbox[i] + key[(i%8)]) % 256;
			short newSi = sbox[j];
			short newSj = sbox[i];
			sbox[i] = newSi;
			sbox[j] = newSj;
		}	
		
		for(i = 0; i < 256; i++){
			j = (j + sbox[i] + key[(i%8)]) % 256;
			short newSi = sbox[j];
			short newSj = sbox[i];
			sbox[i] = newSi;
			sbox[j] = newSj;
		}
	}	

	
	
	
	public static String arrayToHex(byte[] data, boolean setdelimeter) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String bs = Integer.toHexString(data[i] & 0xFF);
            if (bs.length() == 1) {
                sb.append(0);
            }
            sb.append(bs);
            if( setdelimeter){
            sb.append("-");
            }
        }
       return sb.toString();
    } 
	
	public Algorithmus(){
		initalizeSandbox();	
		encryptSequenz("Codierung");
	}

	private void encryptSequenz(String string) {
		int i = 0;
		int j = 0;
		String[] s = string.split("");
		
		for(int z = 1; z < s.length; z++){
			System.out.println((int)s[z].charAt(0));
		}
		
		StringBuffer sb = new StringBuffer();
		for(int laenge = 1;  laenge < s.length; laenge++){
			i = (i+1) % 256;
			j = (j + sbox[i]) % 256;
			short newj = sbox[i];
			short newi = sbox[j];
			sbox[i] = newi;
			sbox[j] = newj;
			short randomnumber = sbox[((sbox[i] + sbox[j]) % 256)];
			short chiffre = (short) (((short)((int) s[laenge].charAt(0)))^ randomnumber);
			int zeichen = ((int) chiffre);
			sb.append(zeichen + "-");
		}
		
		System.out.println(sb.toString());
	}
```


----------



## Murray (22. Feb 2009)

1. Code-Tags verwenden
2. Was kommt raus, was soll rauskommen?


----------



## ProggerSchaf (22. Feb 2009)

Code Tags sind verwendet^^

Java meint: 141-133-189-225-29-182-146-161-102 also das sind halt Dez werte

CrypTool meint: 56-DD-92-F1-1C-19-17-15-98


----------



## ProggerSchaf (22. Feb 2009)

HALT


ERROR FOUND ^^

Zu doof

siehe in der Deklaration des schlüssels
keine Hexdefinition für 00 bis 44

korrektur: 

```
short[] key = {0xac,0xfa, 0x0c,0x00,0x11,0x22,0x33,0x44};
```


----------

