• Coding
  • Cryptology- Caesar Cipher.

Greetings amigos,
I was messing out with cryptology and i decided creating a python GUI and algorithm to encrypt and decrypt plaintext, I used the Caesar Cipher with a key equal to 3. If you change the key the whole thing won't work. And another thing you should have python installed on your PC or Mac (including Tkinter module- which is most probably availble on your pc because it comes with standard python library) though i did not compile it yet. I spent hours working on this thing.
hope you like it:
It is availble as a code on PasteBin and on CodePad.org .
On Codepad.org press the 'raw code' button to download the code.py .
Hope you like it.
I compiled it, you can download it here SlingFile and here MediaFire .
Cool! Now how can you improve your code?
Hint: How can you avoid repeating those if blocks endlessly?
My own code: works with any k:
public class Caesar {
	public static void main(String[] args) {
		int k=Integer.parseInt(args[0]),y;
		String original,caesar;
		char letter;
		caesar="";								// Declaration of variables.
		System.out.print("Please Type The String You Want To Cipher: ");
		original=StdIn.readLine();				// Takes a string from the user and puts it in the variable called original.
		if(k>26)								// These if statement make it possible to input k>26 and k<0. 26 is the number of letter in the alphabet. For example, if k=27, this is same as k=1. These if statements ensure that the program will give correct output for whatever value of k.
			k=k%26;
		if(k<-26)
			k=k%26+26;
		else if(k<0)
				k=26+k;
		for(int i=0;i<original.length();i++){	// walks through every character of the original string.
			letter=original.charAt(i);
			if((letter>='a' && letter<='z'-k)||(letter>='A' && letter<='Z'-k))	// ensure that we will only deal with letters (upper and lower case) and not any character.
				letter=(char)(original.charAt(i)+k);
			else if(letter>'z'-k && letter<='z'){								// the last 2 if statements are the treatment of the last k characters.
				y='z'-k+1;
				letter=(char)('a'+letter-y);
			}
				else if(letter>'Z'-k && letter<='Z'){
					y='Z'-k+1;
					letter=(char)('A'+letter-y);
				}
			caesar=caesar+letter;				// We obtain a new string, which is the ciphered string called caesar.
		}
		System.out.println("Original: " + original);	// prints the original string.
		System.out.println("Caesar  : " + caesar);		// prints the ciphered string.

	}

}
for deciphering, you would just use -k instead of k...
just for fun:
caeser[s_, k_] := StringReplace[s, rules[k]];
rules[k_] := Flatten[rulepairs[#, RotateLeft[#, k]] & /@ characters];
rulepairs[u_, v_] := Rule[#[[1]], #[[2]]] & /@ Partition[Riffle[u, v], 2];
characters := {CharacterRange["A", "Z"], CharacterRange["a", "z"]};
caeser[s, 3]
caeser[s, -3]