// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class RC4 {
    private Cipher rc4;
    private SecretKey rc4key;
    public RC4(String key) {
        try {
            rc4 = Cipher.getInstance("RC4");
            rc4key = new SecretKeySpec(key.getBytes(), "RC4");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private byte[] encrypt(byte[] plaindata) throws Exception {
        rc4.init(Cipher.ENCRYPT_MODE, rc4key);
        return rc4.doFinal(plaindata);
    }
    private byte[] decrypt(byte[] cipherdata) throws Exception {
        rc4.init(Cipher.DECRYPT_MODE, rc4key);
        return rc4.doFinal(cipherdata);
    }
    public static void main(String[] args) throws Exception {
        RC4 cry = new RC4("password123");
        String s1 = "This is a test 123 !!!!";
        System.out.println(s1);
        byte[] b1 = s1.getBytes();
        for(byte b : b1) System.out.print(" " + b);
        System.out.println();
        byte[] b2 = cry.encrypt(b1);
        for(byte b : b2) System.out.print(" " + b);
        System.out.println();
        byte[] b3 = cry.decrypt(b2);
        for(byte b : b3) System.out.print(" " + b);
        System.out.println();
        String s2 = new String(b3);
        System.out.println(s2);
    }
}
