A ROT-13 implementation in CSharp

ROT-13 is a simple substitution cipher where the current character is rotated by 13 characters, which finds its symmetrical opposite in the modern English alphabet. This property allows the cipher to be reciprocal and therefore it can be used to obfuscate a clear text string and also transform the obfuscated string back into its original clear text equivalent.

I use the term obfuscation in the previous paragraph to emphasize the fact that the ROT-13 cipher is not secure and any obfuscated strings can be easily reversed simply by knowing that a ROT-13 cipher has been used.

The Implementation

The implementation of the cipher is really simple and there are many approaches that one can use to achieve the same outcome. The one I chose is based on a lookup table that I initially build and then use many times. This is basically the same approach used in the Python module this (enter import this on a python prompt). You can see the implementation by finding the this.py file that comes with your python implementation.

The lookup table that I use is implemented using a generic dictionary. When the Rot13 function is called, I check if the lookup table exists and if it doesn't, I'll build it. I use the seeds 65 and 97, which are the ASCII values for A and a respectively, and from there I move 26 values to the right while applying a modulo 26, which tells me if I need to wrap-around to the beginning.

Once the lookup table is built, I can use it by simply going over the string parameter passed into Rot13, and substituting each character in the string by the corresponding value in the lookup table. This is what it looks like:

private Dictionary<char, char> d = null;

public string Rot13(string s) {
    if (d == null || d.Count == 0) {         // build the lookup table
        d = new Dictionary<char, char>();    //  if needed
        int[] sr = { 65, 97 };               // A=65; a=97; ASCII Codes
        foreach (int c in sr) {
            for (int i = 0; i < 26; i++) {   // 26 alpha chars
                d.Add((char)(i + c), (char)((i + 13) % 26 + c));

    StringBuilder sb = new StringBuilder();
    foreach (var item in s.ToCharArray()) {
        if (d.ContainsKey(item))             // Worry only about alpha characters
            sb.Append(d[item]);              //  leave number, etc. untouched

    return sb.ToString();

Using the Algorithm

To use the ROT-13 cipher algorithm above, you simply have to call the Rot13 function and send it a string parameter which represents the string you want to obfuscate. In the example code below the Main function uses a command line argument passed in:

static void Main(string[] args) {
    if (args.Length == 0) {
        WL("Please provide a string to obfuscate using the ROT 13 cypher.");

    Program p = new Program();

    WL(String.Format("Original String: {0}", args[0]));
    WL(String.Format("Obfuscated String: {0}", p.Rot13(args[0])));
    WL(String.Format("De-Obfuscated String: {0}", p.Rot13(p.Rot13(args[0]))));

    WL("\nPress enter to continue...");

static Func<string> RL = () => Console.ReadLine();
static Action<Object> WL = obj => Console.WriteLine(obj);</pre>

If you call the Rot13.exe program like this:

H:\>Rot13.exe HELLO

Your output will be the following:

Original String: HELLO
Obfuscated String: URYYB
De-Obfuscated String: HELLO
Press enter to continue...


I am teaching myself cryptography and security concepts and I am using this venue to write my notes and solidify my ideas on this topic. I will be filing these under the crypto and security tags.


Comments powered by Disqus