Bacon cipher: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 22:
{{trans|Nim}}
 
<langsyntaxhighlight lang=11l>V codes = [‘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’,
Line 72:
V decodedtext = decode(ciphertext)
print("\nHidden text →")
print(decodedtext)</langsyntaxhighlight>
 
{{out}}
Line 85:
=={{header|Ada}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=Ada>-- Bacon cipher
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 234:
Put_Line (decode (ciphertext));
end;
end Main;</langsyntaxhighlight>
{{output}}
<pre>
Line 247:
Tested with Agena 2.9.5 Win32
{{Trans|ALGOL 68}}
<langsyntaxhighlight 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
Line 337:
print( "-----------------------------------------------------" );
print( if baconDecoded <> plainText then "UNSUCESSFUL" else "sucessful" fi, " decode" )
epocs</langsyntaxhighlight>
{{out}}
<pre>
Line 353:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight 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"
Line 440:
print( ( bacon decoded, newline ) );
print( ( "-----------------------------------------------------", newline ) );
print( ( IF bacon decoded /= plain text THEN "UNSUCESSFUL" ELSE "sucessful" FI, " decode", newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 457:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>Codes: #[
a: "AAAAA" b: "AAAAB" c: "AAABA" d: "AAABB" e: "AABAA"
f: "AABAB" g: "AABBA" h: "AABBB" i: "ABAAA" j: "ABAAB"
Line 522:
print ""
print "Message:"
print decodeBacon cipherText</langsyntaxhighlight>
 
{{out}}
Line 533:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Bacon_Cipher(message, plaintext:=""){
codes := {"a":"AAAAA", "b":"AAAAB", "c":"AAABA", "d":"AAABB", "e":"AABAA"
, "f":"AABAB", "g":"AABBA", "h":"AABBB", "i":"ABAAA", "j":"ABAAB"
Line 573:
return str
}
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight lang=AutoHotkey>Message:= "
(join
bacon's cipher is a method of steganography created by francis bacon.
Line 589:
MsgBox, 262144, ,% "plain text = " plaintext
. "`n`nencoded = `n" (cipher := Bacon_Cipher(message, plaintext))
. "`n`ndecoded = " recoveredText := Bacon_Cipher(cipher)</langsyntaxhighlight>
{{out}}
<pre>plain text = the quick brown fox jumps over the lazy dog
Line 601:
=={{header|BaCon}}==
A Bacon cipher in [[BaCon]]. Using unique identifiers 'aaaaa'-'bbaab' for a-z and, as other examples on this page, using 'bbbaa' (28) for the space character.
<langsyntaxhighlight lang=qbasic>msg$ = "the quick brown fox jumps over the lazy dog"
 
txt$ = "Bacon's cipher is a method of steganography created by Francis Bacon." \
Line 650:
NEXT
 
PRINT "Decoded:", NL$, result$</langsyntaxhighlight>
{{out}}
<pre>Encoded:
Line 659:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 768:
free(hidden_text);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 783:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Linq;
Line 855:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 866:
Bacon cipher implementation
 
<langsyntaxhighlight lang=cpp>
#include <iostream>
#include <algorithm>
Line 914:
std::vector<std::string> bAlphabet;
};
</syntaxhighlight>
</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...
<langsyntaxhighlight lang=cpp>
class cipherI {
public:
Line 990:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,021:
2. Program
 
<langsyntaxhighlight lang=lisp>;; 22.06.16
 
(defconstant +codes+
Line 1,058:
(setf key (append key (list letter)))
(setf code nil)))
(coerce key 'string)))</langsyntaxhighlight>
 
3. Example
 
<langsyntaxhighlight lang=lisp>(defconstant +monologue+ (concatenate 'string
"I've known adventures, seen places you people will never see, I've been Offw"
"orld and back... frontiers ! I've stood on the back deck of a blinker bound "
Line 1,070:
"en it, felt it..."))
 
(defconstant +key+ "« tears in rain »")</langsyntaxhighlight>
 
4. Execution
Line 1,086:
 
=={{header|D}}==
<langsyntaxhighlight lang=d>import std.array;
import std.stdio;
import std.uni;
Line 1,175:
writeln("Hidden text ->");
writeln(decodedText);
}</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 1,193:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=go>package main
 
import(
Line 1,275:
decodedText := baconDecode(cipherText)
fmt.Printf("\nHidden text ->\n\n%s\n", decodedText)
}</langsyntaxhighlight>
 
{{out}}
Line 1,290:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>class BaconCipher {
private static final Map<Character, String> codes
 
Line 1,377:
System.out.printf("\nHidden text ->\n\n%s\n", decodedText)
}
}</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 1,389:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang=haskell>-- Necessary imports
import Data.List (elemIndex, unfoldr)
import Data.Bool (bool)
Line 1,461:
, encode text "something wrong @ in the message"
, encode "abc" message
]</langsyntaxhighlight>
{{Out}}
<pre>BAcoN's CIPher is A metHod Of StegaNogrApHy creAted By franCiS
Line 1,480:
Implementation:
 
<langsyntaxhighlight lang=J>alfa=: 'ABCDEFGHIKLMNOPQRSTUWXYZ'
beta=: 26{.(}.~i.&'A')a.
norm=: ([ -. -.)&alfa@(rplc&('JIVU'))@toupper
Line 1,487:
 
encrypt=: gena@enca@norm
decrypt=: alfa {~ _5 #.\ 90 < a.&i.</langsyntaxhighlight>
 
We use random letters as the basis for our steganography and we use case to represent "font".
Line 1,493:
Example use:
 
<langsyntaxhighlight lang=J> encrypt 'this is a test'
nWVkJAPkamEuUJIeTGKnUsTVRfAWWuNBIIHdEIcOAPuTBeXKQduQAdU
encrypt 'this is a test'
sFLkBQKqqaQsGGXzAXQsKlZFBcILRlUIRAQaEQoNUBcHIhFTWbeRAlM
decrypt encrypt 'this is a test'
THISISATEST</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight lang=Java>import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Line 1,578:
System.out.printf("\nHidden text ->\n\n%s\n", decodedText);
}
}</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 1,595:
 
'''Preliminaries'''
<langsyntaxhighlight lang=jq>def is_upper: . >= "A" and . <= "Z";
 
def is_lower: . >= "a" and . <= "z";
Line 1,604:
# (*) Change to `keys` for gojq
def key($s): first( keys_unsorted[] as $k | if .[$k] == $s then $k else empty end) // "?";
</syntaxhighlight>
</lang>
'''Bacon Cipher'''
<langsyntaxhighlight lang=jq>def Bacon:
{
"a" : "AAAAA", "b" : "AAAAB", "c" : "AAABA", "d" : "AAABB", "e" : "AABAA",
Line 1,654:
| .decodedText = decode(.cipherText)
| "Cipher text ->\n\n\(.cipherText)",
"\nHidden text ->\n\n\(.decodedText)"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,672:
 
'''Module''':
<langsyntaxhighlight lang=julia>module BaconCipher
 
using Formatting, IterTools.chain
Line 1,737:
end
 
end # module BaconCipher</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang=julia>let msg = "Rosetta code Bacon cipher example secret phrase to encode in the capitalisation of peter pan"
enc = BaconCipher.encrypt(msg)
dec = BaconCipher.decrypt(enc)
Line 1,746:
println(" -> Encrypted:\n", enc)
println(" -> Decrypted:\n", dec)
end</langsyntaxhighlight>
 
{{out}}
Line 1,768:
=={{header|Kotlin}}==
The 'full' Bacon alphabet, which has separate letters for i, j, u and v, has been used in the following:
<langsyntaxhighlight lang=scala>object Bacon {
private val codes = mapOf(
'a' to "AAAAA", 'b' to "AAAAB", 'c' to "AAABA", 'd' to "AAABB", 'e' to "AABAA",
Line 1,829:
val decodedText = Bacon.decode(cipherText)
println("\nHidden text ->\n\n$decodedText")
}</langsyntaxhighlight>
 
{{out}}
Line 1,844:
=={{header|Lua}}==
Based on C++ version
<langsyntaxhighlight lang=Lua>
function Bacon( txt, secret, e )
local alpha = {}
Line 1,954:
print( a )
print( Bacon( "", a, 0 ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,963:
 
=={{header|MiniScript}}==
<langsyntaxhighlight lang=MiniScript>c = {}
c["a"] = "AAAAA"; c["b"] = "AAAAB"; c["c"] = "AAABA"; c["d"] = "AAABB"; c["e"] = "AABAA"; c["f"] = "AABAB";
c["g"] = "AABBA"; c["h"] = "AABBB"; c["i"] = "ABAAA"; c["j"] = "ABAAB"; c["k"] = "ABABA"; c["l"] = "ABABB";
Line 2,026:
codedMsg = encode("the quick brown fox jumps over the lazy dog")
print codedMsg
print decode(codedMsg)</langsyntaxhighlight>
 
{{out}}
Line 2,034:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=Nim>import strutils, sugar, tables
 
const Codes = {'a': "AAAAA", 'b': "AAAAB", 'c': "AAABA", 'd': "AAABB", 'e': "AABAA",
Line 2,081:
echo "Cipher text →\n", cipherText
let decodedText = cipherText.decode()
echo "\nHidden text →\n", decodedText</langsyntaxhighlight>
 
{{out}}
Line 2,092:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use utf8;
Line 2,156:
 
print "$steganography\n"
print "$decoded\n"</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">Steganograpic message hidden in text:
Line 2,195:
half of/an obviously corrupt image file.
 
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">mask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
Line 2,264:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Encrypted:\n"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">ant</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Decrypted:\n"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">dec</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 2,280:
This deviates from the Bacon method as it encodes to different capitalisation of text rather than differences in font.
 
<langsyntaxhighlight lang=python>import string
 
sometext = """All children, except one, grow up. They soon know that they will grow
Line 2,346:
decrypted = decrypt(encrypted)
print('DECRYPTED = \n%s\n' % decrypted)
assert phrase == decrypted, 'Round-tripping error'</langsyntaxhighlight>
 
{{out}}
Line 2,369:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ dup upper swap lower != ] is ischar ( c --> b )
 
[ char a char z 1+ clamp
Line 2,431:
dup nest$ 60 wrap$ cr cr
debaconise echo$
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,444:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
(require xml)
 
Line 2,490:
)
(displayln (bacon-encode->html plain-text false-text values (λ (s) `(i ,s)))))</langsyntaxhighlight>
 
{{out}}
Line 2,511:
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|2015-11-20}}
<syntaxhighlight lang=raku perl6lines>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
Line 2,564:
 
say "Hidden message revealed:";
say reveal $steganography;</langsyntaxhighlight>
 
{{out}}
Line 2,592:
 
=== Bacon cipher solution ===
<syntaxhighlight lang=raku perl6lines>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
Line 2,629:
say "text:\n$baconed\n";
my $unbaconed = unbacon($baconed).trim.uc;
say "hidden message:\n$unbaconed";</langsyntaxhighlight>
 
{{out}}
Line 2,654:
 
All alphabetic letters are handled as if they were in uppercase &nbsp; (i.e., lowercase letters are uppercased).
<langsyntaxhighlight 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."
Line 2,678:
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 $</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
Line 2,690:
:::* &nbsp; the &nbsp; ''bottom tee'' &nbsp; <big><big>┴</big></big> &nbsp; &nbsp; (sometimes known as the &nbsp; ''bottom junction'')
:::* &nbsp; the &nbsp; &nbsp; ''top tee'' &nbsp; &nbsp; &nbsp; <big><big>┬</big></big> &nbsp; &nbsp; (sometimes known as the &nbsp; ''top junction'')
<langsyntaxhighlight 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."
Line 2,714:
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 $</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
Line 2,723:
 
===uses upper/lower case===
<langsyntaxhighlight 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."
Line 2,747:
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 $</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
Line 2,757:
=={{header|Ruby}}==
{{trans|C#}}
<langsyntaxhighlight lang=ruby>CODES = {
'a' => "AAAAA", 'b' => "AAAAB", 'c' => "AAABA", 'd' => "AAABB", 'e' => "AABAA",
'f' => "AABAB", 'g' => "AABBA", 'h' => "AABBB", 'i' => "ABAAA", 'j' => "ABAAB",
Line 2,844:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 2,854:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang=scala>import scala.util.control.Breaks._
 
object BaconCipher {
Line 2,942:
println(s"Hidden text ->\n\n$decodedText")
}
}</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 2,953:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Imports System.Text
 
Module Module1
Line 3,041:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Cipher text ->
Line 3,051:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang=vlang>const codes = {
`a` : "AAAAA", `b` : "AAAAB", `c` : "AAABA", `d` : "AAABB", `e` : "AABAA",
`f` : "AABAB", `g` : "AABBA", `h` : "AABBB", `i` : "ABAAA", `j` : "ABAAB",
Line 3,126:
decoded_text := bacon_decode(cipher_text)
println("\nHidden text ->\n\n$decoded_text")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,141:
{{libheader|Wren-str}}
{{libheader|Wren-trait}}
<langsyntaxhighlight lang=ecmascript>import "/str" for Str, Char
import "/trait" for Stepped
 
Line 3,209:
System.print("Cipher text ->\n\n%(cipherText)")
var decodedText = Bacon.decode(cipherText)
System.print("\nHidden text ->\n\n%(decodedText)")</langsyntaxhighlight>
 
{{out}}
Line 3,224:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang=zkl>class Bacon{
fcn init(_keyText){
var [const] keyText=_keyText.toLower(),
Line 3,253:
fcn{ vm.arglist.pump(String,upperLetters.holds,"toInt") : bin2lc[_] });
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>bacon:=Bacon(
#<<<
0'|All children, except one, grow up. They soon know that they will grow
Line 3,281:
println("ENCRYPTED = \n%s".fmt(encrypted));
println("DECRYPTED = \n%s".fmt(decrypted));
if(phrase.toLower()!=decrypted) throw(Exception.AssertionError("Round-tripping error"));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits