﻿var keys = new Array();
keys[0] = "198ACFA8893843E68E587CECBC30E0F6";
keys[1] = "A901F5E4D45246FF8891B05D77A1F01B";
keys[2] = "1DB8A922FFF340FABD19A77DFC91BCC0";
keys[3] = "53EDCD74DACC4992A15F78C527118E04";
keys[4] = "ABBA07388AC749B3942716C3AF393DC7";
keys[5] = "41FE43DBA1744F0B95290FF5F736111B";
keys[6] = "8AC37C3B09194C0589A689771D4F971B";
keys[7] = "E8CC708D1CE74CE0AAB6A8CE4493743C";
keys[8] = "60A872693E754818A24230AD3EFC8DF1";
keys[9] = "A4516E67936F439499C888D840B96ED4";

var seed = getWhisperSeed();

$(document).ready(function () {
    var iv = getWhisperIV();
    var key = getWhisperKey();

    var cipher = jscrypto.aes;
    var encryptor = new jscrypto.cbcmode.encryptor(cipher, key, iv);
    var ctext = new Array();
    ctext.concat(encryptor.update('hello'));
    ctext.concat(encryptor.update('world')); 
    ctext.concat(encryptor.final()); 
    document.write(ctext);
});

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function getWhisperSeed() {
    var d = new Date();
    return Math.round((Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()) / 1000) / 1000);
}

function getWhisperKey() {
    var idx = seed % 10;
    var bs = guidToByteArray(keys[idx]);
    return bs.concat(bs.reverse());
}

function getWhisperIV() {
    var bs = getBytes(seed);
    return bs.concat(bs.reverse()).concat(bs).concat(bs.reverse());
}

function getBytes(x) {
    var bytes = [];
    var i = 4;
    do {
        bytes[--i] = x & (255);
        x = x >> 8;
    } while (i)
    return bytes;
}

function guidToByteArray(guid) {
    var bytes = [];

    bytes.push(getBytes(parseInt(guid.substr(0, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(4, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(8, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(12, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(16, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(20, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(24, 4), 16)));
    bytes.push(getBytes(parseInt(guid.substr(28, 4), 16)));

    return bytes;
}

