/*
 * This is an implementation of caesars rot13 cipher
 * 
 */
public class rot13 {
	protected String alphabet;
	protected int shift;

	/*
	 * The constructor initialzes the shift with a certain value (originally 13).
	 */
	public rot13(int shift, String alphabet){
		this.alphabet = alphabet;
		this.shift=shift % alphabet.length();
	}
	public String cipher(String in) {
		/*
		 * Shift all chars to the right shift-times.
		 */
		char carray[] = in.toCharArray();
		for (int i=0; i < in.length(); ++i) {
			int pos = alphabet.indexOf(carray[i]);
			if (pos == -1)
				continue;
			pos = (pos + shift) % alphabet.length();
			carray[i] = alphabet.charAt(pos);
		}
		String out = new String(carray);
		return out;
	}
	
	public String decipher(String in) {
		/*
		 * Instead of rewriting the code above we use it and shift right - again - but
		 * this time alphabet.length() - shift times. 
		 */
		int n = shift;
		shift = alphabet.length() - shift;
		String tmp = cipher(in);
		shift = n;
		return tmp;
	}
}
