Bacon cipher: Difference between revisions

→‎Raku: no cheating, Bacon cipher solution (gist)
(Added Arturo implementation)
(→‎Raku: no cheating, Bacon cipher solution (gist))
Line 2,225:
signify anything). This example will work with anything in the
ASCII range... even code! $r%_-^&*(){}+~ #=`/\';*1234567890"'</pre>
 
=== Bacon cipher solution ===
<lang perl6>my @abc = 'a' .. 'z';
my @symb = ' ', |@abc; # modified Baconian charset - space and full alphabet
# TODO original one with I=J U=V, nice for Latin
 
my %bacon = @symb Z=> (^@symb).map(*.base(2).fmt("%05s"));
my %nocab = %bacon.antipairs;
 
sub bacon ($s, $msg) {
my $raw = ( [$_] for $s.lc.comb );
my $txt = $raw.grep(*.[0] (elem) @abc);
for $msg.lc.comb Z $txt.batch(5) -> ($c-msg, @a) {
for @a.kv -> $i, $c-str {
my $x := @a[$i][0];
$x = $x.uc if %bacon{$c-msg}.comb[$i].Int.Bool;
} }
$raw.map(*.[0]).join;
}
 
sub unbacon ($s) {
my $chunks = ($s ~~ m:g/\w+/)>>.Str.join.comb(/.**5/);
$chunks.map(*.comb.map({.uc eq $_})».Int».Str.join).map({%nocab{$_}}).join;
}
 
my $msg = "Omnes homines dignitate et iure liberi et pares nascuntur";
 
my $str = <Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa
qui officia deserunt mollit anim id est laborum.>;
 
my $baconed = bacon $str, $msg;
$baconed .= naive-word-wrapper
if "naive-word-wrapper" (elem) "".^method_names; # FIXME makes dbl space after .
say "text:\n$baconed\n";
my $unbaconed = unbacon($baconed).trim.uc;
say "hidden message:\n$unbaconed";</lang>
 
{{out}}
<pre>text:
lOREM iPSuM dOLOr siT aMEt, cONsectetUr adiPISCiNG eLiT, seD dO EIusmOd
TEmpOR incididUnt uT laBorE ET dOLOre MagNA aLiqua. ut ENiM ad miNiM
veniam, qUiS NoStrud exerCitATiOn ULlaMco lAbOris nisI Ut alIquIp ex Ea
coMmODo cOnsEquAt. duis auTe IRuRe dolor iN reprehenDEriT in vOlUPtaTE
velit eSSE cilluM DolORe eu FUGiAt NuLLA pArIatUr. ExCEptEur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
 
hidden message:
OMNES HOMINES DIGNITATE ET IURE LIBERI ET PARES NASCUNTUR</pre>
 
=={{header|REXX}}==
Anonymous user