I came up with this, it only works on numbers (a string-to-number conversion could happen outside if necessary).
It converts the number to binary and then maps lowercase letters that are either odd (a, c, e, ...) or even (b, d, f, ...) to each 0 or 1 respectively. So
74 = 1001010 = "baababa"
74 = 1001010 = "pcgtghy"
74 = 01001010 = "etcetera"
This is the code in Javascript
const a = 'a'.charCodeAt(0);
const length = 'z'.charCodeAt(0) - a;
function padLeft (string, len){
while(string.length < len) { string = 0 + string; }
return string;
}
function decode (val) {
return (val.charCodeAt(0) - a) % 2;
}
function encode (odd) {
// generate a random letter in the available range
// but make sure it's odd or even
return String.fromCharCode(a + Math.floor(length * Math.random() / 2) * 2 + parseInt(odd, 10));
}
function generate (number, minLength = 0) {
return padLeft(number.toString(2), minLength).split('').map(encode).join('');
}
function solve (code) {
return parseInt(code.split('').map(decode).join(''), 2);
}
And this is the usage, it also accepts a minimum length parameter
code = generate(74); console.log(code, solve(code));
code = generate(74, 5); console.log(code, solve(code));
code = generate(74, 10); console.log(code, solve(code));
code = generate(74, 20); console.log(code, solve(code));
Also on npm