Bacon cipher: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Haskell}}: Applied hlint, hindent, inlined comments, added main.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 15:
# 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.
# Show an example plaintext message encoded and then decoded here on this page.
 
 
=={{header|Agena}}==
Line 409 ⟶ 408:
the quick brown fox jumps over the lazy dog
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace BaconCipher {
class Program {
private static Dictionary<char, string> codes = new Dictionary<char, string> {
{'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" }, // use ' ' to denote any non-letter
};
 
private static string Encode(string plainText, string message) {
string pt = plainText.ToLower();
StringBuilder sb = new StringBuilder();
foreach (char c in pt) {
if ('a' <= c && c <= 'z') sb.Append(codes[c]);
else sb.Append(codes[' ']);
}
string et = sb.ToString();
string mg = message.ToLower(); // 'A's to be in lower case, 'B's in upper case
sb.Length = 0;
int count = 0;
foreach (char c in mg) {
if ('a' <= c && c <= 'z') {
if (et[count] == 'A') sb.Append(c);
else sb.Append((char)(c - 32)); // upper case equivalent
count++;
if (count == et.Length) break;
}
else sb.Append(c);
}
 
return sb.ToString();
}
 
private static string Decode(string message) {
StringBuilder sb = new StringBuilder();
foreach (char c in message) {
if ('a' <= c && c <= 'z') sb.Append('A');
else if ('A' <= c && c <= 'Z') sb.Append('B');
}
string et = sb.ToString();
sb.Length = 0;
for (int i = 0; i < et.Length; i += 5) {
string quintet = et.Substring(i, 5);
char key = codes.Where(a => a.Value == quintet).First().Key;
sb.Append(key);
}
return sb.ToString();
}
 
static void Main(string[] args) {
string plainText = "the quick brown fox jumps over the lazy dog";
string 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.";
string cipherText = Encode(plainText, message);
Console.WriteLine("Cipher text ->\n{0}", cipherText);
string decodedText = Decode(cipherText);
Console.WriteLine("\nHidden text ->\n{0}", decodedText);
}
}
}</lang>
{{out}}
<pre>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</pre>
 
=={{header|C++}}==
Line 551 ⟶ 632:
LETSHAUESOMEFUNWITHBACONCIPHER
</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace BaconCipher {
class Program {
private static Dictionary<char, string> codes = new Dictionary<char, string> {
{'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" }, // use ' ' to denote any non-letter
};
 
private static string Encode(string plainText, string message) {
string pt = plainText.ToLower();
StringBuilder sb = new StringBuilder();
foreach (char c in pt) {
if ('a' <= c && c <= 'z') sb.Append(codes[c]);
else sb.Append(codes[' ']);
}
string et = sb.ToString();
string mg = message.ToLower(); // 'A's to be in lower case, 'B's in upper case
sb.Length = 0;
int count = 0;
foreach (char c in mg) {
if ('a' <= c && c <= 'z') {
if (et[count] == 'A') sb.Append(c);
else sb.Append((char)(c - 32)); // upper case equivalent
count++;
if (count == et.Length) break;
}
else sb.Append(c);
}
 
return sb.ToString();
}
 
private static string Decode(string message) {
StringBuilder sb = new StringBuilder();
foreach (char c in message) {
if ('a' <= c && c <= 'z') sb.Append('A');
else if ('A' <= c && c <= 'Z') sb.Append('B');
}
string et = sb.ToString();
sb.Length = 0;
for (int i = 0; i < et.Length; i += 5) {
string quintet = et.Substring(i, 5);
char key = codes.Where(a => a.Value == quintet).First().Key;
sb.Append(key);
}
return sb.ToString();
}
 
static void Main(string[] args) {
string plainText = "the quick brown fox jumps over the lazy dog";
string 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.";
string cipherText = Encode(plainText, message);
Console.WriteLine("Cipher text ->\n{0}", cipherText);
string decodedText = Decode(cipherText);
Console.WriteLine("\nHidden text ->\n{0}", decodedText);
}
}
}</lang>
{{out}}
<pre>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</pre>
 
=={{header|D}}==
Line 1,502 ⟶ 1,501:
<pre>cOMpuTErs aRE eVErywHErE. THErE aRE ThE OBVIOuS OnEs, Like YOUR SMarT PHoNe or GaMe sYSteM. therE ARE lEsS obvIoUs ones, lIKE iN YouR mICRowAVE or youR BeDsidE CLock. AnD tHeN ThERe ARE the rEAlLY inVIsibLE oNEs, liKE ThE 50 or SO SMAll COmpuTERs in YOUR Car.All tHESe cO
the quick brown fox jumps over the lazy dog</pre>
 
 
=={{header|Perl}}==
Line 1,596 ⟶ 1,594:
ASCII range... even code! $r%_-^&*(){}+~ #=`/\';*1234567890"'
</pre>
 
=={{header|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|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
#'
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>
 
{{out}}
<pre>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"'</pre>
 
=={{header|Phix}}==
Line 1,924 ⟶ 1,839:
The Slings and Arrows of outrageous Fortune,
[...]</div>
 
=={{header|Raku}}==
(formerly 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|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
#'
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>
 
{{out}}
<pre>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"'</pre>
 
=={{header|REXX}}==
10,327

edits