Bacon cipher: Difference between revisions
m
→{{header|Wren}}: Minor tidy
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Minor tidy) |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 1:
[[Category:Encryption]]
{{draft task}}
[[wp: Bacon's cipher| Bacon's cipher]] is a method of steganography created by Francis Bacon.
Line 22:
{{trans|Nim}}
<syntaxhighlight 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 85:
=={{header|Ada}}==
{{trans|Kotlin}}
<syntaxhighlight lang=
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 247:
Tested with Agena 2.9.5 Win32
{{Trans|ALGOL 68}}
<syntaxhighlight 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 353:
=={{header|ALGOL 68}}==
<syntaxhighlight 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 457:
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">Codes: #[
a: "AAAAA" b: "AAAAB" c: "AAABA" d: "AAABB" e: "AABAA"
f: "AABAB" g: "AABBA" h: "AABBB" i: "ABAAA" j: "ABAAB"
Line 533:
=={{header|AutoHotkey}}==
<syntaxhighlight lang=
codes := {"a":"AAAAA", "b":"AAAAB", "c":"AAABA", "d":"AAABB", "e":"AABAA"
, "f":"AABAB", "g":"AABBA", "h":"AABBB", "i":"ABAAA", "j":"ABAAB"
Line 574:
}
}</syntaxhighlight>
Examples:<syntaxhighlight lang=
(join
bacon's cipher is a method of steganography created by francis bacon.
Line 599:
</pre>
=={{header|
==={{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.
<syntaxhighlight 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 655 ⟶ 656:
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</pre>
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Text
Module Module1
ReadOnly CODES As New Dictionary(Of Char, String) From {
{"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
}
Function Encode(plainText As String, message As String) As String
Dim pt = plainText.ToLower()
Dim sb As New StringBuilder()
For Each c In pt
If "a" <= c AndAlso c <= "z" Then
sb.Append(CODES(c))
Else
sb.Append(CODES(" "))
End If
Next
Dim et = sb.ToString()
Dim mg = message.ToLower() '"A"s to be in lower case, "B"s in upper case
sb.Length = 0
Dim count = 0
For Each c In mg
If "a" <= c AndAlso c <= "z" Then
If et(count) = "A" Then
sb.Append(c)
Else
sb.Append(Chr(Asc(c) - 32)) ' upper case equivalent
End If
count += 1
If count = et.Length Then
Exit For
End If
Else
sb.Append(c)
End If
Next
Return sb.ToString()
End Function
Function Decode(message As String) As String
Dim sb As New StringBuilder
For Each c In message
If "a" <= c AndAlso c <= "z" Then
sb.Append("A")
ElseIf "A" <= c AndAlso c <= "Z" Then
sb.Append("B")
End If
Next
Dim et = sb.ToString()
sb.Length = 0
For index = 0 To et.Length - 1 Step 5
Dim quintet = et.Substring(index, 5)
Dim key = CODES.Where(Function(a) a.Value = quintet).First().Key
sb.Append(key)
Next
Return sb.ToString()
End Function
Sub Main()
Dim plainText = "the quick brown fox jumps over the lazy dog"
Dim 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."
Dim cipherText = Encode(plainText, message)
Console.WriteLine("Cipher text ->" & Environment.NewLine & "{0}", cipherText)
Dim decodedText = Decode(cipherText)
Console.WriteLine(Environment.NewLine & "Hidden text ->" & Environment.NewLine & "{0}", decodedText)
End Sub
End Module</syntaxhighlight>
{{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}}==
{{trans|Kotlin}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 783 ⟶ 882:
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 866 ⟶ 965:
Bacon cipher implementation
<syntaxhighlight lang="cpp">
#include <iostream>
#include <algorithm>
Line 918 ⟶ 1,017:
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...
<syntaxhighlight lang="cpp">
class cipherI {
public:
Line 1,021 ⟶ 1,120:
2. Program
<syntaxhighlight lang="lisp">;; 22.06.16
(defconstant +codes+
Line 1,062 ⟶ 1,161:
3. Example
<syntaxhighlight 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,086 ⟶ 1,185:
=={{header|D}}==
<syntaxhighlight lang="d">import std.array;
import std.stdio;
import std.uni;
Line 1,185 ⟶ 1,284:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Bacon%27s_cipher}}
'''Solution'''
'''Program for encoding.''' It uses the extended version of the alphabeth, which can be calculated programatically, with no table.
[[File:Fōrmulæ - Bacon's cipher 01.png]]
'''Test case for encoding'''
[[File:Fōrmulæ - Bacon's cipher 02.png]]
[[File:Fōrmulæ - Bacon's cipher 03.png]]
'''Program for decoding'''
[[File:Fōrmulæ - Bacon's cipher 04.png]]
'''Test case for decoding'''
[[File:Fōrmulæ - Bacon's cipher 05.png]]
[[File:Fōrmulæ - Bacon's cipher 06.png]]
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
import(
Line 1,290 ⟶ 1,407:
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class BaconCipher {
private static final Map<Character, String> codes
Line 1,389 ⟶ 1,506:
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">-- Necessary imports
import Data.List (elemIndex, unfoldr)
import Data.Bool (bool)
Line 1,480 ⟶ 1,597:
Implementation:
<syntaxhighlight lang=
beta=: 26{.(}.~i.&'A')a.
norm=: ([ -. -.)&alfa@(rplc&('JIVU'))@toupper
Line 1,493 ⟶ 1,610:
Example use:
<syntaxhighlight lang=
nWVkJAPkamEuUJIeTGKnUsTVRfAWWuNBIIHdEIcOAPuTBeXKQduQAdU
encrypt 'this is a test'
Line 1,503 ⟶ 1,620:
{{trans|Kotlin}}
{{works with|Java|9}}
<syntaxhighlight lang=
import java.util.Map;
import java.util.Objects;
Line 1,595 ⟶ 1,712:
'''Preliminaries'''
<syntaxhighlight lang="jq">def is_upper: . >= "A" and . <= "Z";
def is_lower: . >= "a" and . <= "z";
Line 1,606 ⟶ 1,723:
</syntaxhighlight>
'''Bacon Cipher'''
<syntaxhighlight lang="jq">def Bacon:
{
"a" : "AAAAA", "b" : "AAAAB", "c" : "AAABA", "d" : "AAABB", "e" : "AABAA",
Line 1,672 ⟶ 1,789:
'''Module''':
<syntaxhighlight lang="julia">module BaconCipher
using Formatting, IterTools.chain
Line 1,740 ⟶ 1,857:
'''Main''':
<syntaxhighlight 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,768 ⟶ 1,885:
=={{header|Kotlin}}==
The 'full' Bacon alphabet, which has separate letters for i, j, u and v, has been used in the following:
<syntaxhighlight 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,844 ⟶ 1,961:
=={{header|Lua}}==
Based on C++ version
<syntaxhighlight lang=
function Bacon( txt, secret, e )
local alpha = {}
Line 1,963 ⟶ 2,080:
=={{header|MiniScript}}==
<syntaxhighlight lang=
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,034 ⟶ 2,151:
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang=
const Codes = {'a': "AAAAA", 'b': "AAAAB", 'c': "AAABA", 'd': "AAABB", 'e': "AABAA",
Line 2,092 ⟶ 2,209:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use utf8;
Line 2,195 ⟶ 2,312:
half of/an obviously corrupt image file.
<!--<syntaxhighlight lang=
<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,280 ⟶ 2,397:
This deviates from the Bacon method as it encodes to different capitalisation of text rather than differences in font.
<syntaxhighlight lang="python">import string
sometext = """All children, except one, grow up. They soon know that they will grow
Line 2,369 ⟶ 2,486:
=={{header|Quackery}}==
<syntaxhighlight lang=
[ char a char z 1+ clamp
Line 2,444 ⟶ 2,561:
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(require xml)
Line 2,511 ⟶ 2,628:
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"
This task is to implement a program for encryption and decryption
of plaintext using the simple alphabet of the Baconian cipher or
Line 2,592 ⟶ 2,709:
=== Bacon cipher solution ===
<syntaxhighlight lang="raku"
my @symb = ' ', |@abc; # modified Baconian charset - space and full alphabet
# TODO original one with I=J U=V, nice for Latin
Line 2,654 ⟶ 2,771:
All alphabetic letters are handled as if they were in uppercase (i.e., lowercase letters are uppercased).
<syntaxhighlight 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,690 ⟶ 2,807:
:::* the ''bottom tee'' <big><big>┴</big></big> (sometimes known as the ''bottom junction'')
:::* the ''top tee'' <big><big>┬</big></big> (sometimes known as the ''top junction'')
<syntaxhighlight 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,723 ⟶ 2,840:
===uses upper/lower case===
<syntaxhighlight 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,757 ⟶ 2,874:
=={{header|Ruby}}==
{{trans|C#}}
<syntaxhighlight 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,854 ⟶ 2,971:
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import scala.util.control.Breaks._
object BaconCipher {
Line 2,951 ⟶ 3,068:
the quick brown fox jumps over the lazy dog</pre>
=={{header|
{{trans|go}}
<syntaxhighlight lang="v (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,140 ⟶ 3,159:
{{trans|Kotlin}}
{{libheader|Wren-str}}
{{libheader|Wren-
<syntaxhighlight lang=
import "./
class Bacon {
Line 3,211 ⟶ 3,230:
System.print("\nHidden text ->\n\n%(decodedText)")</syntaxhighlight>
{{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|XPL0}}==
{{trans|C}}
Works on Raspberry Pi. (MAlloc works differently in other versions.)
<syntaxhighlight lang "XPL0">include xpllib; \for StrCmp, StrLen, Print, ToLower, StrNCopy
int Codes;
func GetCode(C);
char C;
[if C >= 97 and C <= 122 then return Codes(C-97);
return Codes(26);
];
func GetChar(Code);
char Code;
int I;
[if StrCmp(Codes(26), Code) = 0 then return ^ ;
for I:= 0 to 26-1 do
if StrCmp(Codes(I), Code) = 0 then return 97+I;
Print("\nCode ^"%s^" is invalid\n", Code);
exit(1);
];
proc StrToLower(S);
char S;
int I;
for I:= 0 to StrLen(S)-1 do S(I):= ToLower(S(I));
func BaconEncode(PlainText, Message);
char PlainText, Message;
int I, Count;
int PLen, MLen, ELen;
char C, P, Et, Mt;
[PLen:= StrLen(PlainText);
MLen:= StrLen(Message);
ELen:= 5 * PLen;
Et:= MAlloc(ELen+1);
StrToLower(PlainText);
P:= Et;
for I:= 0 to PLen-1 do
[C:= PlainText(I);
StrNCopy(P, GetCode(C), 5);
P:= P+5;
];
P:= P+1; P(0):= 0;
\'A's to be in lowercase, 'B's in uppercase
StrToLower(Message);
Mt:= MAlloc(MLen+1);
Count:= 0;
for I:= 0 to MLen-1 do
[C:= Message(I);
if C >= ^a and C <= ^z then
[if Et(Count) = ^A then Mt(I):= C
else Mt(I):= C-32; \uppercase equivalent
Count:= Count+1;
if Count = ELen then I:= MLen;
]
else Mt(I):= C;
];
Release(Et);
return Mt;
];
func BaconDecode(CipherText);
char CipherText;
int I, Count, CLen, PLen;
char P, Ct, Pt, C, Quintet(6);
[CLen:= StrLen(CipherText);
Ct:= MAlloc(CLen+1);
Count:= 0;
for I:= 0 to CLen-1 do
[C:= CipherText(I);
if C >= ^a and C <= ^z then
[Ct(Count):= ^A; Count:= Count+1]
else if C >= ^A and C <= ^Z then
[Ct(Count):= ^B; Count:= Count+1];
];
PLen:= StrLen(Ct) / 5;
Pt:= MAlloc(PLen+1);
P:= Ct;
for I:= 0 to PLen-1 do
[StrNCopy(Quintet, P, 5);
Quintet(5):= 0;
Pt(I):= GetChar(Quintet);
P:= P+5;
];
Pt(PLen):= 0;
Release(Ct);
return Pt;
];
char PlainText, Message, CipherText, HiddenText;
[\Maps successively from 'a' to 'z' plus ' ' to denote any non-letter
Codes:= ["AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA",
"AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB",
"ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA",
"ABBBB", "BAAAA", "BAAAB", "BAABA", "BAABB",
"BABAA", "BABAB", "BABBA", "BABBB", "BBAAA",
"BBAAB", "BBBAA"];
PlainText:= "the quick brown fox jumps over the lazy dog";
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.";
CipherText:= BaconEncode(PlainText, Message);
Print("Cipher text ->\n\n%s\n", CipherText);
HiddenText:= BaconDecode(CipherText);
Print("\nHidden text ->\n\n%s\n", HiddenText);
Release(CipherText);
Release(HiddenText);
]</syntaxhighlight>
{{out}}
<pre>
Line 3,224 ⟶ 3,371:
=={{header|zkl}}==
{{trans|Python}}
<syntaxhighlight lang="zkl">class Bacon{
fcn init(_keyText){
var [const] keyText=_keyText.toLower(),
Line 3,254 ⟶ 3,401:
}
}</syntaxhighlight>
<syntaxhighlight lang="zkl">bacon:=Bacon(
#<<<
0'|All children, except one, grow up. They soon know that they will grow
|