Author Topic: [Java] RC4 implementation  (Read 8542 times)

0 Members and 1 Guest are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
[Java] RC4 implementation
« on: May 16, 2013, 09:49:00 am »
This is an RC4 implementation in Java (also known as ARC4 or ARCFOUR).
This cipher generates pseudo random numbers based on a given key and performs an XOR operation on the message to encode it. It is used i.e. in SSL, WEP, HTTPS and SSH1.

Code: (Java) [Select]
public class RC4 {

    private char[] key;
    private int[] sbox;
    private static final int SBOX_LENGTH = 256;
    private static final int KEY_MIN_LENGTH = 5;

    public static void main(String[] args) {
        try {
            RC4 rc4 = new RC4("testkey");
            char[] result = rc4.encrypt("hello there".toCharArray());
            System.out.println("encrypted string:\n" + new String(result));
            System.out.println("decrypted string:\n"
                    + new String(rc4.decrypt(result)));
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        }
    }

    public RC4(String key) throws InvalidKeyException {
        setKey(key);
    }

    public RC4() {
    }

    public char[] decrypt(final char[] msg) {
        return encrypt(msg);
    }

    public char[] encrypt(final char[] msg) {
        sbox = initSBox(key);
        char[] code = new char[msg.length];
        int i = 0;
        int j = 0;
        for (int n = 0; n < msg.length; n++) {
            i = (i + 1) % SBOX_LENGTH;
            j = (j + sbox[i]) % SBOX_LENGTH;
            swap(i, j, sbox);
            int rand = sbox[(sbox[i] + sbox[j]) % SBOX_LENGTH];
            code[n] = (char) (rand ^ (int) msg[n]);
        }
        return code;
    }

    private int[] initSBox(char[] key) {
        int[] sbox = new int[SBOX_LENGTH];
        int j = 0;

        for (int i = 0; i < SBOX_LENGTH; i++) {
            sbox[i] = i;
        }

        for (int i = 0; i < SBOX_LENGTH; i++) {
            j = (j + sbox[i] + key[i % key.length]) % SBOX_LENGTH;
            swap(i, j, sbox);
        }
        return sbox;
    }

    private void swap(int i, int j, int[] sbox) {
        int temp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = temp;
    }

    public void setKey(String key) throws InvalidKeyException {
        if (!(key.length() >= KEY_MIN_LENGTH && key.length() < SBOX_LENGTH)) {
            throw new InvalidKeyException("Key length has to be between "
                    + KEY_MIN_LENGTH + " and " + (SBOX_LENGTH - 1));
        }

        this.key = key.toCharArray();
    }

}

public class InvalidKeyException extends Exception {

    private static final long serialVersionUID = 1L;

    public InvalidKeyException(String message) {
        super(message);
    }

}