Bacon cipher: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 134: Line 134:
h2 = c2.encode( s );
h2 = c2.encode( s );


std::cout << h1 << std::endl << std::endl << h2 << std::endl << std::endl;
std::cout << h1 << std::endl << std::endl << c1.decode( h1 ) << std::endl << std::endl;
std::cout << c1.decode( h1 ) << std::endl << std::endl << c2.decode( h2 ) << std::endl << std::endl;
std::cout << h2 << std::endl << std::endl << c2.decode( h2 ) << std::endl << std::endl;


return 0;
return 0;
Line 145: Line 145:
foRMeD In hIs Bed iNto a HorRiblE VErmin. He lay on hiS arMOuR-lIKe back, And If
foRMeD In hIs Bed iNto a HorRiblE VErmin. He lay on hiS arMOuR-lIKe back, And If
he lIFTed hIS HeaD a lIttle
he lIFTed hIS HeaD a lIttle

LETSHAUESOMEFUNWITHBACONCIPHER


¨·¨·¨¨¨·¨¨·¨¨·¨·¨¨¨·¨¨···¨¨¨¨¨·¨¨··¨¨·¨¨·¨¨¨·¨··¨·¨·¨··¨¨·¨¨¨¨·¨··¨¨··¨··¨¨·¨·¨¨
¨·¨·¨¨¨·¨¨·¨¨·¨·¨¨¨·¨¨···¨¨¨¨¨·¨¨··¨¨·¨¨·¨¨¨·¨··¨·¨·¨··¨¨·¨¨¨¨·¨··¨¨··¨··¨¨·¨·¨¨
Line 150: Line 152:


LETSHAUESOMEFUNWITHBACONCIPHER
LETSHAUESOMEFUNWITHBACONCIPHER

LETSHAUESOMEFUNWITHBACONCIPHER

</pre>
</pre>



=={{header|J}}==
=={{header|J}}==

Revision as of 17:11, 5 September 2015

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

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

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>