Bacon cipher

From Rosetta Code
Bacon cipher is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 anything signify anything).

The Baconian alphabet:

a   AAAAA   g     AABBA   n    ABBAA   t     BAABA
b   AAAAB   h     AABBB   o    ABBAB   u-v   BAABB
c   AAABA   i-j   ABAAA   p    ABBBA   w     BABAA
d   AAABB   k     ABAAB   q    ABBBB   x     BABAB
e   AABAA   l     ABABA   r    BAAAA   y     BABBA
f   AABAB   m     ABABB   s    BAAAB   z     BABBB
  1. The Baconian alphabet may optionally be extended to encode all lower case characters individually and/or adding a few punctuation characters such as the space.
  2. It is impractical to use the original change in font for the steganography. For this task you must provide an example that uses a change in the case of successive alphabetical characters instead. Other examples for the language are encouraged to explore alternative steganographic means.
  3. Show an example plaintext message encoded and then decoded here on this page.


Agena

Tested with Agena 2.9.5 Win32

Translation of: ALGOL 68

<lang agena># Bacon cipher

  1. 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"
             ];
  1. 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[ if plainChar < "A" or plainChar > "Z" then "*" else plainChar fi ];
           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       := if stegoPos < size stegoTemplate then upper( stegoTemplate[ stegoPos + 1 ] ) else "A" fi
               od;
               # encode the character by changing the case of the stego character as appropriate
               local templateChar := stegoTemplate[ stegoPos + 1 ];
               encoded := encoded & if code[ c ] = "A" then lower( templateChar ) else upper( templateChar ) fi;
               nextStegoPos()
           od
       od;
       return encoded
   end ; # toBacon
  1. 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 & if codedChar > 25 then " " else char( abs( "a" ) + codedChar ) fi;
           letters   := 0;
           codedChar := 0
       fi
   od;
   return decoded

end ; # toPlain

  1. 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( if baconDecoded <> plainText then "UNSUCESSFUL" else "sucessful" fi, " decode" )

epocs</lang>

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

ALGOL 68

<lang algol68># Bacon's letter codes but with distinct values for i & j and u & v and an extra for any non-letter # []STRING bacon codes = ( #a# "AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA"

                      , #j# "ABAAB", "ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA", "ABBBB", "BAAAA", "BAAAB"
                      , #s# "BAABA", "BAABB", "BABAA", "BABAB", "BABBA", "BABBB", "BBAAA", "BBAAB", "BBBAA"
                      );
  1. operators to convert case #

OP LCASE = ( CHAR c )CHAR: IF c < "A" OR c > "Z" THEN c ELSE REPR ( ( ABS c - ABS "A" ) + ABS "a" ) FI; OP UCASE = ( CHAR c )CHAR: IF c < "a" OR c > "z" THEN c ELSE REPR ( ( ABS c - ABS "a" ) + ABS "A" ) FI;

  1. yields plain text encoded via stego template #

PROC to bacon = ( STRING plain text, STRING stego template )STRING:

    BEGIN
        INT    stego pos   := 0;
        INT    stego len    = ( UPB stego template + 1 ) - LWB stego template;
        INT    stego start  = LWB stego template;
        # selects the next character from the stego template - wraps-around from the end to the beginning #
        PROC next stego pos = VOID: ( stego pos +:= 1; stego pos MODAB stego len);
        # encode the plain text #
        STRING encoded     := "";
        FOR pos FROM LWB plain text TO UPB plain text DO
            # get the Bacon code of the next character #
            CHAR   plain char = UCASE plain text[ pos ];
            STRING code =
                bacon codes[ IF plain char < "A" OR plain char > "Z" THEN UPB bacon codes ELSE ( ABS plain char - ABS "A" ) + 1 FI ];
            FOR c FROM LWB code TO UPB code DO
                # copy punctuation as is from the stego template to the result #
                WHILE CHAR s := UCASE stego template[ stego pos + stego start ];
                      s < "A" OR s > "Z"
                DO
                    encoded   +:= s;
                    next stego pos
                OD;
                # encode the character by changing the case of the stego character as appropriate #
                CHAR template char = stego template[ stego pos + stego start ];
                encoded +:= IF code[ c ] = "A" THEN LCASE template char ELSE UCASE template char FI;
                next stego pos
            OD
        OD;
        encoded
    END ; # to bacon #
  1. yields bacon text decoded via stego template #

PROC to plain = ( STRING bacon text, stego template )STRING:

    BEGIN
        STRING decoded := "";
        INT coded char := 0;
        INT letters    := 0;
        INT code length = ( UPB bacon codes[ 1 ] - LWB bacon codes[ 1 ] ) + 1;
        FOR pos FROM LWB bacon text TO UPB bacon text DO
            CHAR c = bacon text[ pos ];
            IF c >= "a" AND c <= "z"
            THEN
                # lower case letter #
                coded char *:= 2;
                letters    +:= 1
            ELIF c >= "A" AND c <= "Z"
            THEN
                # upper case letter #
                coded char *:= 2;
                coded char +:= 1;
                letters    +:= 1
            FI;
            IF letters = code length
            THEN
                # have a full letter to decode #
                decoded    +:= IF coded char > 25 THEN " " ELSE REPR ( ABS "a" + coded char ) FI;
                letters     := 0;
                coded char  := 0
            FI
        OD;
        decoded
    END ; # to plain #
  1. test encode and decode #

STRING test template = "bacon's cipher is a method of steganography created by francis bacon." + REPR 10

                    + "this task is to implement a program for encryption and decryption of "            + REPR 10
                    + "plaintext using the simple alphabet of the baconian cipher or some "              + REPR 10
                    + "other kind of representation of this alphabet (make anything signify anything). " + REPR 10
                    + "the baconian alphabet may optionally be extended to encode all lower "            + REPR 10
                    + "case characters individually and/or adding a few punctuation characters "         + REPR 10
                    + "such as the space."                                                               + REPR 10
                    ;

STRING plain text = "the quick brown fox jumps over the lazy dog"; STRING bacon encoded = to bacon( plain text, test template ); STRING bacon decoded = to plain( bacon encoded, test template ); print( ( "encoded...", newline ) ); print( ( bacon encoded, newline ) ); print( ( "-----------------------------------------------------", newline, "decoded...", newline ) ); print( ( bacon decoded, newline ) ); print( ( "-----------------------------------------------------", newline ) ); print( ( IF bacon decoded /= plain text THEN "UNSUCESSFUL" ELSE "sucessful" FI, " decode", newline ) )</lang>

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

C++

Bacon cipher implementation

<lang cpp>

  1. include <iostream>
  2. include <algorithm>
  3. include <vector>
  4. include <bitset>
  5. include <string>

class bacon { public:

   bacon() {
       int x = 0;
       for( ; x < 9; x++ )
           bAlphabet.push_back( std::bitset<5>( x ).to_string() );
       bAlphabet.push_back( bAlphabet.back() );
       
       for( ; x < 20; x++ )
           bAlphabet.push_back( std::bitset<5>( x ).to_string() );
       bAlphabet.push_back( bAlphabet.back() );
       
       for( ; x < 24; x++ )
           bAlphabet.push_back( std::bitset<5>( x ).to_string() );
   }
   std::string encode( std::string txt ) {
       std::string r;
       size_t z;
       for( std::string::iterator i = txt.begin(); i != txt.end(); i++ ) {
           z = toupper( *i );
           if( z < 'A' || z > 'Z' ) continue;
           r.append( bAlphabet.at( ( *i & 31 ) - 1 ) );
       }
       return r;
   }
   std::string decode( std::string txt ) {
       size_t len = txt.length();
       while( len % 5 != 0 ) len--;
       if( len != txt.length() ) txt = txt.substr( 0, len );
       std::string r;
       for( size_t i = 0; i < len; i += 5 ) {
           r.append( 1, 'A' + std::distance( bAlphabet.begin(), std::find( bAlphabet.begin(), bAlphabet.end(), txt.substr( i, 5 ) ) ) );
       }
       return r;
   }

private:

   std::vector<std::string> bAlphabet;

}; </lang>

These next 2 classes use the 0's & 1's generated by the 'Bacon encryption' to create different the outputs. One could go wild here... <lang cpp> class cipherI { public:

   std::string encode( std::string txt ) {
       txt = b.encode( txt );
       std::string e, d = "one morning, when gregor samsa woke from troubled dreams, he found himself transformed "
       "in his bed into a horrible vermin. he lay on his armour-like back, and if he lifted his head a little he "
       "could see his brown belly, slightly domed and divided by arches into stiff sections.";
       size_t r = 0; 
       char t;
       for( std::string::iterator i = txt.begin(); i != txt.end(); i++ ) {
           t = d.at( r );
           while( t < 'a' || t > 'z' ) {
               e.append( 1, t );
               r++;
               t = d.at( r );
           }
           r++;
           e.append( 1, *i == '1' ? t - 32 : t );
       }
       return e;
   }
   std::string decode( std::string txt ) {
       std::string h;
       for( std::string::iterator i = txt.begin(); i != txt.end(); i++ ) {
           if( *i < 'a' && ( *i < 'A' || *i > 'Z' ) || *i > 'z' ) continue;
           h.append( 1, *i & 32 ? '0' : '1' );
       }
       return b.decode( h );
   }

private:

   bacon b;

};

class cipherII { public:

   std::string encode( std::string txt ) {
       txt = b.encode( txt );
       std::string e;
       for( std::string::iterator i = txt.begin(); i != txt.end(); i++ )
           e.append( 1, *i == '0' ? 0xf9 : 0xfa );
       return e;
   }
   std::string decode( std::string txt ) {
       std::string h;
       for( std::string::iterator i = txt.begin(); i != txt.end(); i++ ) {
           h.append( 1, *i == ( char )0xf9 ? '0' : '1' );
       }
       return b.decode( h );
   }

private:

   bacon b;

};

int main( int argc, char* argv[] ) {

   cipherI c1;
   cipherII c2;
   std::string s = "lets have some fun with bacon cipher";
   std::string h1 = c1.encode( s ),
               h2 = c2.encode( s );
   std::cout << h1 << std::endl << std::endl << c1.decode( h1 ) << std::endl << std::endl;
   std::cout << h2 << std::endl << std::endl << c2.decode( h2 ) << std::endl << std::endl;
   return 0;

} </lang>

Output:
oNe MornIng, WheN gRegoR saMSA woke fRom TRouBleD dreAmS, He FoUnD HimSelf tRaNS
foRMeD In hIs Bed iNto a HorRiblE VErmin. He lay on hiS arMOuR-lIKe back, And If
 he lIFTed hIS HeaD a lIttle

LETSHAUESOMEFUNWITHBACONCIPHER

¨·¨·¨¨¨·¨¨·¨¨·¨·¨¨¨·¨¨···¨¨¨¨¨·¨¨··¨¨·¨¨·¨¨¨·¨··¨·¨·¨··¨¨·¨¨¨¨·¨··¨¨··¨··¨¨·¨·¨¨
¨·¨¨¨·¨¨·¨¨¨···¨¨¨¨·¨¨¨¨¨¨¨¨·¨¨··¨·¨··¨¨¨¨¨·¨¨·¨¨¨¨···¨¨¨···¨¨·¨¨·¨¨¨¨

LETSHAUESOMEFUNWITHBACONCIPHER

Haskell

Necessary imports <lang Haskell>import Data.List (findIndex, unfoldr) import Data.Char (isAlpha, isUpper, toUpper, toLower) import Data.Bool (bool)</lang>

The list of characters to be encoded: <lang Haskell>chars = ['a'..'z'] ++ ['0'..'9'] ++ ",.;?! " bitsPerChar = 6 :: Int</lang>

Some simple helper functions: <lang Haskell>toBinary :: Int -> [Bool] toBinary = unfoldr (pure . (\(a,b)->(odd b,a)) . (`divMod` 2))

fromBinary :: [Bool] -> Int fromBinary = foldr (\x n -> 2*n + bool 0 1 x) 0</lang>

And, finally main functions -- encoding: <lang Haskell>encode :: String -> String -> Either String String encode message txt = do

 mask <- traverse coding message
 zipAlphas (bool toLower toUpper) (concat mask) txt
 where
   coding ch = case findIndex (== ch) chars of
     Nothing -> Left $ "Unknown symbol " ++ show ch
     Just i -> Right $ take bitsPerChar (toBinary i)
   zipAlphas f = go
     where go _ [] = Left "Text is not long enough!"
           go [] _ = Right []
           go (x:xs) (y:ys) | isAlpha y = (f x y :) <$> go xs ys
                            | otherwise = (y :) <$> go (x:xs) ys</lang>

And decoding: <lang Haskell>decode :: String -> String decode = map decipher . chunksOf bitsPerChar . filter isAlpha

 where
   decipher = (chars !!) . min (length chars-1) . fromBinary . map isUpper
   chunksOf n = takeWhile (not . null) . unfoldr (pure . splitAt n)</lang>

Examples

<lang Haskell>text = concat ["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 anything signify "
              ,"anything). The Baconian alphabet may optionally be extended to encode "
              ,"all lower case characters individually and/or adding a few punctuation "
              ,"characters such as the space." ]

message = "the quick brown fox jumps over the lazy dog"</lang>

<lang Haskell>λ> let m = encode message text λ> m Right "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 Anything sIgnIFy anyTHiNg). tHe BAConian ALPhabET may" λ> decode <$> m Right "the quick brown fox jumps over the lazy dog" λ> encode "something wrong @ in the message" text Left "Unknown symbol '@'" λ> encode message "abc" Left "Text is not long enough!"</lang>


J

Implementation:

<lang J>alfa=: 'ABCDEFGHIKLMNOPQRSTUWXYZ' beta=: 26{.(}.~i.&'A')a. norm=: ([ -. -.)&alfa@(rplc&('JIVU'))@toupper enca=:(5#2),@:#:alfa i. norm gena=: ]`((,:tolower)@(beta {~ 26 ?@#~ #))}

encrypt=: gena@enca@norm decrypt=: alfa {~ _5 #.\ 90 < a.&i.</lang>

We use random letters as the basis for our steganography and we use case to represent "font".

Example use:

<lang J> encrypt 'this is a test' nWVkJAPkamEuUJIeTGKnUsTVRfAWWuNBIIHdEIcOAPuTBeXKQduQAdU

  encrypt 'this is a test'

sFLkBQKqqaQsGGXzAXQsKlZFBcILRlUIRAQaEQoNUBcHIhFTWbeRAlM

  decrypt encrypt 'this is a test'

THISISATEST</lang>

Kotlin

The 'full' Bacon alphabet, which has separate letters for i, j, u and v, has been used in the following: <lang scala>// version 1.0.6

object Bacon {

   private val codes = mapOf('a' to "AAAAA", 'b' to "AAAAB", 'c' to "AAABA", 'd' to "AAABB", 'e' to "AABAA",
                             'f' to "AABAB", 'g' to "AABBA", 'h' to "AABBB", 'i' to "ABAAA", 'j' to "ABAAB",
                             'k' to "ABABA", 'l' to "ABABB", 'm' to "ABBAA", 'n' to "ABBAB", 'o' to "ABBBA",
                             'p' to "ABBBB", 'q' to "BAAAA", 'r' to "BAAAB", 's' to "BAABA", 't' to "BAABB",
                             'u' to "BABAA", 'v' to "BABAB", 'w' to "BABBA", 'x' to "BABBB", 'y' to "BBAAA",
                             'z' to "BBAAB", ' ' to "BBBAA") // use ' ' to denote any non-letter
   fun encode(plainText: String, message: String): String {
       val pt = plainText.toLowerCase()
       val sb = StringBuilder()
       for (c in pt)
           if (c in 'a'..'z') sb.append(codes[c])
           else               sb.append(codes[' '])        
       val et = sb.toString()
       val mg = message.toLowerCase()  // 'A's to be in lower case, 'B's in upper case
       sb.setLength(0)
       var count = 0
       for (c in mg) 
           if (c in 'a'..'z') {
               if (et[count] == 'A') sb.append(c)
               else                  sb.append(c - 32) // upper case equivalent
               count++
               if (count == et.length) break
           }
           else sb.append(c)    
       return sb.toString()
   }
   fun decode(message: String): String {
       val sb = StringBuilder()
       for (c in message)
           when (c) {
                in 'a'..'z' -> sb.append('A')
                in 'A'..'Z' -> sb.append('B')
           }
       val et = sb.toString()
       sb.setLength(0) 
       for (i in 0 until et.length step 5) {
           val quintet = et.substring(i, i + 5)
           val key = codes.entries.find { it.value == quintet }!!.key
           sb.append(key)
       }
       return sb.toString()
   }

}

fun main(args: Array<String>) {

   val plainText = "the quick brown fox jumps over the lazy dog"
   val message   = "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 anything signify anything). " + 
                   "the baconian alphabet may optionally be extended to encode all lower "            +
                   "case characters individually and/or adding a few punctuation characters "         +
                   "such as the space."  
   val cipherText  = Bacon.encode(plainText, message)
   println("Cipher text ->\n\n$cipherText")
   val decodedText = Bacon.decode(cipherText)
   println("\nHidden text ->\n\n$decodedText")

}</lang>

Output:
Cipher text ->

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

Hidden text ->

the quick brown fox jumps over the lazy dog

Lua

Based on C++ version <lang Lua> function Bacon( txt, secret, e )

   local alpha = {}, t
   function toBits( num, bits )
       local t, s = "", 0      
       for b = bits, 1, -1 do
           s = math.fmod( num, 2 )
           t = s .. t
           num = math.floor( num / 2 )
       end
       return t
   end
   
   function find( a, arr )
       for i = 1, #arr do
           if arr[i] == a then return i end
       end
       return -1
   end
   
   function toAlpha( secret )
       local str, z = "", 0
       secret = secret:upper()
       for i = 1, string.len( secret ) do
           z = secret:sub( i, i )
           if z < 'A' or z > 'Z'  then

str = str .. alpha[27] else k = z:byte( 1 ) - 65 + 1 str = str .. alpha[k] end

       end
       return str
   end
   function fromAlpha( secret )
       local l, msg, c, idx = secret:len(), "", 0, 0
       if math.fmod( l, 5 ) ~= 0 then
           print( "Message length does not match!" )
           return
       end
       for i = 1, l, 5 do

c = secret:sub( i, i + 4 )

           idx = find( c, alpha )

if idx > 0 then if idx == 27 then msg = msg .. " " -- unknown char - add space else msg = msg .. string.char( 64 + idx ) end end end return msg

   end
   
   function encode( txt, secret )
       local sec, encoded, idx = toAlpha( secret ), "", 0
       if sec:len() > txt:len() then
           print( "Text is too short!" )
          return 
       end

txt = txt:lower() for i = 1, string.len( sec ) do t = txt:sub( idx, idx )

           while( t < 'a' or t > 'z' ) do
               encoded = encoded .. t
               idx = idx + 1
               t = txt:sub( idx, idx )
           end
           idx = idx + 1
           if sec:sub( i, i ) == '1' then
               encoded = encoded .. string.char( t:byte(1) - 32 )
           else
               encoded = encoded .. t
           end

end

       return encoded
   end
   
   function decode( txt )
       local secret, c = "", 0
       for i = 1, string.len( txt ) do
           c = txt:sub( i, i )
           if not( c < 'a' and ( c < 'A' or c > 'Z' ) or c > 'z' ) then
               local s = 0
               if c == c:upper() then s = 1 end
               secret = secret .. s
           end
       end
       return fromAlpha( secret )
   end
   -- create alphabet 
   for i = 0, 26 do
       t = ( toBits( i, 5 ) )
       alpha[#alpha + 1] = t
   end
   
   -- encode or decode
   if e == 1 then 
       return encode( txt, secret )
   elseif e == 0 then
       return decode( secret )
   end

end

local a = Bacon( "Chase the pig around the house present belly, scratch hand when stroked. ".. "When in doubt, wash jump around on couch, meow constantly until given food, ".. "favor packaging over toy. And sometimes switches in french and say 'miaou' ".. "just because well why not has closed eyes but still sees you lick yarn hanging ".. "out of own butt so pelt around the house and up and down stairs chasing phantoms.", "Fall over dead, not really but gets sypathy", 1 ) print( a ) print( Bacon( "", a, 0 ) ) </lang>

Output:
>lua -e "io.stdout:setvbuf 'no'" "bco.lua" 
chAsE the pig ArOUnD tHE HOuSe pRESeNt BeLly, ScrAtch HANd When sTRokEd. when in douBT, WAsH jUMp AroUNd On COUcH, meOW COnStAntlY unTil given fOoD, FaVoR PACkagINg Over toY. AnD soMetIMES sWitcHEs in FreNch AND saY 'mIAoU' jUst BeCAuse wELL Why not Has CLosED EYEs bu
FALL OVER DEAD  NOT REALLY BUT GETS SYPATHY

Perl 6

Not truly a Bacon Cipher as it doesn't encode using font variations. But fits with the spirit if not the exact definition.

Works with: Rakudo version 2015-11-20

<lang perl6>my $secret = q:to/END/;

   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 anything
   signify anything). This example will work with anything in the
   ASCII range... even code! $r%_-^&*(){}+~ #=`/\';*1234567890"'
   END

my $text = q:to/END/;

   Bah. It isn't really practical to use typeface changes to encode
   information, it is too easy to tell that there is something going
   on and will attract attention. Font changes with enough regularity
   to encode mesages relatively efficiently would need to happen so
   often it would be obvious that there was some kind of manipulation
   going on. Steganographic encryption where it is obvious that there
   has been some tampering with the carrier is not going to be very
   effective. Not that any of these implementations would hold up to
   serious scrutiny anyway. Anyway, here's a semi-bogus implementation
   that hides information in white space. The message is hidden in this
   paragraph of text. Yes, really. It requires a fairly modern file
   viewer to display (not display?) the hidden message, but that isn't
   too unlikely any more. It may be stretching things to call this a
   Bacon cipher, but I think it falls within the spirit of the task,
   if not the exact definition.
   END
  1. '

my @enc = "", "​"; my %dec = @enc.pairs.invert;

sub encode ($c) { @enc[($c.ord).fmt("%07b").comb].join() }

sub hide ($msg is copy, $text) {

   $msg ~= @enc[0] x (0 - ($msg.chars % 8)).abs;
   my $head = $text.substr(0,$msg.chars div 8);
   my $tail = $text.substr($msg.chars div 8, *-1);
   ($head.comb «~» $msg.comb(/. ** 8/)).join() ~ $tail;

}

sub reveal ($steg) {

   join , map { :2(%dec{$_.comb}.join()).chr }, 
   $steg.subst( /\w | <punct> | " " | "\n" /, , :g).comb(/. ** 7/);

}

my $hidden = join , map { .&encode }, $secret.comb;

my $steganography = hide $hidden, $text;

say "Steganograpic message hidden in text:"; say $steganography;

say '*' x 70;

say "Hidden message revealed:"; say reveal $steganography;</lang>

Output:
Steganograpic message hidden in text:
B​​​​a​​​​h​​​​​.​​​ ​​​​I​​t​​​​​​ ​​​​​i​​​s​​​​​n​​​'​​​​t​​​​​ ​​r​​​​​e​​​​​​a​​​l​​​​l​​​​​​y​​​​ ​​​​​​p​​​​r​​​a​​c​​​t​​​i​​​​​​c​​​​​​a​​​​​l​​​​ ​​​​t​​o​​​​​​ ​​​​​​u​s​​​​​e​​​​​​ ​​​​​t​​​​​y​​​​p​​​​e​​​​f​​​​​​​a​​​​​c​​e​​​​ ​​​​​c​h​​​a​​​​​n​​​​​g​​​​​e​​​​​s​​​​ ​​​t​​​​​​​o​​​​​ ​​​​e​​​​​​n​​​c​​​o​​​​d​​​e​​​​​
i​​​​n​​​​​​f​​​​o​r​​​​​m​​​​​​a​​​​​t​​​​​i​​​​o​​​​n​​​,​​​​ ​​i​​​​​​t​​​​ ​​​​​​i​​​​s​​​ ​​t​​​​o​​​​​o​​​ ​​​e​​​a​​​​s​​​​​y​​ ​​​​​​​t​​​o​​​ ​​​​t​​​e​​l​​​l​​​​ ​​​​t​​​​​​​h​​​​​a​​​t​​​​​​ ​​t​​​​​h​​​​​e​​​r​​​e​​​​​ ​i​​​​​​​s​​​ ​​​​​s​​​​​o​​​​​​​m​​​​e​​t​​​​​​​h​​​​​i​​​n​​​​​g​​ ​​​​g​​​​​o​​​​​​i​​​n​​g​​​​​​
o​​​​n​​​​ ​​​​a​​​​​n​​​​​d​​​​ ​​​​​​w​​​​​i​​​l​​​​​l​​​​​ ​​​​​​a​​​​t​​​​t​​​​​​r​a​​​​​c​​​​t​​​​​ ​​​a​​t​​​​​t​​​e​​​​n​​​​t​​​i​​​​​o​​n​​.​​​​​ ​​​​F​​​​​​o​​​n​​t​​​​​ ​​​​​c​​​​​h​​​​a​​​​​n​​​​​g​​​​e​​​​s​​​​​ ​​​​​​w​​​​​​i​​​​​t​​​​​h​​​ ​​e​​​​​​n​​​​​o​​​​​u​​​​g​​​​h​​​​​ ​​​​​r​​​​​e​g​​​​u​​​​l​​​​​a​​​r​​​i​​​​​t​​​y​​​​​​
t​​​​o​​​ ​​​​e​​​​​n​​​​​c​​​​o​​​d​​​​​​e​​​​​​​ ​​​​m​​​e​​​​​​s​​​​​a​​​​​g​​e​​s​​​​​ ​​​​​r​​​​​e​​​​l​​​​​a​​​​​t​​​​i​​​v​​​​​​e​l​​​​​y​​​​ ​​e​​​f​​​f​​​​i​​​​c​​​i​​​e​​​​n​​​​t​​​​​l​​​​​y​​​​​ ​​​​w​​​​​o​​u​​​​​l​​​​​d​​​​​​ ​n​​​​​e​​​​​​​e​​​d​​​ ​​t​​o​​​​ ​​​​h​​​​​​a​​​​​p​​​​p​​​​e​​​n​​​ ​​​​​​​s​​​​​​o​​​​​​
o​f​​​​​​t​​​e​​​​​​n​​​​ ​​​​​i​​​​​t​​​ ​​​​w​​​​o​​​u​​​​​l​​​​​d​​​​​​ ​​​b​​​e​​ ​​​​o​​bvious that there was some kind of manipulation
going on. Steganographic encryption where it is obvious that there
has been some tampering with the carrier is not going to be very
effective. Not that any of these implementations would hold up to
serious scrutiny anyway. Anyway, here's a semi-bogus implementation
that hides information in white space. The message is hidden in this
paragraph of text. Yes, really. It requires a fairly modern file
viewer to display (not display?) the hidden message, but that isn't
too unlikely any more. It may be stretching things to call this a
Bacon cipher, but I think it falls within the spirit of the task,
if not the exact definition.
**********************************************************************
Hidden message revealed:
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 anything
signify anything). This example will work with anything in the
ASCII range... even code! $r%_-^&*(){}+~ #=`/\';*1234567890"'

Python

This deviates from the Bacon method as it encodes to different capitalisation of text rather than differences in font.

<lang python>import string

sometext = """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.""".lower()

lc2bin = {ch: '{:05b}'.format(i)

         for i, ch in enumerate(string.ascii_lowercase + ' .')}

bin2lc = {val: key for key, val in lc2bin.items()}

phrase = 'Rosetta code Bacon cipher example secret phrase to encode in the capitalisation of peter pan'.lower()

def to_5binary(msg):

   return ( ch == '1' for ch in .join(lc2bin.get(ch, ) for ch in msg.lower()))

def encrypt(message, text):

   bin5 = to_5binary(message)
   textlist = list(text.lower())
   out = []
   for capitalise in bin5:
       while textlist:
           ch = textlist.pop(0)
           if ch.isalpha():
               if capitalise:
                   ch = ch.upper()
               out.append(ch)
               break
           else:
               out.append(ch)
       else:
           raise Exception('ERROR: Ran out of characters in sometext')
   return .join(out) + '...'


def decrypt(bacontext):

   binary = []
   bin5 = []
   out = []
   for ch in bacontext:
       if ch.isalpha():
           binary.append('1' if ch.isupper() else '0')
           if len(binary) == 5:
               bin5 = .join(binary)
               out.append(bin2lc[bin5])
               binary = []
   return .join(out)
               

print('PLAINTEXT = \n%s\n' % phrase) encrypted = encrypt(phrase, sometext) print('ENCRYPTED = \n%s\n' % encrypted) decrypted = decrypt(encrypted) print('DECRYPTED = \n%s\n' % decrypted) assert phrase == decrypted, 'Round-tripping error'</lang>

Output:
PLAINTEXT = 
rosetta code bacon cipher example secret phrase to encode in the capitalisation 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 capitalisation of peter pan

Racket

<lang racket>#lang racket (require xml)

(define (char->bacon-number C)

 (define c (char-downcase C))
 (define c-code (- (char->integer c) (char->integer #\a)))
 (and (<= 0 c-code 26) (- c-code (if (> c-code  8) 1 0) (if (> c-code 20) 1 0))))

(define (inr-encode bacons f-cs seg/r rv/r last-bacon-bit fce)

 (cond
   [(null? bacons) (append (reverse (if (null? seg/r) rv/r (cons seg/r rv/r))) (list f-cs))]    
   [(null? f-cs) (error 'bacon-encode->list "not enough false message to hide the text")]    
   [(zero? last-bacon-bit) (inr-encode (cdr bacons) f-cs seg/r rv/r 5 fce)]    
   [(not (char-alphabetic? (car f-cs)))
    (inr-encode bacons (cdr f-cs) (cons (car f-cs) seg/r) rv/r last-bacon-bit fce)]    
   [else
    (define bit (sub1 last-bacon-bit))
    (define bs? (bitwise-bit-set? (car bacons) bit))
    (match-define (cons f-a f-d) f-cs)
    (match (cons bs? fce)
      [(or '(#f . 1) '(#t . 2)) (inr-encode bacons f-d (cons f-a seg/r) rv/r bit fce)]
      [_ (inr-encode bacons f-d (list f-a) (cons (reverse seg/r) rv/r) bit (if bs? 2 1))])]))

(define (bacon-encode->segments-list plain-text false-message)

 (define bacon-numbers (filter-map char->bacon-number (string->list plain-text)))
 (map list->string (inr-encode bacon-numbers (string->list false-message) null null 5 1)))

(define (bacon-encode->html plain-text false-message

                           (->face1 (λ (s) `(span ((face "1")) ,s)))
                           (->face2 (λ (s) `(span ((face "2")) ,s))))
 (define segments (bacon-encode->segments-list plain-text false-message))
 (xexpr->string
  (list* 'div '((style "white-space: pre"))
         (for/list ((seg (in-list segments)) (face (in-cycle (in-list (list ->face1 ->face2)))))
           (face seg)))))

(module+ main

 (define plain-text "i wrote this F.B.")
 (define false-text #<<EOS

To be, or not to be, that is the question: Whether 'tis Nobler in the mind to suffer The Slings and Arrows of outrageous Fortune, [...] EOS

   )
 
 (displayln (bacon-encode->html plain-text false-text values (λ (s) `(i ,s)))))</lang>
Output:
  • Literal
<div style="white-space: pre">T<i>o </i>be, o<i>r </i>n<i>o</i>t t<i>o </i>be, tha<i>t i</i>s <i>th</i>e q<i>u</i>est<i>i</i>on:
<i>W</i>he<i>t</i>her '<i>tis </i>N<i>o</i>ble<i>r </i>in t<i>h</i>e m<i>i</i>n<i>d </i>to su<i>f</i>fer
The Slings and Arrows of outrageous Fortune,
[...]</div>
  • HTML
To be, or not to be, that is the question:

Whether 'tis Nobler in the mind to suffer The Slings and Arrows of outrageous Fortune,

[...]

REXX

assigned cipher codes

This REXX version supports a full (26-letter) Roman (Latin) alphabet, plus a few punctuation symbols:
      .   (period),     ,   (comma),     ?   (question mark),     :   (colon),     !   (exclamation mark),   and blanks.

All alphabetic letters are handled as if they were in uppercase   (i.e., lowercase letters are uppercased). <lang rexx>/*REXX program implements and demonstrates a (full) "Bacon" cipher (cypher).*/ parse arg plain /*obtain optional arguments from the CL*/ if plain= then plain = "The quick brown fox jumped over the lazy dog."

                                      /* [↓]  code supports complete alphabet*/

@.=; @.a=11111; @.b=11110; @.c=11101; @.d=11100; @.e=11011; @.f=11010; @.g=11001

    @.h=11000; @.i=10111; @.j=00111; @.k=10110; @.l=10101; @.m=10100; @.n=10011
    @.o=10010; @.p=10001; @.q=10000; @.r=01111; @.s=01110; @.t=01101; @.u=01100
    @.v=00100; @.w=01011; @.x=01010; @.y=01001; @.z=01000; @.?=00000; @.!=00101
    @..=00110;   _=','  ; @._=00001;   _=' '  ; @._=00011;   _=':'  ; @._=00010
                                      /* [↑]  code supports some punctuation.*/

say ' plain text: ' plain /*display the original (plain) text. */

     encoded=BaconEnc(plain)          /*encode using a (full)  Bacon  cipher.*/

say 'cipher text: ' encoded /*display the ciphered (coded) text. */

     decoded=BaconDec(encoded)        /*decode ciphered text──►plain (almost)*/

say 'cycled text: ' decoded /*display the recycled text (~ plain),*/ exit /*stick a fork in it, we're all done. */ /*────────────────────────────────────────────────────────────────────────────*/ BaconEnc: procedure expose @.; arg x; $=; Lx=length(x)

           do j=1  for Lx;           _=substr(x,j,1);      $=$ || @._;      end
         return $

/*────────────────────────────────────────────────────────────────────────────*/ BaconDec: procedure expose @.; parse arg x; $=; Lx=length(x)

           do k=0 for 256; _=d2c(k); if @._== then iterate; q=@._; !.q=_; end
           do j=1  to Lx  by 5;      y=substr(x,j,5);      $=$ || !.y;      end
         return $</lang>

output   when using the default input:

 plain text:  The quick brown fox jumped over the lazy dog.
cipher text:  011011100011011000111000001100101111110110110000111111001111100100101110011000111101010010010100001100111011001010010001110111110000011100100010011011011110001101101110001101100011101011111101000010010001111100100101100100110
cycled text:  THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.

generated cipher codes

The twp glyphs (characters) chosen for this REXX program are:

  •   the   bottom tee       (sometimes known as the   bottom junction)
  •   the     top tee           (sometimes known as the   top junction)

<lang rexx>/*REXX program implements and demonstrates a (full) "Bacon" cipher (cypher).*/ parse arg plain /*obtain optional arguments from the CL*/ if plain= then plain = "The quick brown fox jumped over the lazy dog."

                                      /*alphabet must be in uppercase letters*/

alphabet= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!:' /*list of letters & punctuation.*/ @.= /*assign a default for all chars*/

    do j=0  for min(32,length(alphabet));              _=substr(alphabet,j+1,1)
    @._=translate(right(x2b(d2x(j)), 5, 0),  '┴┬', 01)
    end   /*j*/                       /* [↑]  build the symbol table (max=32)*/
                                      /* [↑]  code supports some punctuation.*/

say ' plain text: ' plain /*display the original (plain) text. */

     encoded=BaconEnc(plain)          /*encode using a (full)  Bacon  cipher.*/

say 'cipher text: ' encoded /*display the ciphered (coded) text. */

     decoded=BaconDec(encoded)        /*decode ciphered text──►plain (almost)*/

say 'cycled text: ' decoded /*display the recycled text (~ plain),*/ exit /*stick a fork in it, we're all done. */ /*────────────────────────────────────────────────────────────────────────────*/ BaconEnc: procedure expose @.; arg x; $=; Lx=length(x)

           do j=1  for Lx;           _=substr(x,j,1);      $=$ || @._;      end
         return $

/*────────────────────────────────────────────────────────────────────────────*/ BaconDec: procedure expose @.; parse arg x; $=; Lx=length(x)

           do k=0 for 256; _=d2c(k); if @._== then iterate; q=@._; !.q=_; end
           do j=1  to Lx  by 5;      y=substr(x,j,5);      $=$ || !.y;      end
         return $</lang>

output   when using the default input:

 plain text:  The quick brown fox jumped over the lazy dog.
cipher text:  ┬┴┴┬┬┴┴┬┬┬┴┴┬┴┴┬┬┴┬┴┬┴┴┴┴┬┴┬┴┴┴┬┴┴┴┴┴┴┬┴┴┬┴┬┴┬┬┴┬┴┴┴┴┴┬┬┴┴┴┬┴┬┬┬┴┬┴┬┬┴┴┬┬┴┬┬┬┴┬┴┴┴┬┴┬┴┬┬┬┴┬┴┬┬┬┬┬┴┬┴┴┬┴┴┬┬┴┬┴┴┴┬┬┴┴┴┬┬┬┬┴┴┬┴┴┴┴┴┬┬┬┬┴┬┴┴┬┬┬┴┬┴┬┴┬┴┴┬┴┴┬┴┴┴┬┬┬┴┬┴┬┴┴┬┬┴┴┬┬┬┴┴┬┴┴┬┬┴┬┴┴┬┴┬┬┴┴┴┴┴┬┬┴┴┬┬┬┴┴┴┬┬┴┬┴┴┴┴┬┬┴┬┬┬┴┴┴┬┬┴┬┬┴┬┬
cycled text:  THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.

uses upper/lower case

<lang rexx>/*REXX program implements and demonstrates a (full) "Bacon" cipher (cypher).*/ parse arg plain /*obtain optional arguments from the CL*/ if plain= then plain = "The quick brown fox jumped over the lazy dog."

                                      /*alphabet must be in uppercase letters*/

alphabet= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!:' /*list of letters & punctuation.*/ @.= /*assign a default for all chars*/

    do j=0  for min(32,length(alphabet));              _=substr(alphabet,j+1,1)
    @._=translate(right(x2b(d2x(j)), 5, 0),  'sS', 01)
    end   /*j*/                       /* [↑]  build the symbol table (max=32)*/
                                      /* [↑]  code supports some punctuation.*/

say ' plain text: ' plain /*display the original (plain) text. */

     encoded=BaconEnc(plain)          /*encode using a (full)  Bacon  cipher.*/

say 'cipher text: ' encoded /*display the ciphered (coded) text. */

     decoded=BaconDec(encoded)        /*decode ciphered text──►plain (almost)*/

say 'cycled text: ' decoded /*display the recycled text (~ plain),*/ exit /*stick a fork in it, we're all done. */ /*────────────────────────────────────────────────────────────────────────────*/ BaconEnc: procedure expose @.; arg x; $=; Lx=length(x)

           do j=1  for Lx;           _=substr(x,j,1);      $=$ || @._;      end
         return $

/*────────────────────────────────────────────────────────────────────────────*/ BaconDec: procedure expose @.; parse arg x; $=; Lx=length(x)

           do k=0 for 256; _=d2c(k); if @._== then iterate; q=@._; !.q=_; end
           do j=1  to Lx  by 5;      y=substr(x,j,5);      $=$ || !.y;      end
         return $</lang>

output   when using the default input:

 plain text:  The quick brown fox jumped over the lazy dog.
cipher text:  SssSSssSSSssSssSSsSsSssssSsSsssSssssssSssSsSsSSsSsssssSSsssSsSSSsSsSSssSSsSSSsSsssSsSsSSSsSsSSSSSsSssSssSSsSsssSSsssSSSSssSsssssSSSSsSssSSSsSsSsSssSssSsssSSSsSsSssSSssSSSssSssSSsSssSsSSsssssSSssSSSsssSSsSssssSSsSSSsssSSsSSsSS
cycled text:  THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.

zkl

Translation of: Python

<lang zkl>class Bacon{

  fcn init(_keyText){
     var [const] keyText=_keyText.toLower(),
       lowerLetters=["a".."z"].pump(String), 

upperLetters=["A".."Z"].pump(String), 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(),"reverse"); //-->("00000":"a", ...),

     ;
  }
  fcn to5binary(msg){ //-->stream of 1s and 0s (1 means capitalize)
     msg.toLower().pump(String,lc2bin.get.fp1(""))  //-->("abcde"|"") * length
     .pump(Data,"toAsc",'-(0x30)).howza(0)  // "1"-->1, treat result as bytes
  }
  fcn encrypt(msg){
     bin5:=to5binary(msg).walker();  // capitalization overlay of keyText
     e:=keyText.pump(String, 'wrap(ch){

if(not lowerLetters.holds(ch)) return(Void.Write); // encrypt only ASCII if(not bin5._next()) return(Void.Stop); // end of msg (bin5.value and ch.toUpper() or ch);

     });
     if(not bin5.atEnd) throw(Exception.ValueError("Ran out of characters in key text"));
     e + "...."  // 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(

  1. <<<

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.| );

  1. <<<

phrase:="Rosetta code Bacon cipher example secret phrase to encode in the capitalization of peter pan";

println("PLAINTEXT = \n%s".fmt(phrase)); encrypted,decrypted:=bacon.encrypt(phrase), bacon.decrypt(encrypted); println("ENCRYPTED = \n%s".fmt(encrypted)); println("DECRYPTED = \n%s".fmt(decrypted)); if(phrase.toLower()!=decrypted) throw(Exception.AssertionError("Round-tripping error"));</lang>

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