My first quick implementation of the ”full page as key” idea

4. lokakuuta 2015 klo 10.46
Sijainti: Keskustelupalstat: Library of Babel

Here’s my first quick implementation of the ”full page as key” idea:

Save page as key:

(function () {
    localStorage.setItem('libraryOfBabelKey', document.getElementById('textblock').textContent);
}());

Decrypt using previously saved key:

(function () {
    var alphabet, c, i, m, text, key;

    alphabet = 'abcdefghijklmnopqrstuvwxyz,. ';
    key = localStorage.getItem('libraryOfBabelKey');
    if (!key) {
        alert('No key has been saved.');
        return;
    }
    text = {
        original: document.getElementById('textblock').textContent,
        decoded: ''
    };

    for (i = 0; i < text.original.length; i += 1) {
        c = text.original.substr(i, 1);
        if (c === '\n') {
            text.decoded += c;
        } else {
            m = alphabet.indexOf(key.substr(i, 1));
            text.decoded += alphabet.substr(((alphabet.indexOf(c) - m) % alphabet.length), 1);
        }
    }
    document.getElementById('textblock').textContent = text.decoded;
}());

Vastaa viestiin sen kontekstissa (Library of Babel)