• 【算法】Hidden 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 capitalisation 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 capitalisation.

    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):

    LetterPattern
    auuuuu
    buuuul
    cuuulu
    duuull
    euuluu
    fuulul
    guullu
    huulll
    iuluuu
    juluul
    kululu
    lulull
    mulluu
    nullul
    oulllu
    pullll
    qluuuu
    rluuul
    sluulu
    tluull
    ululuu
    vlulul
    wlullu
    xlulll
    ylluuu
    zlluul
    .llllu
    lllll

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    Notes
    • N/A
    Solutions
    const baconify = (msg,mark)=>{
        let answer = [];
        const isAlpha = c => (c>='a'&&c<='z')||(c>='A'&&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'&&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 && 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': 'lulll',
            'y': 'lluuu',
            'z': 'lluul',
            '.': 'llllu',
            ' ': 'lllll'
        };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    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.")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    字节的面试题到底有多难?大厂为何都注重算法?我们该如何应对?
    Git 命令行使用指南
    TS复习-----TS中的函数
    《互联网大厂晋升指南》读书笔记-上
    Qt 非圆角图片裁剪为圆角图片
    HTTPS加密对中小微客户的价值?
    Java项目:ssm流浪猫狗救助管理系统
    torch.Tensor详解
    errno变量和显示错误信息
    江苏显卡服务器适用于哪些场景?
  • 原文地址:https://blog.csdn.net/avenccssddnn/article/details/133895583