Bacon cipher: Difference between revisions

Added Agena
(Added Algol 68)
(Added Agena)
Line 16:
# Show an example plaintext message encoded and then decoded here on this page.
 
 
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
{{Trans|ALGOL 68}}
<lang agena># Bacon cipher
 
# Bacon's letter codes but with distinct values for i & j and u & v and an extra for any non-letter
baconCodes := [ "A" ~ "AAAAA", "B" ~ "AAAAB", "C" ~ "AAABA", "D" ~ "AAABB", "E" ~ "AABAA"
, "F" ~ "AABAB", "G" ~ "AABBA", "H" ~ "AABBB", "I" ~ "ABAAA", "J" ~ "ABAAB"
, "K" ~ "ABABA", "L" ~ "ABABB", "M" ~ "ABBAA", "N" ~ "ABBAB", "O" ~ "ABBBA"
, "P" ~ "ABBBB", "Q" ~ "BAAAA", "R" ~ "BAAAB", "S" ~ "BAABA", "T" ~ "BAABB"
, "U" ~ "BABAA", "V" ~ "BABAB", "W" ~ "BABBA", "X" ~ "BABBB", "Y" ~ "BBAAA"
, "Z" ~ "BBAAB", "*" ~ "BBBAA"
];
# yields plain text encoded via stego template
toBacon :=
proc( plainText :: string, stegoTemplate :: string ) :: string is
local stegoPos := 0;
local stegoLen := size stegoTemplate;
# selects the next character from the stego template - wraps-around from the end to the beginning
local nextStegoPos := proc() is inc stegoPos, 1; mod stegoPos, stegoLen end;
# encode the plain text
local encoded := "";
for pos to size plainText do
# get the Bacon code of the next character #
local plainChar := upper( plainText[ pos ] );
local code := baconCodes[ ( ( plainChar < "A" or plainChar > "Z" ) and "*" or plainChar ) ];
for c to size code do
# copy punctuation as is from the stego template to the result
local s := upper( stegoTemplate[ stegoPos + 1 ] );
while s < "A" or s > "Z" do
encoded := encoded & s;
nextStegoPos();
s := ( ( stegoPos < size stegoTemplate ) and upper( stegoTemplate[ stegoPos + 1 ] ) or "A" )
od;
# encode the character by changing the case of the stego character as appropriate
local templateChar := stegoTemplate[ stegoPos + 1 ];
encoded := encoded & ( ( code[ c ] = "A" ) and lower( templateChar ) or upper( templateChar ) )
nextStegoPos()
od
od;
return encoded
end ; # toBacon
# yields bacon text decoded via stego template
toPlain := proc( baconText :: string, stegoTemplate :: string ) :: string is
local decoded := "";
local codedChar := 0;
local letters := 0;
local codeLength := size baconCodes[ "A" ];
for pos to size baconText do
local c := baconText[ pos ];
if c >= "a" and c <= "z"
then
# lower case letter
mul codedChar, 2;
inc letters, 1
elif c >= "A" and c <= "Z"
then
# upper case letter
mul codedChar, 2;
inc codedChar, 1;
inc letters, 1
fi;
if letters = codeLength
then
# have a full letter to decode
decoded := decoded & ( ( codedChar > 25 ) and " " or char( abs( "a" ) + codedChar ) )
letters := 0;
codedChar := 0
fi
od;
return decoded
end ; # toPlain
# test encode and decode
scope
local nl := char( 10 );
local testTemplate := "bacon's cipher is a method of steganography created by francis bacon." & nl
& "this task is to implement a program for encryption and decryption of " & nl
& "plaintext using the simple alphabet of the baconian cipher or some " & nl
& "other kind of representation of this alphabet (make anything signify anything). " & nl
& "the baconian alphabet may optionally be extended to encode all lower " & nl
& "case characters individually and/or adding a few punctuation characters " & nl
& "such as the space." & nl
;
local plainText := "the quick brown fox jumps over the lazy dog";
local baconEncoded := toBacon( plainText, testTemplate );
local baconDecoded := toPlain( baconEncoded, testTemplate );
print( "encoded..." );
print( baconEncoded );
print( "-----------------------------------------------------" );
print( "decoded..." );
print( baconDecoded );
print( "-----------------------------------------------------" );
print( ( ( baconDecoded <> plainText ) and "UNSUCESSFUL" or "sucessful" ), " decode" )
epocs</lang>
{{out}}
<pre>
encoded...
BacON's cIPHer Is a METhoD of stEgAnogRaphy crEatEd By FRAncis baCOn.
thIs TASk Is TO imPLeMENT a proGrAm FOR eNcRYPTIOn anD deCRyPtioN Of
plAINTExt UsING the SIMpLe AlPhaBet Of thE BAConIan CIphER Or sOme
OTHer kInD Of reprESenTATion OF This alPHaBET (makE An
-----------------------------------------------------
decoded...
the quick brown fox jumps over the lazy dog
-----------------------------------------------------
sucessful decode
</pre>
 
=={{header|ALGOL 68}}==
3,048

edits