

Task

Implement a Caesar cipher, both encoding and decoding. 

The key is an integer from 1 to 25.

This cipher rotates (either towards left or right) the letters of the alphabet (A to Z).

The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A).

So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC".

This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys.

Caesar cipher is identical to Vigenère cipher with a key of length 1. 

Also, Rot-13 is identical to Caesar cipher with key 13.



Related tasks

 Rot-13
 Substitution Cipher
 Vigenère Cipher/Cryptanalysis


class Caesar {
  int _key;
 
  Caesar(this._key);
 
  int _toCharCode(String s) {
    return s.charCodeAt(0);
  }
 
  String _fromCharCode(int ch) {
    return new String.fromCharCodes([ch]);
  }
 
  String _process(String msg, int offset) {
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<msg.length;i++) {
      int ch=msg.charCodeAt(i);
      if(ch>=_toCharCode('A')&&ch<=_toCharCode('Z')) {
        sb.add(_fromCharCode(_toCharCode("A")+(ch-_toCharCode("A")+offset)%26));
      }
      else if(ch>=_toCharCode('a')&&ch<=_toCharCode('z')) {
        sb.add(_fromCharCode(_toCharCode("a")+(ch-_toCharCode("a")+offset)%26));
      } else {
        sb.add(msg[i]);
      }
    }
    return sb.toString();
  }
 
  String encrypt(String msg) {
    return _process(msg, _key);
  }
 
  String decrypt(String msg) {
    return _process(msg, 26-_key);
   }
}
 
void trip(String msg) {
  Caesar cipher=new Caesar(10);
 
  String enc=cipher.encrypt(msg);
  String dec=cipher.decrypt(enc);
  print("\"$msg\" encrypts to:");
  print("\"$enc\" decrypts to:");
  print("\"$dec\"");
  Expect.equals(msg,dec);
}
 
main() {
  Caesar c2=new Caesar(2);
  print(c2.encrypt("HI"));
  Caesar c20=new Caesar(20);
  print(c20.encrypt("HI"));
 
  // try a few roundtrips
 
  trip("");
  trip("A");
  trip("z");
  trip("Caesar cipher");
  trip(".-:/\"\\!");
  trip("The Quick Brown Fox Jumps Over The Lazy Dog.");
}