// 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) 
public class Privacy {
    public static int encrypt(int v, int offset) {
        if(v < 0 || v > 9) throw new IllegalArgumentException("Non digit in string");
        return (v + offset) % 10;
    }
    public static int decrypt(int v, int offset) {
        if(v < 0 || v > 9) throw new IllegalArgumentException("Non digit in string");
        return (v - offset + 10) % 10;
    }
    public static String encrypt(String s, int offset, int[] ix) {
        if(s.length() != ix.length) throw new IllegalArgumentException("String and index of different length");
        char[] c = new char[s.length()];
        for(int i = 0; i < s.length(); i++) {
            c[ix[i]] = (char)(encrypt(s.charAt(i) - '0', offset) + '0');
        }
        return new String(c);
    }
    public static String decrypt(String s, int offset, int[] ix) {
        if(s.length() != ix.length) throw new IllegalArgumentException("String and index of different length");
        char[] c = new char[s.length()];
        for(int i = 0; i < s.length(); i++) {
            c[i] = (char)(decrypt(s.charAt(ix[i]) - '0', offset) + '0');
        }
        return new String(c);
    }
    public static void main(String[] args) {
        String s1 = "1357";
        String s2 = encrypt(s1, 7, new int[] { 2, 3, 0, 1 });
        String s3 = decrypt(s2, 7, new int[] { 2, 3, 0, 1 });
        System.out.println(s1 + " -> " + s2 + " -> " + s3);
    }
}
