Saturday, October 25, 2014

ROT13 Cryptography

ROT13 is one of the most simple cryptography systems, is a good way to give an introduction  of how the cryptography works!
A cryptography system is based on:

  • The plain text: P
  • An invertible function: F
  • The inverse of F: I
  • The encoded text: E = F(P)
  • The plain text again: P = I(F(P))

Here is a javascript implementation of ROT13

function F(P) {
    var x, E = "";

    for (var i = 0; i < P.length; i++) {
        x = P.charCodeAt(i);
        if (x >= 65 && x <= 90) //'A' = 65 | 'Z' = 90
            x = (x - 65 + 13) % 26 + 65;
        if (x >= 97 && x <= 122) //'a' = 97 | 'z' = 122
            x = (x - 97 + 13) % 26 + 97;
        E = E + String.fromCharCode(x);
    }

    return E;
}




As you can observe:
FI (ROT13 is his own inverse).
This can be explained:
x + 13 ≡ x - 13 (mod 26) for any given x


No comments:

Post a Comment