ROT / Shift cipher generator and solver

Just playing around with ciphers again. No really use for this nowadays since it’s easilly broken, but I figured I’ll post it here anyway.

Here you can generate a cipher text based on rotation/shift in the alphabet.

Remember that the chars that will be shifted are ABCDEFGHIJKLMNOPQRSTUVWXYZ.

Genererate ciphertext


Shift steps

(i.e. type ‘13’ to get ROT13)

Solve ciphertext


Shift steps:

Here’s the javascript source:

var ROT = {
    generate: function(plaintext, shift)
        var ciphertext = this.rotate(plaintext, parseInt(shift));
        return ciphertext;
    solve: function(ciphertext, shift)
        var plaintext = this.rotate(ciphertext, -1*parseInt(shift));
        return plaintext;
    rotate: function(str, shift, pos)
        var pos = pos || 0;

            return '';

        var old_char = str.charAt(pos);
        var old_pos = this.chars.indexOf(old_char.toUpperCase());

        if(old_pos < 0)//Some other character
            return old_char.toUpperCase() + this.rotate(str, shift, pos+1);

        var new_pos = (old_pos + shift) % this.chars.length;
        new_pos = new_pos < 0 ? this.chars.length + new_pos : new_pos;

        var new_char = this.chars.charAt(new_pos);

        if(str.length-1 > pos)
            return new_char + this.rotate(str, shift, pos+1);

        return new_char;