Bacon cipher: Difference between revisions

(→‎{{header|REXX}}: added a second REXX version with a couple of very different glyphs.)
(→‎{{header|REXX}}: Added zkl)
Line 406:
cipher text: ┬┴┴┬┬┴┴┬┬┬┴┴┬┴┴┬┬┴┬┴┬┴┴┴┴┬┴┬┴┴┴┬┴┴┴┴┴┴┬┴┴┬┴┬┴┬┬┴┬┴┴┴┴┴┬┬┴┴┴┬┴┬┬┬┴┬┴┬┬┴┴┬┬┴┬┬┬┴┬┴┴┴┬┴┬┴┬┬┬┴┬┴┬┬┬┬┬┴┬┴┴┬┴┴┬┬┴┬┴┴┴┬┬┴┴┴┬┬┬┬┴┴┬┴┴┴┴┴┬┬┬┬┴┬┴┴┬┬┬┴┬┴┬┴┬┴┴┬┴┴┬┴┴┴┬┬┬┴┬┴┬┴┴┬┬┴┴┬┬┬┴┴┬┴┴┬┬┴┬┴┴┬┴┬┬┴┴┴┴┴┬┬┴┴┬┬┬┴┴┴┬┬┴┬┴┴┴┴┬┬┴┬┬┬┴┴┴┬┬┴┬┬┴┬┬
cycled text: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
</pre>
 
=={{header|zkl}}==
{{trans|Python}}
<lang zkl>class Bacon{
fcn init(_keyText){
var [const] keyText=_keyText.toLower(),
lowerLetters=Utils.Helpers.lowerLetters,
upperLetters=Utils.Helpers.upperLetters,
letters=String(lowerLetters,upperLetters),
lc2bin=[0..].zip(lowerLetters + " .") //-->("a":"00000", ...)
.pump(Dictionary(),fcn([(n,ch)]){ T(ch,"%05.2B".fmt(n)) }),
bin2lc=lc2bin.pump(Dictionary(),"copy",T("swap",0,1)); //-->("00000":"a", ...),
;
}
fcn to5binary(msg){ //-->stream of 1s and 0s
msg.toLower().pump(String,lc2bin.get.fp1(""))
.pump(Data,"toAsc",'-(0x30)).howza(0)
}
fcn encrypt(msg){ msg=msg.toLower();
out,bin5:=Sink(String), to5binary(msg); // capitalization overlay
keyText:=self.keyText.walker(); // keyText as char stream
foreach capitalise in (bin5){
while(keyText){
ch:=keyText.next();
if(lowerLetters.holds(ch)){
out.write(capitalise and ch.toUpper() or ch);
break;
}
out.write(ch);
}
fallthrough{ throw(Exception.ValueError("ERROR: Ran out of characters in key text")) }
}
out.write("...").close() // pad
}
fcn decrypt(bacontext){
bacontext.filter(letters.holds).pump(String,T(Void.Read,4),
fcn{ vm.arglist.pump(String,upperLetters.holds,"toInt") : bin2lc[_] });
}
}</lang>
<lang zkl>bacon:=Bacon(
#<<<
0'|All children, except one, grow up. They soon know that they will grow
up, and the way Wendy knew was this. One day when she was two years old
she was playing in a garden, and she plucked another flower and ran with
it to her mother. I suppose she must have looked rather delightful, for
Mrs. Darling put her hand to her heart and cried, "Oh, why can't you
remain like this for ever!" This was all that passed between them on
the subject, but henceforth Wendy knew that she must grow up. You always
know after you are two. Two is the beginning of the end.
Of course they lived at 14 [their house number on their street], and
until Wendy came her mother was the chief one. She was a lovely lady,
with a romantic mind and such a sweet mocking mouth. Her romantic
mind was like the tiny boxes, one within the other, that come from the
puzzling East, however many you discover there is always one more; and
her sweet mocking mouth had one kiss on it that Wendy could never get,
though there it was, perfectly conspicuous in the right-hand corner.|
);
#<<<
 
phrase:="Rosetta code Bacon cipher example secret phrase to encode in the capitalization of peter pan";
println("PLAINTEXT = \n%s".fmt(phrase));
encrypted:=bacon.encrypt(phrase); println("ENCRYPTED = \n%s".fmt(encrypted));
decrypted:=bacon.decrypt(encrypted); println("DECRYPTED = \n%s".fmt(decrypted));
if(phrase.toLower()!=decrypted) throw(Exception.AssertionError("Round-tripping error"));</lang>
{{out}}
<pre>
PLAINTEXT =
Rosetta code Bacon cipher example secret phrase to encode in the capitalization of peter pan
ENCRYPTED =
All cHiLDReN, exCept One, GroW UP. thEY soon kNOw That tHey WILl groW
Up, aNd tHE wAy wendY knew was tHis. ONE daY WhEN ShE was tWo yEars oLD
SHe wAS PlaYinG in a GARdEn, anD shE pLUCked anoTHer fLOWEr AnD Ran WitH
It To Her MothEr. i supPoSe shE muSt hAve LOOKeD raTHER deLIGHtfuL, for
mrS. daRlinG puT HeR hAnd TO hER HeARt And cRied, "OH, wHy caN't yOU
RemaiN LikE thIS fOr eVer!" thIS wAS AlL tHat PAssED BetWeeN ThEm on
tHe subjecT, BUT hEnceForTH wendy kNeW ThAt shE MusT grow uP. yoU AlWays
kNOW afTEr YOU aRe tWO. Two iS tHE BeGinNING of The End.
OF coUrsE theY LIvEd aT 14 [THEir housE NuM...
DECRYPTED =
rosetta code bacon cipher example secret phrase to encode in the capitalization of peter pan
</pre>
Anonymous user