JSFiddle - React, Tailwind, and code Playground
HTML
<table id="possibleStrings">
</table>
JavaScript
function OneTimePad(encdec, text, key)
{
var pad, i, out, c, uc;
pad = "";
key = key.toUpperCase();
for (i = 0; i < key.length; i ++) {
c = key.charAt(i);
if (c >= 'A' && c <= 'Z') {
pad += c;
}
}
out = "";
for (i = 0; i < text.length; i ++) {
c = text.charAt(i);
uc = ' ';
if (c >= 'A' && c <= 'Z') {
uc = 'A';
}
if (c >= 'a' && c <= 'z') {
uc = 'a';
}
if (uc != ' ') {
if (pad.length == 0) {
pad = "AAAAAAAA";
}
c = c.charCodeAt(0) - uc.charCodeAt(0) +
encdec * (pad.charCodeAt(0) - 'A'.charCodeAt(0));
c = (c + 26) % 26;
c = String.fromCharCode(uc.charCodeAt(0) + c);
pad = pad.slice(1, pad.length);
}
out += c;
}
return out;
}
var msg = "Caki64 aOI0eOY6Ww9yW3SogMAcWy1egE5wYOOqgUAweaYqIMKoCSQwWwA5WyuqYsAuAqA5CsAsQKCAQKQOGs==";
var count = 0;
function guessKey() {
guessNextChar("", 0);
}
function guessNextChar(key, len) {
if (key.length == msg.length) {
alert(key);
return;
}
var c = msg.charCodeAt(len);
var isUpper = c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0);
var isLower = c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0);
var isLetter = isUpper || isLower;
if (isLetter) {
for (var j = 'A'.charCodeAt(0); j <= 'Z'.charCodeAt(0); ++j) {
var subs = msg.substring(0, len + 1);
var thisChar = String.fromCharCode(j);
if (isLower) thisChar = thisChar.toLowerCase();
if (OneTimePad(-1, subs, key + thisChar) == key + thisChar) {
guessNextChar(key + thisChar, len + 1);
}
}
} else {
guessNextChar(key + String.fromCharCode(c), len + 1);
}
}
guessKey();