Sorry. It's an encryption program that uses one-time pads. It's kind of hard to follow the logic since it's undocumented. I'm porting it from visual basic. Basically, you call cipherText with your alphabet, clear text, key and a 0 or 1 telling whether you're decrypting or encrypting, respectively. It takes one character from the plaintext, one character from the key, looks up their positions in the supplied alphabet, adds together their values, does some math, spits out a numerical value, then looks this value up in the alphabet for an output character. Hope that's clear.
Thanks
function findOffset(alphabet, ch)
{
var newFindOffset = alphabet.indexOf(ch);
if (newFindOffset == -1) {
return -5;
} else {
return newFindOffset;
}
}
function findChr(alphabet, offset)
{
return alphabet.charAt(offset);
}
function cipherText(alphabet, inputtext, key, encrypt)
{
var understand = 0;
var ckeyl = 1;
var ciphertext = "";
for (i=0; i<= inputtext.length; i++) {
if (encrypt == 1) {
var opt = findOffset(alphabet, inputtext.charAt(i));
} else {
var oct = findOffset(alphabet, inputtext.charAt(i));
}
var oky = findOffset(alphabet, key.charAt(ckeyl));
if (opt == -5 || oky == -5 || oct == -5) {
alert("Error: not enough symbols");
//todo: terminate
}
if (encrypt == 1) {
var opo = (opt + oky) % alphabet.length;
} else {
var opo = (oct - oky + alphabet.length) % alphabet.length
}
var cop = findChr(alphabet, opo);
ciphertext = ciphertext + cop;
ckeyl++;
if (ckeyl > key.length && input.length > key.length) {
ckeyl=1;
if (encrypt == 1){
if (understood == 0){
alert("Wrap?");
//todo: fix this up, make it interactive
understood == 1;
}
}
}
}
document.write("execution complete");
return ciphertext;
}
function testCrypt(){
var textencrypt = cipherText('abcdefghijklmnopqrstuvwxyz','testing','thisiswhereyourkeygoes',1);
document.write(textencrypt);
}