Edabit Algorithm Bacon Encryption: Hidden under the AppearanceHidden in Plain Sight

Hidden in Plain Sight

cryptography simulation

Instructions

This challenge makes use of a modified Baconian (Francis Bacon) cipher. The following is an example of a (modified) Baconian ciphertext:

ciphertext = "KNowlEDgE ITsElf Is power."

The peculiar capitalization might, at first glance, suggest that either the lowercase or uppercase letters contain, or code for, the hidden message (upper = “KNEDEITEI”, lower = “owlgslfspower”).

But actually, the Baconian cipher is a steganographic method of hiding information. The hidden message is not in the content of the ciphertext, but rather in the presentation. It doesn’t matter which letters are capitalised, just the order of the capitalization.

To decipher the ciphertext above, remove spaces and punctuation, then cleave the message into chunks of length 5, leaving out the remainder:

ciphertext = "KNowl EDgEI TsElf Ispow"

Each chunk represents a letter. Decipher them according to the following table (“u” means uppercase, “l” means lowercase):

Letter Pattern
a uuuuu
b uuuul
c uuulu
d uuull
e uuluu
f uulul
g uullu
h uulll
i uluuu
j uluul
k ululu
l ulull
m ulluu
n ullul
o ulllu
p ullll
q luuuu
r luuul
s luulu
t luull
u luluu
v lulul
w lullu
x lull
y lluuu
z lluul
. llllu
llllll

deciphered = "help"
Create a function that takes 1 or 2 arguments:

  • 1.A Baconian ciphertext or a plaintext message to be enciphered: msg.
  • 2.A background text in which the message is to be hidden: mask.
    If only one argument is given (ciphertext), return the deciphered message (in lowercase, with spaces and full stops as encoded).

If a second argument is given, encipher the first argument msg into the mask, and return the resulting ciphertext. When enciphering, encipher full stops and spaces along with the words. Disregard the rest. The ciphertext itself should retain all the punctuation and spaces of the original mask.

Examples
baconify("KNowlEDgE ITsElf Is power.") // "help"

baconify("Help me.", "Man prefers to believe what he prefers to be true.") // "MAn prEFeRS To BelIeve what he PreFERS tO Be truE."
// Both the space (between "help" and "me") and the full stop at the end are enciphered.

baconify("Help!!!", "Knowledge itself is power.") // "KNowlEDgE ITsElf Is power."
// Exclamation marks not enciphered, so the resulting ciphertext is identical to the first example.
Notes
  • N/A
Solutions
const baconify = (msg,mark)=>{<!-- -->
    let answer = [];
    const isAlpha = c => (c>='a' & amp; & amp;c<='z')||(c>='A' & amp; & amp;c<= 'Z');
    if(!mark){<!-- -->// decipher
        const markAlphaMapping = {<!-- -->}
        for(let c in alphaMarkMapping){<!-- -->
            markAlphaMapping[alphaMarkMapping[c]] = c;
        }
        mark = msg.split('')
            .filter(isAlpha)
            .map(c=>(c>='a' & amp; & amp;c<='z')?'l':'u');
        for(let i=0;i<mark.length;i + =5){<!-- -->
            answer.push(markAlphaMapping[(mark.slice(i,i + 5)).join('')])
        }
    }else{<!-- --> // encipher
        answer = mark.split('');
        let j = 0;
        for(let i = 0; i < msg.length;i + + ){<!-- -->
            let mask = alphaMarkMapping[msg[i].toLowerCase()];
            if(mask){<!-- -->
                let k = 0;
                while(k < 5 & amp; & amp; j < answer.length){<!-- -->
                    if(!isAlpha(answer[j])){<!-- -->
                        j++
                        continue
                    }
                    if(mask[k]=='u'){<!-- -->
                        answer[j] = answer[j].toUpperCase();
                    }else{<!-- -->
                        answer[j] = answer[j].toLowerCase();
                    }
                    k++;
                    j + + ;
                }
            }
        }
    }
    return answer.join('');
};
const alphaMarkMapping = {<!-- -->
        'a': 'uuuuu',
        'b': 'uuuul',
        'c': 'uuulu',
        'd': 'uuull',
        'e': 'uuluu',
        'f': 'uulul',
        'g': 'uullu',
        'h': 'uulll',
        'i': 'uluuu',
        'j': 'uluul',
        'k': 'ululu',
        'l': 'ulull',
        'm': 'ulluu',
        'n': 'ullul',
        'o': 'ulllu',
        'p': 'ullll',
        'q': 'luuuu',
        'r': 'luuul',
        's': 'luulu',
        't': 'luull',
        'u': 'luluu',
        'v': 'lulul',
        'w': 'lullu',
        'x': 'lull',
        'y': 'lluuu',
        'z': 'lluul',
        '.': 'llllu',
        ' ': 'llllll'
    };

TestCases
let Test = (function(){<!-- -->
    return {<!-- -->
        assertEquals:function(actual,expected){<!-- -->
            if(actual !== expected){<!-- -->
                let errorMsg = `actual is ${<!-- -->actual},${<!-- -->expected} is expected`;
                throw new Error(errorMsg);
            }
        }
    }
})();
Test.assertEquals(baconify("KNowlEDgE ITsElf Is power."), "help")
Test.assertEquals(baconify("Help me.", "Man prefers to believe what he prefers to be true."), "MAn preFeRS To BelIeve what he PreFERS tO Be truE.")
Test.assertEquals(baconify("THE GenERAl ROOT OF suPerstitIOn: nAMElY, ThAT men OBsErve wheN ThiNGs hiT, AnD Not wheN tHEY miss; aNd coMMit To memory THe oNE, and fORGeT and PAss OvER tHE otheR. man preFerS tO BelIEvE what he prefers to be true."), "bran gets the iron throne. wtf ")
Test.assertEquals(baconify("Bran gets the Iron Throne. WTF?!", "The general root of superstition: namely, that men observe when things hit, and not when they miss; and commit to memory the one, and forget and pass over the other. Man prefers to believe what he prefers to be true."), "THE GenERAl ROOT OF suPerstitIOn: nAMElY, ThAT men OBsErve wheN ThiNGs hiT, AnD Not wheN tHEY miss; aNd coMMit To memory THe oNE, and fORGeT and PAss OvER tHE otheR. man preFerS tO BelIEvE what he prefers to be true.")
Test.assertEquals(baconify("knowledge itself is power."), " ")
Test.assertEquals(baconify("Snape kills Dumbledore at Dumbledore's behest.", "Philosophy when superficially studied, excites doubt, when thoroughly explored, it dispels it. The root of all superstition is that men observe when a thing hits, but not when it misses. It is a sad fate for a man to die too well known to everybody else, and still unknown to himself."), "pHIlOSopHy WHEN SUperfICiALly stuDiEd, EXcITES dOubT, wHen tHOrOughly EXPlorEd, IT DisPELS IT. tHe RooT Of ALL SUpeRstiTiON Is THaT Men obsERVE WhEN a thing hITS, but NoT WHen IT MISSeS. iT is A SaD FATE foR a maN tO DIe TOo WElL KnOwn to eVERYbODy ELSE, and STiLL uNKnOwN To himseLf.")