Beale’s Cipher

December 2, 2016

(define declaration "\
when in the course of human events it becomes necessary for one people \
to dissolve the political bands which have connected them with another \
and to assume among the powers of the earth the separate and equal \
station to which the laws of nature and of natures god entitle them a \
decent respect to the opinions of mankind requires that they should \
declare the causes which impel them to the separation we hold these \
truths to be self evident that all men are created equal that they are \
endowed by their creator with certain unalienable rights that among \
these are life liberty and the pursuit of happiness that to secure \
these rights governments are instituted among men deriving their just \
powers from the consent of the governed that whenever any form of \
government becomes destructive of these ends it is the right of the \
people to alter or to abolish it and to institute new government laying \
its foundation on such principles and organizing its powers in such \
form as to them shall seem most likely to effect their safety and \
happiness prudence indeed will dictate that governments long \
established should not be changed for light and transient causes and \
accordingly all experience hath shown that mankind are more disposed to \
suffer while evils are sufferable than to right themselves by \
abolishing the forms to which they are accustomed but when a long train \
of abuses and usurpations pursuing invariably the same object evinces a \
design to reduce them under absolute despotism it is their right it is \
their duty to throw off such government and to provide new guards for \
their future security such has been the patient sufferance of these \
colonies and such is now the necessity which constrains them to alter \
their former systems of government the history of the present king of \
great britain is a history of repeated injuries and usurpations all \
having in direct object the establishment of an absolute tyranny over \
these states to prove this let facts be submitted to a candid world he \
has refused his assent to laws the most wholesome and necessary for the \
public good he has forbidden his governors to pass laws of immediate \
and pressing importance unless suspended in their operation till his \
assent should be obtained and when so suspended he has utterly \
neglected to attend to them he has refused to pass other laws for the \
accommodation of large districts of people unless those people would \
relinquish the right of representation in the legislature a right \
inestimable to them and formidable to tyrants only he has called \
together legislative bodies at places unusual uncomfortable and distant \
from the depository of their public records for the sole purpose of \
fatiguing them into compliance with his measures he has dissolved \
representative houses repeatedly for opposing with manly firmness his \
invasions on the rights of the people he has refused for a long time \
after such dissolutions to cause others to be elected whereby the \
legislative powers incapable of annihilation have returned to the \
people at large for their exercise the state remaining in the meantime \
exposed to all the dangers of invasion from without and convulsions \
within he has endeavored to prevent the population of these states for \
that purpose obstructing the laws for naturalization of foreigners \
refusing to pass others to encourage their migration hither and raising \
the conditions of new appropriations of lands he has obstructed the \
administration of justice by refusing his assent to laws for \
establishing judiciary powers he has made judges dependent on his will \
alone for the tenure of their offices and the amount and payment of \
their salaries he has erected a multitude of new offices and sent \
hither swarms of officers to harass our people and eat out their \
substance he has kept among us in times of peace standing armies \
without the consent of our legislatures he has affected to render the \
military independent of and superior to the civil power he has combined \
with others to subject us to a jurisdiction foreign to our constitution \
and unacknowledged by our laws giving his assent to their acts of \
pretended legislation for quartering large bodies of armed troops among \
us for protecting them by a mock trial from punishment for any murders \
which they should commit on the inhabitants of these states for cutting \
off our trade with all parts of the world for imposing taxes on us \
without our consent for depriving us in many cases of the benefits of \
trial by jury for transporting us beyond seas to be tried for pretended \
offenses for abolishing the free system of english laws in a \
neighboring province establishing therein an arbitrary government and \
enlarging its boundaries so as to render it at once an example and fit \
instrument for introducing the same absolute rule into these colonies \
for taking away our charters abolishing our most valuable laws and \
altering fundamentally the forms of our governments for suspending our \
own legislature and declaring themselves invested with power to \
legislate for us in all cases whatsoever he has abdicated government \
here by declaring us out of his protection and waging war against us he \
has plundered our seas ravaged our coasts burnt our towns and destroyed \
the lives of our people he is at this time transporting large armies of \
foreign mercenaries to complete the works of death desolation and \
tyranny already begun with circumstances of cruelty and perfidy \
scarcely paralleled in the most barbarous ages and totally unworthy the \
head of a civilized nation he has constrained our fellow citizens taken \
captive on the high seas to bear arms against their country to become \
the executioners of their friends and brethren or to fall themselves by \
their hands he has excited domestic insurrections amongst us and has \
endeavored to bring on the inhabitants of our frontiers the merciless \
indian savages whose known rule of warfare is an undistinguished \
destruction of all ages sexes and conditions in every stage of these \
oppressions we have petitioned for redress in the most humble terms our \
repeated petitions have been answered only by repeated injury a prince \
whole character is thus marked by every act which may define a tyrant \
is unfit to be the ruler of a free people nor have we been wanting in \
attention to our british brethren we have warned them from time to time \
of attempts by their legislature to extend an unwarrantable \
jurisdiction over us we have reminded them of the circumstances of our \
emigration and settlement here we have appealed to their native justice \
and magnanimity and we have conjured them by the ties of our common \
kindred to disavow these usurpations which would inevitably interrupt \
our connections and correspondence they too have been deaf to the voice \
of justice and of consanguinity we must therefore acquiesce in the \
necessity which denounces our separation and hold them as we hold the \
rest of mankind enemies in war in peace friends we therefore the \
representatives of the united states of america in general congress \
assembled appealing to the supreme judge of the world for the rectitude \
of our intentions do in the name and by authority of the good people of \
these colonies solemnly publish and declare that these united colonies \
are and of right ought to be free and independent states that they are \
absolved from all allegiance to the british crown and that all \
political connection between them and the state of great britain is and \
ought to be totally dissolved and that as free and independent states \
they have full power to levy war conclude peace contract alliances \
establish commerce and to do all other acts and things which \
independent states may of right do and for the support of this \
declaration with a firm reliance on the protection of divine providence \
we mutually pledge to each other our lives our fortunes and our sacred \
honor")
(define text2 '(
115 73 24 807 37 52 49 17 31 62 647 22 7 15 140 47 29 107 79 84 56 239
10 26 811 5 196 308 85 52 160 136 59 211 36 9 46 316 554 122 106 95 53
58 2 42 7 35 122 53 31 82 77 250 196 56 96 118 71 140 287 28 353 37
1005 65 147 807 24 3 8 12 47 43 59 807 45 316 101 41 78 154 1005 122
138 191 16 77 49 102 57 72 34 73 85 35 371 59 196 81 92 191 106 273 60
394 620 270 220 106 388 287 63 3 6 191 122 43 234 400 106 290 314 47 48
81 96 26 115 92 158 191 110 77 85 197 46 10 113 140 353 48 120 106 2
607 61 420 811 29 125 14 20 37 105 28 248 16 159 7 35 19 301 125 110
486 287 98 117 511 62 51 220 37 113 140 807 138 540 8 44 287 388 117 18
79 344 34 20 59 511 548 107 603 220 7 66 154 41 20 50 6 575 122 154 248
110 61 52 33 30 5 38 8 14 84 57 540 217 115 71 29 84 63 43 131 29 138
47 73 239 540 52 53 79 118 51 44 63 196 12 239 112 3 49 79 353 105 56
371 557 211 505 125 360 133 143 101 15 284 540 252 14 205 140 344 26
811 138 115 48 73 34 205 316 607 63 220 7 52 150 44 52 16 40 37 158 807
37 121 12 95 10 15 35 12 131 62 115 102 807 49 53 135 138 30 31 62 67
41 85 63 10 106 807 138 8 113 20 32 33 37 353 287 140 47 85 50 37 49 47
64 6 7 71 33 4 43 47 63 1 27 600 208 230 15 191 246 85 94 511 2 270 20
39 7 33 44 22 40 7 10 3 811 106 44 486 230 353 211 200 31 10 38 140 297
61 603 320 302 666 287 2 44 33 32 511 548 10 6 250 557 246 53 37 52 83
47 320 38 33 807 7 44 30 31 250 10 15 35 106 160 113 31 102 406 230 540
320 29 66 33 101 807 138 301 316 353 320 220 37 52 28 540 320 33 8 48
107 50 811 7 2 113 73 16 125 11 110 67 102 807 33 59 81 158 38 43 581
138 19 85 400 38 43 77 14 27 8 47 138 63 140 44 35 22 177 106 250 314
217 2 10 7 1005 4 20 25 44 48 7 26 46 110 230 807 191 34 112 147 44 110
121 125 96 41 51 50 140 56 47 152 540 63 807 28 42 250 138 582 98 643
32 107 140 112 26 85 138 540 53 20 125 371 38 36 10 52 118 136 102 420
150 112 71 14 20 7 24 18 12 807 37 67 110 62 33 21 95 220 511 102 811
30 83 84 305 620 15 2 10 8 220 106 353 105 106 60 275 72 8 50 205 185
112 125 540 65 106 807 138 96 110 16 73 33 807 150 409 400 50 154 285
96 106 316 270 205 101 811 400 8 44 37 52 40 241 34 205 38 16 46 47 85
24 44 15 64 73 138 807 85 78 110 33 420 505 53 37 38 22 31 10 110 106
101 140 15 38 3 5 44 7 98 287 135 150 96 33 84 125 807 191 96 511 118
40 370 643 466 106 41 107 603 220 275 30 150 105 49 53 287 250 208 134
7 53 12 47 85 63 138 110 21 112 140 485 486 505 14 73 84 575 1005 150
200 16 42 5 4 25 42 8 16 811 125 160 32 205 603 807 81 96 405 41 600
136 14 20 28 26 353 302 246 8 131 160 140 84 440 42 16 811 40 67 101
102 194 138 205 51 63 241 540 122 8 10 63 140 47 48 140 288))
Advertisements

Pages: 1 2 3

4 Responses to “Beale’s Cipher”

  1. KCR said

    This was my 2nd ever programming course assignment, and the one I enjoyed programming the most. Thanks for posting this.

  2. Paul said

    In Python. The Beale cipher is apparently so complicated to use, that Beale (probably) messed up the encoding. It is, of course, a daunting task to set up the encoding with a document of 1322 words without a computer. It is also not really known which version of the Declaration of Independence he used.

    def read_decl(list_of_words):
        """create encode and decode dictionaries
        the input is a sequence of lowercase words
        """
        E, D = defaultdict(list), {}
        for n, word in enumerate(list_of_words, 1):
            E[word[0]].append(n)
            D[n] = word[0]
        return E, D
    
    def decode(code, D):
        'input is list of ints - output is string'
        return "".join(D.get(n, "?") for n in code)
    
    def encode(txt, E):
        'input is string - output is list of ints'
        txt = txt.lower()
        return [choice(E.get(c, [0])) for c in txt]
    
    E, D = read_decl(open(DECLARATION).read().lower().split())
    print(decode((int(n) for n in open(LETTER).read().split(", ")), D))
    
  3. matthew said

    Sounds like a good excuse to play around with Unicode and ES6 a little more. ES6 has a number of features that make proper Unicode handling rather easier than in previous versions of Javascript. Notably, strings now support the new iterator protocol that allows us to easily convert strings to arrays of single Unicode characters rather than having to deal with surrogate pairs. For testing this, we will use texts in the Gothic script which uses the astral Unicode codepoints U+10330 to U+1034A, and seems to be reasonably well supported by fonts and browsers. Extant Gothic scripts are mainly religious, but there is one poem in Gothic, “Bagme Bloma”, written by J. R. R. Tolkein. Here we encrypt that poem, using as key text the Gothic version of the Lord’s Prayer. I couldn’t find the texts already in the Gothic script, so we start by converting from latin transliterations.

    "use strict"
    
    // The Lord's Prayer in Gothic, transliterated.
    const text1 = [
        "atta unsar þu in himinam",
        "weihnai namo þein",
        "qimai þiudinassus þeins",
        "wairþai wilja þeins",
        "swe in himina jah ana airþai",
        "hlaif unsarana þana sinteinan gif uns himma daga",
        "jah aflet uns þatei skulans sijaima",
        "swaswe jah weis afletam þaim skulam unsaraim",
        "jah ni briggais uns in fraistubnjai",
        "ak lausei uns af þamma ubilin",
        "unte þeina ist þiudangardi jah mahts",
        "jah wulþus in aiwins"
    ];
    
    // Tolkein's Bagme Bloma poem
    const text2 = [
        "brunaim bairiþ bairka bogum",
        "laubans liubans liudandei",
        "gilwagroni glitmunjandei",
        "bagme bloma blauandei",
        "fagrafahsa liþulinþi",
        "fraujinondei fairguni",
    
        "wopjand windos wagjand lindos",
        "lutiþ limam laikandei",
        "slaihta raihta hweitarinda",
        "razda rodeiþ reirandei",
        "bandwa bairhta runa goda",
        "þiuda meina þiuþjandei",
    
        "andanahti milhmam neipiþ",
        "liuhteiþ liuhmam lauhmuni",
        "laubos liubai fliugand lausai",
        "tulgus triggwa standandei",
        "bairka baza beidiþ blaika",
        "fraujinondei fairguni"
    ];
    
    // Gothic alphabet and the standard latin transliteration
    const gothic = "𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊";
    const latin = "abgdeqzhþiklmnjup*rstwfxƕo^";
    const gchars = [...gothic] // iterator respects codepoints
    const lchars = [...latin]
    const charmap = new Map(lchars.map((c,i)=>[c,gchars[i]])) // zip
    const convert = s => [...s].map(c=>charmap.get(c)||c).join("")
    const keytext = text1.map(convert)
    
    // Now construct the encoding tables
    const encoder = new Map()
    const decoder = new Map([[0,"."]]) // Unknown character
    
    const allwords = [].concat(...keytext.map(s=>s.split(" ")))
    allwords.forEach((w,i) => {
        const index = i+1
        const c = String.fromCodePoint(w.codePointAt(0))
        if (!encoder.has(c)) encoder.set(c,[])
        encoder.get(c).push(index)
        decoder.set(index,c)
    })
    
    const encodeone = c => {
        const a = encoder.get(c)
        if (a == undefined) return 0
        else return a[Math.floor(Math.random() * a.length)]
    }
    const encode = s => [...s].map(encodeone,s)
    const decode = s => s.map(n => decoder.get(n)).join("")
    
    const plaintext = text2.map(convert)
    const ciphertext = plaintext.map(encode)
    const plaintext2 = ciphertext.map(decode)
    
    keytext.forEach(s=>console.log(s))
    console.log()
    plaintext.forEach(s=>console.log(s))
    console.log()
    plaintext2.forEach(s=>console.log(s))
    

    Here’s the output with the key text, the plain text and the decrypted cipher text, we can see one of the disadvantages of Beale’s scheme – some of the letters do not occur as first letter in the key text so cannot be enciphered:

    𐌰𐍄𐍄𐌰 𐌿𐌽𐍃𐌰𐍂 𐌸𐌿 𐌹𐌽 𐌷𐌹𐌼𐌹𐌽𐌰𐌼
    𐍅𐌴𐌹𐌷𐌽𐌰𐌹 𐌽𐌰𐌼𐍉 𐌸𐌴𐌹𐌽
    𐌵𐌹𐌼𐌰𐌹 𐌸𐌹𐌿𐌳𐌹𐌽𐌰𐍃𐍃𐌿𐍃 𐌸𐌴𐌹𐌽𐍃
    𐍅𐌰𐌹𐍂𐌸𐌰𐌹 𐍅𐌹𐌻𐌾𐌰 𐌸𐌴𐌹𐌽𐍃
    𐍃𐍅𐌴 𐌹𐌽 𐌷𐌹𐌼𐌹𐌽𐌰 𐌾𐌰𐌷 𐌰𐌽𐌰 𐌰𐌹𐍂𐌸𐌰𐌹
    𐌷𐌻𐌰𐌹𐍆 𐌿𐌽𐍃𐌰𐍂𐌰𐌽𐌰 𐌸𐌰𐌽𐌰 𐍃𐌹𐌽𐍄𐌴𐌹𐌽𐌰𐌽 𐌲𐌹𐍆 𐌿𐌽𐍃 𐌷𐌹𐌼𐌼𐌰 𐌳𐌰𐌲𐌰
    𐌾𐌰𐌷 𐌰𐍆𐌻𐌴𐍄 𐌿𐌽𐍃 𐌸𐌰𐍄𐌴𐌹 𐍃𐌺𐌿𐌻𐌰𐌽𐍃 𐍃𐌹𐌾𐌰𐌹𐌼𐌰
    𐍃𐍅𐌰𐍃𐍅𐌴 𐌾𐌰𐌷 𐍅𐌴𐌹𐍃 𐌰𐍆𐌻𐌴𐍄𐌰𐌼 𐌸𐌰𐌹𐌼 𐍃𐌺𐌿𐌻𐌰𐌼 𐌿𐌽𐍃𐌰𐍂𐌰𐌹𐌼
    𐌾𐌰𐌷 𐌽𐌹 𐌱𐍂𐌹𐌲𐌲𐌰𐌹𐍃 𐌿𐌽𐍃 𐌹𐌽 𐍆𐍂𐌰𐌹𐍃𐍄𐌿𐌱𐌽𐌾𐌰𐌹
    𐌰𐌺 𐌻𐌰𐌿𐍃𐌴𐌹 𐌿𐌽𐍃 𐌰𐍆 𐌸𐌰𐌼𐌼𐌰 𐌿𐌱𐌹𐌻𐌹𐌽
    𐌿𐌽𐍄𐌴 𐌸𐌴𐌹𐌽𐌰 𐌹𐍃𐍄 𐌸𐌹𐌿𐌳𐌰𐌽𐌲𐌰𐍂𐌳𐌹 𐌾𐌰𐌷 𐌼𐌰𐌷𐍄𐍃
    𐌾𐌰𐌷 𐍅𐌿𐌻𐌸𐌿𐍃 𐌹𐌽 𐌰𐌹𐍅𐌹𐌽𐍃
    
    𐌱𐍂𐌿𐌽𐌰𐌹𐌼 𐌱𐌰𐌹𐍂𐌹𐌸 𐌱𐌰𐌹𐍂𐌺𐌰 𐌱𐍉𐌲𐌿𐌼
    𐌻𐌰𐌿𐌱𐌰𐌽𐍃 𐌻𐌹𐌿𐌱𐌰𐌽𐍃 𐌻𐌹𐌿𐌳𐌰𐌽𐌳𐌴𐌹
    𐌲𐌹𐌻𐍅𐌰𐌲𐍂𐍉𐌽𐌹 𐌲𐌻𐌹𐍄𐌼𐌿𐌽𐌾𐌰𐌽𐌳𐌴𐌹
    𐌱𐌰𐌲𐌼𐌴 𐌱𐌻𐍉𐌼𐌰 𐌱𐌻𐌰𐌿𐌰𐌽𐌳𐌴𐌹
    𐍆𐌰𐌲𐍂𐌰𐍆𐌰𐌷𐍃𐌰 𐌻𐌹𐌸𐌿𐌻𐌹𐌽𐌸𐌹
    𐍆𐍂𐌰𐌿𐌾𐌹𐌽𐍉𐌽𐌳𐌴𐌹 𐍆𐌰𐌹𐍂𐌲𐌿𐌽𐌹
    𐍅𐍉𐍀𐌾𐌰𐌽𐌳 𐍅𐌹𐌽𐌳𐍉𐍃 𐍅𐌰𐌲𐌾𐌰𐌽𐌳 𐌻𐌹𐌽𐌳𐍉𐍃
    𐌻𐌿𐍄𐌹𐌸 𐌻𐌹𐌼𐌰𐌼 𐌻𐌰𐌹𐌺𐌰𐌽𐌳𐌴𐌹
    𐍃𐌻𐌰𐌹𐌷𐍄𐌰 𐍂𐌰𐌹𐌷𐍄𐌰 𐌷𐍅𐌴𐌹𐍄𐌰𐍂𐌹𐌽𐌳𐌰
    𐍂𐌰𐌶𐌳𐌰 𐍂𐍉𐌳𐌴𐌹𐌸 𐍂𐌴𐌹𐍂𐌰𐌽𐌳𐌴𐌹
    𐌱𐌰𐌽𐌳𐍅𐌰 𐌱𐌰𐌹𐍂𐌷𐍄𐌰 𐍂𐌿𐌽𐌰 𐌲𐍉𐌳𐌰
    𐌸𐌹𐌿𐌳𐌰 𐌼𐌴𐌹𐌽𐌰 𐌸𐌹𐌿𐌸𐌾𐌰𐌽𐌳𐌴𐌹
    𐌰𐌽𐌳𐌰𐌽𐌰𐌷𐍄𐌹 𐌼𐌹𐌻𐌷𐌼𐌰𐌼 𐌽𐌴𐌹𐍀𐌹𐌸
    𐌻𐌹𐌿𐌷𐍄𐌴𐌹𐌸 𐌻𐌹𐌿𐌷𐌼𐌰𐌼 𐌻𐌰𐌿𐌷𐌼𐌿𐌽𐌹
    𐌻𐌰𐌿𐌱𐍉𐍃 𐌻𐌹𐌿𐌱𐌰𐌹 𐍆𐌻𐌹𐌿𐌲𐌰𐌽𐌳 𐌻𐌰𐌿𐍃𐌰𐌹
    𐍄𐌿𐌻𐌲𐌿𐍃 𐍄𐍂𐌹𐌲𐌲𐍅𐌰 𐍃𐍄𐌰𐌽𐌳𐌰𐌽𐌳𐌴𐌹
    𐌱𐌰𐌹𐍂𐌺𐌰 𐌱𐌰𐌶𐌰 𐌱𐌴𐌹𐌳𐌹𐌸 𐌱𐌻𐌰𐌹𐌺𐌰
    𐍆𐍂𐌰𐌿𐌾𐌹𐌽𐍉𐌽𐌳𐌴𐌹 𐍆𐌰𐌹𐍂𐌲𐌿𐌽𐌹
    
    𐌱.𐌿𐌽𐌰𐌹𐌼.𐌱𐌰𐌹.𐌹𐌸.𐌱𐌰𐌹..𐌰.𐌱.𐌲𐌿𐌼
    𐌻𐌰𐌿𐌱𐌰𐌽𐍃.𐌻𐌹𐌿𐌱𐌰𐌽𐍃.𐌻𐌹𐌿𐌳𐌰𐌽𐌳.𐌹
    𐌲𐌹𐌻𐍅𐌰𐌲..𐌽𐌹.𐌲𐌻𐌹.𐌼𐌿𐌽𐌾𐌰𐌽𐌳.𐌹
    𐌱𐌰𐌲𐌼..𐌱𐌻.𐌼𐌰.𐌱𐌻𐌰𐌿𐌰𐌽𐌳.𐌹
    𐍆𐌰𐌲.𐌰𐍆𐌰𐌷𐍃𐌰.𐌻𐌹𐌸𐌿𐌻𐌹𐌽𐌸𐌹
    𐍆.𐌰𐌿𐌾𐌹𐌽.𐌽𐌳.𐌹.𐍆𐌰𐌹.𐌲𐌿𐌽𐌹
    𐍅..𐌾𐌰𐌽𐌳.𐍅𐌹𐌽𐌳.𐍃.𐍅𐌰𐌲𐌾𐌰𐌽𐌳.𐌻𐌹𐌽𐌳.𐍃
    𐌻𐌿.𐌹𐌸.𐌻𐌹𐌼𐌰𐌼.𐌻𐌰𐌹.𐌰𐌽𐌳.𐌹
    𐍃𐌻𐌰𐌹𐌷.𐌰..𐌰𐌹𐌷.𐌰.𐌷𐍅.𐌹.𐌰.𐌹𐌽𐌳𐌰
    .𐌰.𐌳𐌰...𐌳.𐌹𐌸...𐌹.𐌰𐌽𐌳.𐌹
    𐌱𐌰𐌽𐌳𐍅𐌰.𐌱𐌰𐌹.𐌷.𐌰..𐌿𐌽𐌰.𐌲.𐌳𐌰
    𐌸𐌹𐌿𐌳𐌰.𐌼.𐌹𐌽𐌰.𐌸𐌹𐌿𐌸𐌾𐌰𐌽𐌳.𐌹
    𐌰𐌽𐌳𐌰𐌽𐌰𐌷.𐌹.𐌼𐌹𐌻𐌷𐌼𐌰𐌼.𐌽.𐌹.𐌹𐌸
    𐌻𐌹𐌿𐌷..𐌹𐌸.𐌻𐌹𐌿𐌷𐌼𐌰𐌼.𐌻𐌰𐌿𐌷𐌼𐌿𐌽𐌹
    𐌻𐌰𐌿𐌱.𐍃.𐌻𐌹𐌿𐌱𐌰𐌹.𐍆𐌻𐌹𐌿𐌲𐌰𐌽𐌳.𐌻𐌰𐌿𐍃𐌰𐌹
    .𐌿𐌻𐌲𐌿𐍃...𐌹𐌲𐌲𐍅𐌰.𐍃.𐌰𐌽𐌳𐌰𐌽𐌳.𐌹
    𐌱𐌰𐌹..𐌰.𐌱𐌰.𐌰.𐌱.𐌹𐌳𐌹𐌸.𐌱𐌻𐌰𐌹.𐌰
    𐍆.𐌰𐌿𐌾𐌹𐌽.𐌽𐌳.𐌹.𐍆𐌰𐌹.𐌲𐌿𐌽𐌹
    
  4. V said

    In Ruby

      
    def encipher(key, text)
      words = key.scan(/\w+/)
      text.scan(/\w+/).flat_map do |word|
        word.chars.map do |char, i|
          words
            .each_with_index
            .select { |word, _| word.chars.first == char }
            .sample
            .last + 1
        end
      end
    end
    
    def decipher(key, cipher)
      char_map = Hash[
        key
          .scan(/\w+/)
          .each_with_index
          .map { |w, i| [i + 1, w[0]] }
      ]
    
      cipher.map { |char| char_map[char] }.join
    end
    
    encipher("now is the time" , "tin")
     => [4, 2, 1]
    
    decipher("now is the time",  [4, 2, 1])
      => "Tin"
    
    key = "lorem ipsum dolor sit amet, consectetur adipisicing elit"
    decipher(key, encipher(key, "lisa is ace"))
      => "lisaisace"
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: