// 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 java.util.ArrayList;
import java.util.List;

public class IdObfuscate {
    private class LCG {
        private long a;
        private long c;
        private long m;
        private LCG(long a, long c, long m) {
            this.a = a;
            this.c = c;
            this.m = m;
        }
        public LCG(int a, int c) {
            this(4 * a + 1, 2 * c + 1, 1L << 31);
        }
        public int next(int v) {
            long v2 = 0xFFFFFFFFL & v;
            return (int)((a * v2 + c) % m);
        }
        public int prev(int v) {
            long v2 = 0xFFFFFFFFL & v;
            long res;
            int k = 0;
            do {
                res = (v2 - c + k * m) / a;
                k++;
            } while(res < 0 || next((int)res) != v2);
            return (int)res;
        }
    }
    private class Encoder {
        private String cs;
        public Encoder(String cs) {
            this.cs = cs;
        }
        public String encode(int v) {
            long v2 = 0xFFFFFFFFL & v;
            String res = "";
            for(int i = 0; i < 6; i++) {
                res = cs.charAt((int) (v2 % cs.length())) + res;
                v2 /= 36;
            }
            return res;
        }
        public int decode(String s) {
            long res = 0;
            for(char c : s.toCharArray()) {
                res = res * 36 + cs.indexOf(c);
            }
            return (int)res;
        }
    }
    private LCG lcg;
    private Encoder enc;
    private int key;
    public IdObfuscate(int a, int c, String cs, int key) {
        lcg = new LCG(a, c);
        enc = new Encoder(cs);
        this.key = key;
    }
    public String encode(int id) {
        return enc.encode(key ^ lcg.next(lcg.next(lcg.next(lcg.next(id)))));
    }
    public int decode(String id) {
        return lcg.prev(lcg.prev(lcg.prev(lcg.prev(key ^ enc.decode(id)))));
    }
    public List<String> encode(List<Integer> ids) {
        List<String> res = new ArrayList<String>();
        for(int id : ids) {
            res.add(encode(id));
        }
        return res;
    }
    public List<Integer> decode(List<String> ids) {
        List<Integer> res = new ArrayList<Integer>();
        for(String id : ids) {
            res.add(decode(id));
        }
        return res;
    }
    public static void main(String[] args) {
        //IdObfuscate obf = new IdObfuscate(2048, 55555555, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 12345);
        //IdObfuscate obf = new IdObfuscate(1234, 55555555, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 12345);
        IdObfuscate obf = new IdObfuscate(77, 55555555, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 12345);
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(4);
        ids.add(5);
        ids.add(6);
        ids.add(8);
        ids.add(9);
        ids.add(10);
        ids.add(12);
        ids.add(13);
        for(int id : ids) {
            System.out.println(id);
        }
        List<String> obfids = obf.encode(ids);
        for(String obfid : obfids) {
            System.out.println(obfid);
        }
        List<Integer> xids = obf.decode(obfids);
        for(int id : xids) {
            System.out.println(id);
        }
        int n = 1000000;
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < n; i++) {
            if(i != obf.decode(obf.encode(i))) throw new RuntimeException("Ooops");
            if(i % 10000 == 0) System.out.println(i);
        }
        long t2 = System.currentTimeMillis();
        System.out.println(n * 1000L / (t2 - t1));
    }
}
