Chaocipher: Difference between revisions

44,126 bytes added ,  2 months ago
Added Easylang
(Added 11l)
(Added Easylang)
 
(30 intermediate revisions by 16 users not shown)
Line 8:
 
;Task:
Code the algorithm in your language and to test that it works with the plaintext 'WELLDONEISBETTERTHANWELLSAID' used in the paper itself.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F correct_case(string)
R string.filter(s -> s.is_alpha()).map(s -> s.uppercase()).join(‘’)
 
Line 61 ⟶ 60:
print()
 
do_chao(msg, lalpha, ralpha, 1B, 1B)</langsyntaxhighlight>
 
{{out}}
Line 105 ⟶ 104:
YFJBGMTKWNOQXCHIDVALZRSPUE JIBMESWKYZXUCOPRTLNHFAGVQD
</pre>
 
=={{header|Ada}}==
This solution uses array slices to permute the left and right strings. Use of slices clarifies the looping logic.
Ada strings are indexed with the predefined subtype Positive which begins at 1.
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
 
Line 211 ⟶ 209:
Put_Line ("The recovered plaintext is : " & plaintext2);
end chao_slices;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 251 ⟶ 249:
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">-- Chaocipher algorithm by J.F.Byrne 1918.
on chaocipher(input, |key|, mode)
-- input: text to be enciphered or deciphered.
-- |key|: script object or record with leftAlpha and rightAlpha properties, each of whose values is a shuffled alphabet text.
-- mode: the text "encipher" or "decipher".
script o
property inputChars : input's characters
property leftAlpha : |key|'s leftAlpha's characters
property rightAlpha : |key|'s rightAlpha's characters
property inAlpha : leftAlpha
property outAlpha : rightAlpha
property output : {}
end script
set alphaLen to (count o's leftAlpha)
if ((count o's rightAlpha) ≠ alphaLen) then error
if (mode is "encipher") then
set {o's inAlpha, o's outAlpha} to {o's rightAlpha, o's leftAlpha}
else if (mode is not "decipher") then
error
end if
set zenith to 1
set nadir to alphaLen div 2 + 1
repeat with char in o's inputChars
set char to char's contents
set found to false
repeat with i from 1 to alphaLen
if (o's inAlpha's item i = char) then
set end of o's output to o's outAlpha's item i
set found to true
exit repeat
end if
end repeat
if (found) then
rotate(o's leftAlpha, zenith, alphaLen, -(i - zenith))
rotate(o's leftAlpha, zenith + 1, nadir, -1)
rotate(o's rightAlpha, zenith, alphaLen, -i)
rotate(o's rightAlpha, zenith + 2, nadir, -1)
end if
end repeat
return join(o's output, "")
end chaocipher
 
on rotate(theList, l, r, amount)
set listLength to (count theList)
if (listLength < 2) then return
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
script o
property lst : theList
property storage : missing value
end script
set rangeLength to r - l + 1
set amount to (rangeLength + rangeLength - amount) mod rangeLength
if (amount is 0) then return
set o's storage to o's lst's items l thru (l + amount - 1)
repeat with i from (l + amount) to r
set o's lst's item (i - amount) to o's lst's item i
end repeat
set j to r - amount
repeat with i from 1 to amount
set o's lst's item (j + i) to o's storage's item i
end repeat
end rotate
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
-- Return a script object containing a couple of randomised alphabets to use as a choacipher key.
on makeKey()
set lAlpha to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"'s characters
copy lAlpha to rAlpha
script |key|
property leftAlpha : join(shuffle(lAlpha, 1, -1), "")
property rightAlpha : join(shuffle(rAlpha, 1, -1), "")
end script
return |key|
end makeKey
 
-- Fisher-Yates (aka Durstenfeld, aka Knuth) shuffle.
on shuffle(theList, l, r)
set listLength to (count theList)
if (listLength < 2) then return array
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
script o
property lst : theList
end script
repeat with i from l to (r - 1)
set j to (random number from i to r)
set v to o's lst's item i
set o's lst's item i to o's lst's item j
set o's lst's item j to v
end repeat
return theList
end shuffle
 
-- Demo using the two-alphabet key from the Rubin paper and another generated at random.
-- Decription must be with the key that was used for the encription.
on demo(originalText)
set key1 to {leftAlpha:"HXUCZVAMDSLKPEFJRIGTWOBNYQ", rightAlpha:"PTLNBQDEOYSFAVZKGJRIHWXUMC"}
set key2 to makeKey()
set enciphered to chaocipher(originalText, key1, "encipher")
set doubleEnciphered to chaocipher(enciphered, key2, "encipher")
set deDoubleEnciphered to chaocipher(doubleEnciphered, key2, "decipher")
set deciphered to chaocipher(deDoubleEnciphered, key1, "decipher")
return join({"Original text = " & originalText, ¬
"Enciphered = " & enciphered, "Double enciphered = " & doubleEnciphered, ¬
"De-double enciphered = " & deDoubleEnciphered, "Deciphered = " & deciphered}, linefeed)
end demo
demo("WELLDONEISBETTERTHANWELLSAID")</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"Original text = WELLDONEISBETTERTHANWELLSAID
Enciphered = OAHQHCNYNXTSZJRRHJBYHQKSOUJY
Double enciphered = ZJVDGIXNNDNRHAXQUUJZGAFTANHW
De-double enciphered = OAHQHCNYNXTSZJRRHJBYHQKSOUJY
Deciphered = WELLDONEISBETTERTHANWELLSAID"</syntaxhighlight>
=={{header|Arc}}==
<langsyntaxhighlight lang="arc">(= lshift '((0 1) (2 14) (1 2) (14 26)))
(= rshift '((1 3) (4 15) (3 4) (15 26) (0 1)))
 
Line 288 ⟶ 416:
(chaocipher "HXUCZVAMDSLKPEFJRIGTWOBNYQ" "PTLNBQDEOYSFAVZKGJRIHWXUMC"
"OAHQHCNYNXTSZJRRHJBYHQKSOUJY" nil 1)
</syntaxhighlight>
</lang>
 
{{output}}
<langsyntaxhighlight lang="arc">
arc> (chaocipher "HXUCZVAMDSLKPEFJRIGTWOBNYQ" "PTLNBQDEOYSFAVZKGJRIHWXUMC"
"WELLDONEISBETTERTHANWELLSAID")
Line 325 ⟶ 453:
YFJBGMTKWNOQXCHIDVALZRSPUE JIBMESWKYZXUCOPRTLNHFAGVQD
"OAHQHCNYNXTSZJRRHJBYHQKSOUJY"
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">LeftW := "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
RghtW := "PTLNBQDEOYSFAVZKGJRIHWXUMC"
 
Line 375 ⟶ 502:
Arr[14] := ch ; place 2nd/3rd chr in pos 14
return Arr
}</langsyntaxhighlight>
{{out}}
<pre>Original text: WELLDONEISBETTERTHANWELLSAID
Cipher text: OAHQHCNYNXTSZJRRHJBYHQKSOUJY
Decipher text: WELLDONEISBETTERTHANWELLSAID</pre>
=={{header|BASIC}}==
{{Works with|FreeBasic}}
{{Works with|PowerBasic}}
<SyntaxHighlight lang="BASIC">
' Caocipher Example
' Rosetta Code
' This code was made in Power Basic 3.5 for DOS
 
CLS
 
' Left Alphabet
Function AlphaLeft(ct as String, pt as String, CharPos as Integer) as String
 
Dim tStr as String: tStr=ct
 
' 1. Shift the entire left alphabet cyclically so the ciphertext letter
' just enciphered is positioned at the zenith (i.e., position 1).
tStr=Right$(ct, Len(ct)-CharPos+1)+Left$(ct, CharPos-1)
 
' 2. Extract the letter found at position zenith+1 (i.e., the letter to
' the right of the zenith), taking it out of the alphabet, temporarily
' leaving an unfilled "hole"
 
Dim Hole as String: Hole=Mid$(tStr, 2, 1): Mid$(tStr, 2, 1)=" "
 
' 3. Shift all letters in positions zenith+2 up to, and including, the
' nadir (zenith+13), moving them one position to the left
 
tStr=Left$(tStr, 1)+Mid$(tStr, 3, 12)+" "+Right$(tStr, 12)
 
' 4. Insert the just-extracted letter into the nadir position
' (i.e., zenith+13)
 
Mid$(tStr, 14, 1)=Hole
 
AlphaLeft=tStr
End Function
 
' Right Alphabet
Function AlphaRight(ct as String, pt as String, CharPos as Integer) as String
 
Dim tStr as String: tStr=pt
 
' 1. Shift the entire right alphabet cyclically so the plaintext letter
' just enciphered is positioned at the zenith.
 
tStr=Right$(tStr, Len(tStr)-CharPos+1)+Left$(tStr, CharPos-1)
 
' 2. Now shift the entire alphabet one more position to the left (i.e.,
' the leftmost letter moves cyclically to the far right), moving a new
' letter into the zenith position.
 
tStr=Right$(tStr, 25)+Left$(tStr, 1)
 
' 3. Extract the letter at position zenith+2, taking it out of the
' alphabet, temporarily leaving an unfilled "hole".
 
Dim Hole as String: Hole=Mid$(tStr, 3, 1): Mid$(tStr, 3, 1)=" ":
 
' 4. Shift all letters beginning with zenith+3 up to, and including, the
' nadir (zenith+13), moving them one position to the left.
 
tStr=Left$(tStr, 2)+Mid$(tStr, 4, 11)+" "+Right$(tStr, 12)
 
' 5. Insert the just-extracted letter into the nadir position (zenith+13)
 
Mid$(tStr, 14, 1)=Hole
 
AlphaRight=tStr
End Function
 
Function Encode(Text as String, ct as String, pt as String) as String
Dim t as Integer
Dim tStr as String: tStr=""
 
For t=1 to Len(Text)
Dim Char as String: Char=Mid$(Text, t, 1)
Dim CharPos as Integer: CharPos=Instr(pt, Char)
 
ct=AlphaLeft(ct, pt, CharPos)
pt=AlphaRight(ct, pt, CharPos)
 
tStr=tStr+Left$(ct, 1)
Next
 
Encode=tStr
End Function
 
' Deciphering a Chaocipher-encrypted message is identical to the steps used
' for enciphering. The sole difference is that the decipherer locates the
' known ciphertext letter in the left (ct) alphabet, with the plaintext
' letter being the corresponding letter in the right (pt) alphabet
'
' Alphabet permuting is identical in enciphering and deciphering
 
Function Decode(Text as String, ct as String, pt as String) as String
Dim t as Integer
Dim tStr as String: tStr=""
 
For t=1 to Len(Text)
Dim Char as String: Char=Mid$(Text, t, 1)
Dim CharPos as Integer: CharPos=Instr(ct, Char)
 
ct=AlphaLeft(ct, pt, CharPos)
pt=AlphaRight(ct, pt, CharPos)
 
tStr=tStr+Right$(pt, 1)
Next
 
Decode=tStr
End Function
 
' Start of Main Code
 
' LEFT (Cipher Text): HXUCZVAMDSLKPEFJRIGTWOBNYQ
Dim tLeft as String: tLeft="HXUCZVAMDSLKPEFJRIGTWOBNYQ"
 
' RIGHT (Plain Text): PTLNBQDEOYSFAVZKGJRIHWXUMC
Dim tRight as String: tRight="PTLNBQDEOYSFAVZKGJRIHWXUMC"
 
' Cipher Message (Used to verify a good encoding)
Dim cText as String: cText="OAHQHCNYNXTSZJRRHJBYHQKSOUJY"
 
' Plain Text Message
Dim pText as String: pText="WELLDONEISBETTERTHANWELLSAID"
Print " Plain Text: "; pText: Print
 
Dim ctLeft as String: ctLeft=tLeft
Dim ptRight as String: ptRight=tRight
 
' Final Cipher Text
Dim eText as String: eText=Encode(pText, ctLeft, ptRight)
Print " Cipher Text: "; eText: Print
 
If eText=cText then Print "Successful" else Print "Failed"
 
ctLeft=tLeft: ptRight=tRight
Dim dText as String: dText=Decode(eText, ctLeft, ptRight)
Print: Print " Plain Text: "; dText: Print
 
If dText=pText then Print "Successful" else Print "Failed"
</SyntaxHighlight>
<pre>
Plain Text: WELLDONEISBETTERTHANWELLSAID
Cipher text: OAHQHCNYNXTSZJRRHJBYHQKSOUJY
Successful
Plain Text: WELLDONEISBETTERTHANWELLSAID
Successful
</pre>
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 454 ⟶ 730:
free(plain_text2);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 495 ⟶ 771:
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace Chaocipher {
Line 563 ⟶ 838:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 601 ⟶ 876:
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
enum class Mode {
Line 670 ⟶ 944:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 707 ⟶ 981:
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.stdio;
import std.string;
 
Line 773 ⟶ 1,046:
auto plainText2 = exec(cipherText, Mode.DECRYPT);
writeln("\nThe recovered plaintext is : ", plainText2);
}</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 814 ⟶ 1,087:
{{libheader| System.SysUtils}}
{{Trans|Kotlin}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Chaocipher;
 
Line 912 ⟶ 1,185:
writeln(#10'The recovered plaintext is : ', plainText2);
readln;
end.</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc index c$ . a$[] ind .
for ind = 1 to len a$[]
if a$[ind] = c$
return
.
.
ind = 0
.
left$ = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
right$ = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
#
func$ chao txt$ mode .
left$[] = strchars left$
right$[] = strchars right$
len tmp$[] 26
for c$ in strchars txt$
# print strjoin left$[] & " " & strjoin right$[]
if mode = 1
index c$ right$[] ind
if ind = 0
return ""
.
r$ &= left$[ind]
else
index c$ left$[] ind
if ind = 0
print c$
return ""
.
r$ &= right$[ind]
.
# permute left
for j = ind to 26
tmp$[j - ind + 1] = left$[j]
.
for j = 1 to ind - 1
tmp$[26 - ind + j + 1] = left$[j]
.
h$ = tmp$[2]
for j = 3 to 14
tmp$[j - 1] = tmp$[j]
.
tmp$[14] = h$
swap tmp$[] left$[]
#
# permute right
for j = ind to 26
tmp$[j - ind + 1] = right$[j]
.
for j = 1 to ind - 1
tmp$[26 - ind + j + 1] = right$[j]
.
h$ = tmp$[1]
for j = 2 to 26
tmp$[j - 1] = tmp$[j]
.
tmp$[26] = h$
h$ = tmp$[3]
for j = 4 to 14
tmp$[j - 1] = tmp$[j]
.
tmp$[14] = h$
swap tmp$[] right$[]
.
return r$
.
h$ = chao "WELLDONEISBETTERTHANWELLSAID" 1
print h$
print chao h$ 2
</syntaxhighlight>
{{out}}
<pre>
OAHQHCNYNXTSZJRRHJBYHQKSOUJY
WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|EMal}}==
{{trans|C#}}
<syntaxhighlight lang="emal">
type Chaocipher:Mode
enum
int ENCRYPT, DECRYPT
end
type Chaocipher
text L_ALPHABET = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
text R_ALPHABET = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
fun exec = text by text value, Chaocipher:Mode mode, logic showSteps
^|since texts are mutable, we can operate directly on them without the need of Lists|^
text left = *L_ALPHABET # by using the valueOf operator we are sure that the string is copied
text right = *R_ALPHABET
text eText = text(" ", value.length)
text temp = text(" ", 26)
for int i = 0; i < value.length; ++i
if showSteps do writeLine(left + " " + right) end
int index = 0
if mode == Chaocipher:Mode.ENCRYPT
index = right.find(value[i])
eText[i] = left[index]
else
index = left.find(value[i])
eText[i] = right[index]
end
if i == value.length - 1 do break end
# permute left
for int j = index; j < 26; ++j do temp[j - index] = left[j] end
for int j = 0; j < index; ++j do temp[26 - index + j] = left[j] end
var store = temp[1]
for int j = 2; j < 14; ++j do temp[j - 1] = temp[j] end
temp[13] = store
left = *temp
# permute right
for int j = index; j < 26; ++j do temp[j - index] = right[j] end
for int j = 0; j < index; ++j do temp[26 - index + j] = right[j] end
store = temp[0]
for int j = 1; j < 26; ++j do temp[j - 1] = temp[j] end
temp[25] = store
store = temp[2]
for int j = 3; j < 14; ++j do temp[j - 1] = temp[j] end
temp[13] = store
right = *temp
end
return eText
end
var plainText = "WELLDONEISBETTERTHANWELLSAID"
writeLine("The original plaintext is : " + plainText)
writeLine(EOL + "The left and right alphabets after each permutation during encryption are :" + EOL)
var cipherText = exec(plainText, Chaocipher:Mode.ENCRYPT, true)
writeLine(EOL + "The ciphertext is : " + cipherText)
var plainText2 = exec(cipherText, Chaocipher:Mode.DECRYPT, false)
writeLine(EOL + "The recovered plaintext is : " + plainText2)
</syntaxhighlight>
{{out}}
<pre>
The original plaintext is : WELLDONEISBETTERTHANWELLSAID
 
The left and right alphabets after each permutation during encryption are :
 
HXUCZVAMDSLKPEFJRIGTWOBNYQ PTLNBQDEOYSFAVZKGJRIHWXUMC
ONYQHXUCZVAMDBSLKPEFJRIGTW XUCPTLNBQDEOYMSFAVZKGJRIHW
ADBSLKPEFJRIGMTWONYQHXUCZV OYSFAVZKGJRIHMWXUCPTLNBQDE
HUCZVADBSLKPEXFJRIGMTWONYQ NBDEOYSFAVZKGQJRIHMWXUCPTL
QUCZVADBSLKPEHXFJRIGMTWONY NBEOYSFAVZKGQDJRIHMWXUCPTL
HFJRIGMTWONYQXUCZVADBSLKPE JRHMWXUCPTLNBIEOYSFAVZKGQD
CVADBSLKPEHFJZRIGMTWONYQXU YSAVZKGQDJRHMFWXUCPTLNBIEO
NQXUCVADBSLKPYEHFJZRIGMTWO BIOYSAVZKGQDJERHMFWXUCPTLN
YHFJZRIGMTWONEQXUCVADBSLKP RHFWXUCPTLNBIMOYSAVZKGQDJE
NQXUCVADBSLKPEYHFJZRIGMTWO MOSAVZKGQDJERYHFWXUCPTLNBI
XCVADBSLKPEYHUFJZRIGMTWONQ AVKGQDJERYHFWZXUCPTLNBIMOS
TONQXCVADBSLKWPEYHUFJZRIGM IMSAVKGQDJERYOHFWZXUCPTLNB
SKWPEYHUFJZRILGMTONQXCVADB RYHFWZXUCPTLNOBIMSAVKGQDJE
ZILGMTONQXCVARDBSKWPEYHUFJ LNBIMSAVKGQDJOERYHFWZXUCPT
JILGMTONQXCVAZRDBSKWPEYHUF LNIMSAVKGQDJOBERYHFWZXUCPT
RBSKWPEYHUFJIDLGMTONQXCVAZ RYFWZXUCPTLNIHMSAVKGQDJOBE
RSKWPEYHUFJIDBLGMTONQXCVAZ YFZXUCPTLNIHMWSAVKGQDJOBER
HFJIDBLGMTONQUXCVAZRSKWPEY LNHMWSAVKGQDJIOBERYFZXUCPT
JDBLGMTONQUXCIVAZRSKWPEYHF MWAVKGQDJIOBESRYFZXUCPTLNH
BGMTONQUXCIVALZRSKWPEYHFJD VKQDJIOBESRYFGZXUCPTLNHMWA
YFJDBGMTONQUXHCIVALZRSKWPE HMAVKQDJIOBESWRYFGZXUCPTLN
HIVALZRSKWPEYCFJDBGMTONQUX RYGZXUCPTLNHMFAVKQDJIOBESW
QXHIVALZRSKWPUEYCFJDBGMTON SWYGZXUCPTLNHRMFAVKQDJIOBE
KPUEYCFJDBGMTWONQXHIVALZRS NHMFAVKQDJIOBRESWYGZXUCPTL
SPUEYCFJDBGMTKWONQXHIVALZR NHFAVKQDJIOBRMESWYGZXUCPTL
OQXHIVALZRSPUNEYCFJDBGMTKW WYZXUCPTLNHFAGVKQDJIOBRMES
UEYCFJDBGMTKWNOQXHIVALZRSP GVQDJIOBRMESWKYZXUCPTLNHFA
JBGMTKWNOQXHIDVALZRSPUEYCF OBMESWKYZXUCPRTLNHFAGVQDJI
 
The ciphertext is : OAHQHCNYNXTSZJRRHJBYHQKSOUJY
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|F_Sharp|F#}}==
===The Functions===
<langsyntaxhighlight lang="fsharp">
// Implement Chaocipher. Nigel Galloway: July 13th., 2019
let pL n=function g when g=n->0 |g when g=(n+1)%26->13 |g->let x=(25+g-n)%26 in if x<13 then x else x+1
Line 924 ⟶ 1,370:
let decrypt lW rW txt=Array.scan(fun (_,lW,rW) t->let n=Array.findIndex(fun n->n=t) lW in ((Array.item n rW,Array.permute(pL n) lW,(Array.permute(pR n) rW))))('0',lW,rW) txt
|>Array.skip 1|>Array.map(fun(n,_,_)->n)|>System.String
</syntaxhighlight>
</lang>
 
===The Task===
<langsyntaxhighlight lang="fsharp">
printfn "%s" (encrypt ("HXUCZVAMDSLKPEFJRIGTWOBNYQ".ToCharArray()) ("PTLNBQDEOYSFAVZKGJRIHWXUMC".ToCharArray()) ("WELLDONEISBETTERTHANWELLSAID".ToCharArray()))
printfn "%s" (decrypt ("HXUCZVAMDSLKPEFJRIGTWOBNYQ".ToCharArray()) ("PTLNBQDEOYSFAVZKGJRIHWXUMC".ToCharArray()) ("OAHQHCNYNXTSZJRRHJBYHQKSOUJY".ToCharArray()))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 936 ⟶ 1,382:
WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays combinators fry io kernel locals math namespaces
prettyprint sequences sequences.extras strings ;
IN: rosetta-code.chaocipher
Line 998 ⟶ 1,443:
[ print ] bi@ ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,004 ⟶ 1,449:
WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Chaocipher}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Chaocipher 01.png]]
 
'''Test 1.''' Encryption
 
[[File:Fōrmulæ - Chaocipher 02.png]]
 
[[File:Fōrmulæ - Chaocipher 03.png]]
 
'''Test 2.''' Decryption
 
[[File:Fōrmulæ - Chaocipher 04.png]]
 
[[File:Fōrmulæ - Chaocipher 05.png]]
 
=={{header|FreeBASIC}}==
The [[#BASIC|BASIC]] solution works without any changes.
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="FutureBasic">
 
begin enum
_encrypt
_decrypt
end enum
 
local fn chaocipher(orig as str255, action as byte, show as bool) as str255
str255 leftStr, rightStr, out
short i, index
leftStr = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
rightStr = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
orig = ucase$(orig)
out[0] = orig[0]
if show then print:print,"The left and right alphabets during encryption are:":print
for i = 1 to orig[0]
if show then print ,leftStr,,rightStr
if action == _encrypt
index = instr$(0, rightStr, mid$(orig, i, 1))
out[i] = leftStr[index]
else
index = instr$(0, leftStr, mid$(orig, i, 1))
out[i] = rightStr[index]
end if
//leftStr permutation
leftStr = mid$(leftStr, index) + left$(leftStr, index-1)
leftStr = left$(leftStr, 1) + mid$(leftStr, 3, 12) + mid$(leftStr, 2, 1) + mid$(leftStr, 15)
//rightStr permutation
rightStr = mid$(rightStr, index+1) + left$(rightStr, index-1) + mid$(rightStr, index, 1)
rightStr = left$(rightStr, 2) + mid$(rightStr, 4, 11) + mid$(rightStr, 3, 1) + mid$(rightStr, 15)
next
end fn = out
 
 
str255 original, encrypted, decrypted
original = "WellDoneIsBetterThanWellSaid"
 
window 1, @"Chaocipher", ( 0, 0, 475, 550 )
print : print ,"The original text is: """; original; """"
encrypted = fn chaocipher(original, _encrypt, yes)
print : print ,"The encrypted text is: """; encrypted; """"
decrypted = fn chaocipher(encrypted, _decrypt, no)
print : print ,"The decrypted text is: """; decrypted; """"
handleevents
 
</syntaxhighlight>
{{out}}
[[File:FB Chaocipher results.png]]
 
In '''[https://formulae.org/?example=Chaocipher this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import(
Line 1,109 ⟶ 1,624:
plainText2 := Chao(cipherText, Decrypt, false)
fmt.Println("\nThe recovered plaintext is :", plainText2)
}</langsyntaxhighlight>
 
{{out}}
Line 1,118 ⟶ 1,633:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Chaocipher {
private enum Mode {
ENCRYPT,
Line 1,196 ⟶ 1,711:
println("\nThe recovered plaintext is : $plainText2")
}
}</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 1,233 ⟶ 1,748:
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (elemIndex)
 
chao :: Eq a => [a] -> [a] -> Bool -> [a] -> [a]
chao
:: Eq a
=> [a] -> [a] -> Bool -> [a] -> [a]
chao _ _ _ [] = []
chao l r plain (x : xs) = maybe [] go (elemIndex x src)
where
(src, dst)
Line 1,263 ⟶ 1,775:
(x, y) = splitAt src a
 
encode, decode :: Bool
encode = False
 
decode = True
 
Line 1,270 ⟶ 1,782:
main = do
let chaoWheels =
chao
chao "HXUCZVAMDSLKPEFJRIGTWOBNYQ" "PTLNBQDEOYSFAVZKGJRIHWXUMC"
"HXUCZVAMDSLKPEFJRIGTWOBNYQ"
"PTLNBQDEOYSFAVZKGJRIHWXUMC"
plainText = "WELLDONEISBETTERTHANWELLSAID"
cipherText = chaoWheels encode plainText
mapM_
print plainText
print
print cipherText
[ plainText,
print $ chaoWheels decode cipherText</lang>
cipherText,
chaoWheels decode cipherText
]</syntaxhighlight>
{{Out}}
<pre>
Line 1,282 ⟶ 1,799:
"WELLDONEISBETTERTHANWELLSAID"</pre>
 
=={{header|J}}==
{{trans|Raku}}
<syntaxhighlight lang="j">reset =: verb define
LEFT =: 'HXUCZVAMDSLKPEFJRIGTWOBNYQ'
RIGHT =: 'PTLNBQDEOYSFAVZKGJRIHWXUMC'
)
 
enc =: verb define
z =. LEFT {~ i =. RIGHT i. y
permute {. i
z
)
 
dec =: verb define
z =. RIGHT {~ i =. LEFT i. y
permute {. i
z
)
 
permute =: verb define
LEFT =: LEFT |.~ - y
LEFT =: (1 |. 13 {. LEFT) , 13 }. LEFT
RIGHT =: RIGHT |.~ - y + 1
RIGHT =: ({. RIGHT) , (1 |. RIGHT {~ 2+i.12) , 13 }. RIGHT
)
 
chao =: enc :. dec
 
reset ''
smoutput E =. chao 'WELLDONEISBETTERTHANWELLSAID'
reset ''
smoutput D =. chao^:_1 E</syntaxhighlight>
 
{{out}}
<pre>
OMUUADCMTLZMXXMGXWPCOMUULPTA
WELLDONEISBETTERTHANWELLSAID
</pre>
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Chaocipher {
Line 1,364 ⟶ 1,920:
System.out.printf("\nThe recovered plaintext is : %s\n", plainText2);
}
}</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 1,401 ⟶ 1,957:
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|J}}==
{{trans|Raku}}
<lang j>reset =: verb define
LEFT =: 'HXUCZVAMDSLKPEFJRIGTWOBNYQ'
RIGHT =: 'PTLNBQDEOYSFAVZKGJRIHWXUMC'
)
 
enc =: verb define
z =. LEFT {~ i =. RIGHT i. y
permute {. i
z
)
 
dec =: verb define
z =. RIGHT {~ i =. LEFT i. y
permute {. i
z
)
 
permute =: verb define
LEFT =: LEFT |.~ - y
LEFT =: (1 |. 13 {. LEFT) , 13 }. LEFT
RIGHT =: RIGHT |.~ - y + 1
RIGHT =: ({. RIGHT) , (1 |. RIGHT {~ 2+i.12) , 13 }. RIGHT
)
 
chao =: enc :. dec
 
reset ''
smoutput E =. chao 'WELLDONEISBETTERTHANWELLSAID'
reset ''
smoutput D =. chao^:_1 E</lang>
 
{{out}}
<pre>
OMUUADCMTLZMXXMGXWPCOMUULPTA
WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|JavaScript}}==
{{trans|C}}
Script source
<langsyntaxhighlight lang="javascript">const L_ALPHABET = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
const R_ALPHABET = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
 
Line 1,544 ⟶ 2,059:
var decipher_text = chao(cipher_text, DECRYPT, false);
out.innerHTML += "<p>The recovered plaintext is : " + decipher_text + "</p>";
}</langsyntaxhighlight>
 
Solution page
<langsyntaxhighlight lang="html"><!DOCTYPE html>
<html>
<head>
Line 1,556 ⟶ 2,071:
<div id="content"></div>
</body>
</html></langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
The ciphertext is : OAHQHCNYNXTSZJRRHJBYHQKSOUJY
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|Julia}}==
Modified from the Kotlin and Raku entries.
<langsyntaxhighlight lang="julia">const leftalphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
const rightalphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
 
Line 1,594 ⟶ 2,108:
 
testchacocipher("WELLDONEISBETTERTHANWELLSAID")
</langsyntaxhighlight>{{out}}
<pre>
The original plaintext is: WELLDONEISBETTERTHANWELLSAID
Line 1,632 ⟶ 2,146:
Decoded, the recovered plaintext is: WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|Kotlin}}==
This is based on the C# implementation referred to in the task description, except that the encrypt and decrypt operations are combined into a single method.
<langsyntaxhighlight lang="scala">// Version 1.2.40
 
enum class Mode { ENCRYPT, DECRYPT }
Line 1,697 ⟶ 2,210:
val plainText2 = Chao.exec(cipherText, Mode.DECRYPT)
println("\nThe recovered plaintext is : $plainText2")
}</langsyntaxhighlight>
 
{{output}}
Line 1,738 ⟶ 2,251:
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
 
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Chaocipher, in Lua, 6/19/2020 db
local Chaocipher = {
ct = "HXUCZVAMDSLKPEFJRIGTWOBNYQ",
Line 1,766 ⟶ 2,277:
print("The original text was: " .. plainText)
print("The encrypted text is: " .. encryptText)
print("The decrypted text is: " .. decryptText)</langsyntaxhighlight>
{{out}}
<pre>HXUCZVAMDSLKPEFJRIGTWOBNYQ PTLNBQDEOYSFAVZKGJRIHWXUMC O W
Line 1,800 ⟶ 2,311:
The encrypted text is: OAHQHCNYNXTSZJRRHJBYHQKSOUJY
The decrypted text is: WELLDONEISBETTERTHANWELLSAID</pre>
 
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ichaoalphabet, iMoveToFront, ChaoCipher]
ichaoalphabet = CharacterRange["A", "Z"];
iMoveToFront[l_List, sel_] := Module[{p},
Line 1,848 ⟶ 2,357:
]
ChaoCipher["WELLDONEISBETTERTHANWELLSAID",{Characters@"PTLNBQDEOYSFAVZKGJRIHWXUMC",Characters@"HXUCZVAMDSLKPEFJRIGTWOBNYQ"}]
</syntaxhighlight>
</lang>
{{out}}
<pre>OAHQHCNYNXTSZJRRHJBYHQKSOUJY</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="nim">import strformat
 
type
Line 1,922 ⟶ 2,430:
echo &"\nThe ciphertext is: {cipherText}"
var plainText2 = chao(cipherText, Decrypt, false)
echo &"\nThe recovered plaintext is: {plainText2}"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,964 ⟶ 2,472:
===Another implementation===
Using functions from the stdlib instead of manual array manipulations:
<langsyntaxhighlight lang="nim">import std/[algorithm, strutils]
 
type
Line 2,010 ⟶ 2,518:
echo "\nThe ciphertext is: ", cipherText
let plainText2 = chao(cipherText, Decrypt, false)
echo "\nThe recovered plaintext is: ", plainText2</langsyntaxhighlight>
Same output as above.
 
=={{header|Objeck}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="objeck">class Chaocipher {
L_ALPHABET : static : Char[];
R_ALPHABET : static : Char[];
Line 2,093 ⟶ 2,600:
 
enum Mode { ENCRYPT, DECRYPT }
}</langsyntaxhighlight>
 
{{output}}
Line 2,133 ⟶ 2,640:
The recovered plaintext is: WELLDONEISBETTERTHANWELLSAID
</pre>
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<syntaxhighlight lang="pascal">program chaocipher(input, output);
 
const
{ This denotes a `set` literal: }
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
{ The `card` function is an Extended Pascal (ISO 10206) extension. }
alphabetCardinality = card(alphabet);
{ 1st character denotes “zenith”. }
zenith = 1;
{ In a 26-character alphabet the 14th character denotes “nadir”. }
nadir = alphabetCardinality div 2 + 1;
{ For simplicity use compile-time-defined maximum lengths. }
messageMaximumLength = 80;
 
type
{ This “discriminates” the Extended Pascal schema data type `string` to be }
{ capable of holding strings up to `alphabetCardinality` `char` values. }
map = string(alphabetCardinality);
{ Variables of this data type can only assume integer values within 1..26: }
mapCharacterIndex = 1..alphabetCardinality;
{ Later used as a buffer for the input/output. }
message = string(messageMaximumLength);
messageCharacterIndex = 1..messageMaximumLength;
{ Stores a key for the Chaocipher algorithm. }
key = record
cipherText: map;
plainText: map;
end;
 
{ --- auxilliary routines ---------------------------------------------- }
 
{
\brief verifies that a key is valid for the Chaocipher
\param sample a potential `key` for a Chaocipher
\return `true` iff \param sample is an acceptable `key`
}
{ `protected` (Extended Pascal extension) denotes an immutable parameter. }
function isValid(protected sample: key): Boolean;
{ Determines whether a `map` contains all characters of `alphabet`. }
{ Nesting this function allows for a neat expression below. }
function isComplete(protected text: map): Boolean;
var
i: integer;
{ `value []` will initialize this variable to an empty set value. }
{ This is an Extended Pascal (ISO 10206) extension. }
s: set of char value [];
begin
{ NB: In Pascal `for`-loop limits are inclusive. }
for i := 1 to length(text) do
begin
{ This adds the set containing one character to the set `s`. }
s := s + [text[i]]
end;
isComplete := card(s) = alphabetCardinality
end;
begin
{ This way `sample.cipherText` can be simply written as `cipherText`. }
with sample do
begin
{ `and_then` is an EP extension indicating “lazy evaluation”. }
isValid := (alphabetCardinality > 8) and_then
isComplete(cipherText) and_then isComplete(plainText)
end
end;
 
{
\brief permutes a key for the next encryption/decryption step
\param shift the index of the characters just substituted
}
{ `var` means the parameter value will be modified _at_ the call site. }
procedure permute(var state: key; protected shift: mapCharacterIndex);
begin
with state do
begin
{ Indices in `cipherText[1..pred(shift)]` _must_ be non-descending: }
if shift > 1 then
begin
cipherText := subStr(cipherText, shift) + cipherText[1..pred(shift)]
{ `subStr(str, ini)` is equivalent to `str[ini..length(str)]`. }
end;
{ Likewise, `succ(shift)` must be a valid index in `plainText`: }
if shift < alphabetCardinality then
begin
plainText := subStr(plainText, succ(shift)) + plainText[1..shift]
end;
{ If it does _not_ _alter_ the _entire_ string’s _length_, you can }
{ modify parts of a string like this (Extended Pascal extension): }
cipherText[zenith+1..nadir] := cipherText[zenith+2..nadir] + cipherText[zenith+1];
plainText[zenith+2..nadir] := plainText[zenith+3..nadir] + plainText[zenith+2]
end
end;
 
{ --- the core routine of the algorithm -------------------------------- }
 
{
\brief performs Chaocipher common steps
\param line the message to encrypt/decrypt
\param state the initial key to start encrpytion/decryption with
\param locate a function determining the 2-tuple index in the key
\param substitute the procedure substituting the correct characters
}
procedure chaocipher(
var line: message;
var state: key;
{ These are “routine parameters”. Essentially the address of a routine }
{ matching the specified routine signature is passed to `chaocipher`. }
function locate(protected i: messageCharacterIndex): mapCharacterIndex;
procedure substitute(
protected i: messageCharacterIndex;
protected z: mapCharacterIndex
)
);
var
{ For demonstration purposes: In this program }
{ `line.capacity` refers to `messageMaximumLength`. }
i: 1..line.capacity;
substitutionPairIndex: mapCharacterIndex;
begin
{ Don’t trust user input, even though this is just a RosettaCode example. }
if not isValid(state) then
begin
writeLn('Error: Key is invalid. Got:');
writeLn('Cipher text: ', state.cipherText);
writeLn(' Plain text: ', state.plainText);
halt
end;
for i := 1 to length(line) do
begin
{ We’ll better skip characters that aren’t in the `alphabet`. }
if line[i] in alphabet then
begin
{ Here you see the beauty of using routine parameters. }
{ Depending on whether we’re encrypting or decrypting, }
{ you need to find a character in the `cipherText` or }
{ `plainText` key value respectively, yet the basic order
{ of the steps are still the same. }
substitutionPairIndex := locate(i);
substitute(i, substitutionPairIndex);
permute(state, substitutionPairIndex)
end
end
end;
 
{ --- entry routines --------------------------------------------------- }
 
{
\brief encrypts a message according to Chaocipher
\param line a message to encrypt
\param state the key to begin with
\return the encrypted message \param line using the provided key
}
{ Note: without `var` or `protected` both `encrypt` and `decrypt`get }
{ and have their own independent copies of the parameter values. }
function encrypt(line: message; state: key): message;
function encryptor(protected i: messageCharacterIndex): mapCharacterIndex;
begin
encryptor := index(state.plainText, line[i])
end;
procedure substitutor(
protected i: messageCharacterIndex;
protected z: mapCharacterIndex
);
begin
line[i] := state.cipherText[z]
end;
begin
chaocipher(line, state, encryptor, substitutor);
encrypt := line
end;
 
{
\brief decrypts a message according to Chaocipher
\param line the encrypted message
\param state the key to begin with
\return the decrypted message \param line using the provided key
}
function decrypt(line: message; state: key): message;
function decryptor(protected i: messageCharacterIndex): mapCharacterIndex;
begin
decryptor := index(state.cipherText, line[i])
end;
procedure substitutor(
protected i: messageCharacterIndex;
protected z: mapCharacterIndex
);
begin
line[i] := state.plainText[z]
end;
begin
chaocipher(line, state, decryptor, substitutor);
decrypt := line
end;
 
{ === MAIN ============================================================= }
var
exampleKey: key;
line: message;
begin
{ Instead of writing `exampleKey.cipherText := '…', you can }
{ write in Extended Pascal a `record` literal like this: }
exampleKey := key[
cipherText: 'HXUCZVAMDSLKPEFJRIGTWOBNYQ';
plainText: 'PTLNBQDEOYSFAVZKGJRIHWXUMC';
];
{ `EOF` is shorthand for `EOF(input)`. }
while not EOF do
begin
{ `readLn(line)` is shorthand for `readLn(input, line)`. }
readLn(line);
line := encrypt(line, exampleKey);
writeLn(decrypt(line, exampleKey));
{ Likewise, `writeLn(line)` is short for `writeLn(output, line)`. }
writeLn(line)
end
end.</syntaxhighlight>
{{in}}
WELLDONEISBETTERTHANWELLSAID
{{out}}
WELLDONEISBETTERTHANWELLSAID
OAHQHCNYNXTSZJRRHJBYHQKSOUJY
 
=={{header|Perl}}==
{{trans|Raku}}
Since <tt>rotate</tt> is not a built-in in Perl, using a custom one, not general-purpose but sufficient for this task.
<syntaxhighlight lang="perl" line>use strict;
<lang perl>sub init {
use warnings;
my(@left,@right,$e_msg,$d_msg);
 
sub init {
@left = split '', 'HXUCZVAMDSLKPEFJRIGTWOBNYQ';
@right = split '', 'PTLNBQDEOYSFAVZKGJRIHWXUMC';
Line 2,173 ⟶ 2,910:
 
sub rotate {
ourmy @list; local *list = @{ shift() };
my($n,$s,$e) = @_;
@list = $s ? @list[0..$s-1, $s+$n..$e+$n-1, $s..$s+$n-1, $e+1..$#list]
: @list[$n..$#list, 0..$n-1]
}
 
init; $e_msg .= encode( $_) for split '', 'WELLDONEISBETTERTHANWELLSAID';
init; $d_msg .= decode( $_) for split '', $e_msg;
 
print "$e_msg\n$d_msg\n";</syntaxhighlight>
print "$d_msg\n";</lang>
{{out}}
<pre>OMUUADCMTLZMXXMGXWPCOMUULPTA
<pre>OAHQHCNYNXTSZJRRHJBYHQKSOUJY
WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|Phix}}==
Originally translated from C, but ended up more of a direct implementation of the algorithm in the pdf.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo\rosetta\Chao_cipher.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Chao_cipher.exw</span>
constant l_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ",
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
r_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
<span style="color: #008080;">constant</span> <span style="color: #000000;">l_alphabet</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"HXUCZVAMDSLKPEFJRIGTWOBNYQ"</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">r_alphabet</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"PTLNBQDEOYSFAVZKGJRIHWXUMC"</span>
enum ENCRYPT, DECRYPT
<span style="color: #008080;">enum</span> <span style="color: #000000;">ENCRYPT</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DECRYPT</span>
function chao_cipher(string in, integer mode, bool show_steps)
integer len = length(in)
<span style="color: #008080;">function</span> <span style="color: #000000;">chao_cipher</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">mode</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">show_steps</span><span style="color: #0000FF;">)</span>
string out = repeat(' ',len)
<span style="color: #004080;">integer</span> <span style="color: #000000;">len</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
string left = l_alphabet,
<span style="color: #004080;">string</span> <span style="color: #000000;">out</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">),</span>
right = r_alphabet
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">l_alphabet</span><span style="color: #0000FF;">,</span>
for i=1 to len do
<span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r_alphabet</span>
if show_steps then printf(1,"%s %s\n", {left, right}) end if
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">len</span> <span style="color: #008080;">do</span>
integer index = find(in[i],iff(mode==ENCRYPT?right:left))
<span style="color: #008080;">if</span> <span style="color: #000000;">show_steps</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
out[i] = iff(mode==ENCRYPT?left:right)[index]
<span style="color: #004080;">integer</span> <span style="color: #000000;">index</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">==</span><span style="color: #000000;">ENCRYPT</span><span style="color: #0000FF;">?</span><span style="color: #000000;">right</span><span style="color: #0000FF;">:</span><span style="color: #000000;">left</span><span style="color: #0000FF;">))</span>
 
<span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">==</span><span style="color: #000000;">ENCRYPT</span><span style="color: #0000FF;">?</span><span style="color: #000000;">left</span><span style="color: #0000FF;">:</span><span style="color: #000000;">right</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">index</span><span style="color: #0000FF;">]</span>
if i==len then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">==</span><span style="color: #000000;">len</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
/* permute left */
left = left[index..26]&left[1..index-1]
<span style="color: #000080;font-style:italic;">/* permute left */</span>
left[2..14] = left[3..14]&left[2]
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">index</span><span style="color: #0000FF;">..</span><span style="color: #000000;">26</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">index</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
 
<span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">14</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..</span><span style="color: #000000;">14</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
/* permute right */
right = right[index+1..26]&right[1..index]
right[3..14] = right[4..14]&right[3]
end for
return out
end function
<span style="color: #000080;font-style:italic;">/* permute right */</span>
string plain_text = "WELLDONEISBETTERTHANWELLSAID"
<span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">index</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">26</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">index</span><span style="color: #0000FF;">]</span>
printf(1,"The original plaintext is : %s\n", {plain_text})
<span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..</span><span style="color: #000000;">14</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">..</span><span style="color: #000000;">14</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
--printf(1,"\nThe left and right alphabets after each permutation"&
<span style="color: #008080;">return</span> <span style="color: #000000;">out</span>
-- " during encryption are :\n\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
--string cipher_text = chao_cipher(plain_text, ENCRYPT, true)
string cipher_text = chao_cipher(plain_text, ENCRYPT, false)
<span style="color: #004080;">string</span> <span style="color: #000000;">plain_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"WELLDONEISBETTERTHANWELLSAID"</span>
printf(1,"\nThe ciphertext is : %s\n", {cipher_text})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The original plaintext is : %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">plain_text</span><span style="color: #0000FF;">})</span>
 
string plain_text2 = chao_cipher(cipher_text, DECRYPT, false)
<span style="color: #000080;font-style:italic;">--printf(1,"\nThe left and right alphabets after each permutation"&
printf(1,"\nThe recovered plaintext is : %s\n", {plain_text2})</lang>
-- " during encryption are :\n\n")
--string cipher_text = chao_cipher(plain_text, ENCRYPT, true)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">cipher_text</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">chao_cipher</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plain_text</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ENCRYPT</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe ciphertext is : %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">cipher_text</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">plain_text2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">chao_cipher</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cipher_text</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DECRYPT</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe recovered plaintext is : %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">plain_text2</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,238 ⟶ 2,977:
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python"># Python3 implementation of Chaocipher
# left wheel = ciphertext wheel
# right wheel = plaintext wheel
Line 2,316 ⟶ 3,054:
return lalph, ralph
 
main()</langsyntaxhighlight>
<pre>L: HXUCZVAMDSLKPEFJRIGTWOBNYQ
R: PTLNBQDEOYSFAVZKGJRIHWXUMC
Line 2,362 ⟶ 3,100:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Chaocipher'''
 
from itertools import chain, cycle, islice
Line 2,547 ⟶ 3,285:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>WELLDONEISBETTERTHANWELLSAID
OAHQHCNYNXTSZJRRHJBYHQKSOUJY
WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|QBasic}}==
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION AlphaLeft$ (ct$, pt$, CharPos!)
DECLARE FUNCTION AlphaRight$ (ct$, pt$, CharPos!)
DECLARE FUNCTION Decode$ (Text$, ct$, pt$)
DECLARE FUNCTION Encode$ (Text$, ct$, pt$)
 
CLS
 
' Deciphering a Chaocipher-encrypted message is identical to the steps used
' for enciphering. The sole difference is that the decipherer locates the
' known ciphertext letter in the left (ct$) alphabet, with the plaintext
' letter being the corresponding letter in the right (pt$) alphabet
'
' Alphabet permuting is identical in enciphering and deciphering
 
' Start of Main Code
 
' LEFT (Cipher Text$): HXUCZVAMDSLKPEFJRIGTWOBNYQ
tLeft$ = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
 
' RIGHT (Plain Text$): PTLNBQDEOYSFAVZKGJRIHWXUMC
tRight$ = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
 
' Cipher Message (Used to verify a good encoding)
cText$ = "OAHQHCNYNXTSZJRRHJBYHQKSOUJY"
 
' Plain Text$ Message
pText$ = "WELLDONEISBETTERTHANWELLSAID"
PRINT " Plain Text$: "; pText$
PRINT
 
ctLeft$ = tLeft$
ptRight$ = tRight$
 
' Final Cipher Text$
eText$ = Encode$(pText$, ctLeft$, ptRight$)
PRINT " Cipher Text$: "; eText$
PRINT
 
IF eText$ = cText$ THEN PRINT "Successful" ELSE PRINT "Failed"
 
ctLeft$ = tLeft$
ptRight$ = tRight$
dText$ = Decode$(eText$, ctLeft$, ptRight$)
PRINT
PRINT " Plain Text$: "; dText$
PRINT
 
IF dText$ = pText$ THEN PRINT "Successful" ELSE PRINT "Failed"
END
 
' Left Alphabet
FUNCTION AlphaLeft$ (ct$, pt$, CharPos)
tStr$ = ct$
' 1. Shift the entire left alphabet cyclically so the ciphertext letter
' just enciphered is positioned at the zenith (i.e., position 1).
tStr$ = RIGHT$(ct$, LEN(ct$) - CharPos + 1) + LEFT$(ct$, CharPos - 1)
' 2. Extract the letter found at position zenith+1 (i.e., the letter to
' the right of the zenith), taking it out of the alphabet, temporarily
' leaving an unfilled "Hole$"
Hole$ = MID$(tStr$, 2, 1)
MID$(tStr$, 2, 1) = " "
' 3. Shift all letters in positions zenith+2 up to, and including, the
' nadir (zenith+13), moving them one position to the left
tStr$ = LEFT$(tStr$, 1) + MID$(tStr$, 3, 12) + " " + RIGHT$(tStr$, 12)
' 4. Insert the just-extracted letter into the nadir position
' (i.e., zenith+13)
MID$(tStr$, 14, 1) = Hole$
AlphaLeft$ = tStr$
END FUNCTION
 
' Right Alphabet
FUNCTION AlphaRight$ (ct$, pt$, CharPos)
tStr$ = pt$
' 1. Shift the entire right alphabet cyclically so the plaintext letter
' just enciphered is positioned at the zenith.
tStr$ = RIGHT$(tStr$, LEN(tStr$) - CharPos + 1) + LEFT$(tStr$, CharPos - 1)
' 2. Now shift the entire alphabet one more position to the left (i.e.,
' the leftmost letter moves cyclically to the far right), moving a new
' letter into the zenith position.
tStr$ = RIGHT$(tStr$, 25) + LEFT$(tStr$, 1)
' 3. Extract the letter at position zenith+2, taking it out of the
' alphabet, temporarily leaving an unfilled "Hole$".
Hole$ = MID$(tStr$, 3, 1)
MID$(tStr$, 3, 1) = " ":
' 4. Shift all letters beginning with zenith+3 up to, and including, the
' nadir (zenith+13), moving them one position to the left.
tStr$ = LEFT$(tStr$, 2) + MID$(tStr$, 4, 11) + " " + RIGHT$(tStr$, 12)
' 5. Insert the just-extracted letter into the nadir position (zenith+13)
MID$(tStr$, 14, 1) = Hole$
AlphaRight$ = tStr$
END FUNCTION
 
FUNCTION Decode$ (Text$, ct$, pt$)
tStr$ = ""
FOR t = 1 TO LEN(Text$)
Char$ = MID$(Text$, t, 1)
CharPos = INSTR(ct$, Char$)
ct$ = AlphaLeft$(ct$, pt$, CharPos)
pt$ = AlphaRight$(ct$, pt$, CharPos)
tStr$ = tStr$ + RIGHT$(pt$, 1)
NEXT
Decode$ = tStr$
END FUNCTION
 
FUNCTION Encode$ (Text$, ct$, pt$)
tStr$ = ""
FOR t = 1 TO LEN(Text$)
Char$ = MID$(Text$, t, 1)
CharPos = INSTR(pt$, Char$)
ct$ = AlphaLeft$(ct$, pt$, CharPos)
pt$ = AlphaRight$(ct$, pt$, CharPos)
tStr$ = tStr$ + LEFT$(ct$, 1)
NEXT
Encode$ = tStr$
END FUNCTION
</syntaxhighlight>
 
=={{header|Raku}}==
Line 2,557 ⟶ 3,441:
{{works with|Rakudo|2018.03}}
 
<syntaxhighlight lang="raku" perl6line>my @left;
my @right;
 
Line 2,589 ⟶ 3,473:
say 'WELLDONEISBETTERTHANWELLSAID'.comb».&encode.join;
reset;
say 'OAHQHCNYNXTSZJRRHJBYHQKSOUJY'.comb».&decode.join;</langsyntaxhighlight>
{{out}}
<pre>OAHQHCNYNXTSZJRRHJBYHQKSOUJY
WELLDONEISBETTERTHANWELLSAID</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">txt = "WELLDONEISBETTERTHANWELLSAID"
@left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ".chars
@right = "PTLNBQDEOYSFAVZKGJRIHWXUMC".chars
Line 2,614 ⟶ 3,497:
 
puts txt.each_char.map{|c| encrypt(c) }.join
</syntaxhighlight>
</lang>
{{out}}
<pre>OAHQHCNYNXTSZJRRHJBYHQKSOUJY
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">const LEFT_ALPHABET_CT: &str = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
const RIGHT_ALPHABET_PT: &str = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
const ZENITH: usize = 0;
Line 2,651 ⟶ 3,533:
println!("Plaintext: {}", SEQUENCE);
println!("Ciphertext: {}", ciphertext);
}</langsyntaxhighlight>
{{out}}
<pre>Plaintext: WELLDONEISBETTERTHANWELLSAID
Ciphertext: OAHQHCNYNXTSZJRRHJBYHQKSOUJY</pre>
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
templates chaocipher&{left:,right:,decode:}
templates permute
Line 2,688 ⟶ 3,569:
'OAHQHCNYNXTSZJRRHJBYHQKSOUJY' -> chaocipher&{left:'HXUCZVAMDSLKPEFJRIGTWOBNYQ', right:'PTLNBQDEOYSFAVZKGJRIHWXUMC',decode:1} -> '$;
' -> !OUT::write
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,695 ⟶ 3,576:
WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
ReadOnly L_ALPHABET As String = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
Line 2,775 ⟶ 3,655:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The original plaintext is : WELLDONEISBETTERTHANWELLSAID
Line 2,813 ⟶ 3,693:
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">type Mode = int
const(
encrypt = Mode(0)
decrypt = Mode(1)
)
const(
l_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
r_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
)
fn chao(text string, mode Mode, show_steps bool) string {
len := text.len
if text.bytes().len != len {
println("Text contains non-ASCII characters")
return ""
}
mut left := l_alphabet
mut right := r_alphabet
mut e_text := []u8{len: len}
mut temp := []u8{len: 26}
for i in 0..len {
if show_steps {
println('$left $right')
}
mut index := 0
if mode == encrypt {
index = right.index_u8(text[i])
e_text[i] = left[index]
} else {
index = left.index_u8(text[i])
e_text[i] = right[index]
}
if i == len - 1 {
break
}
// permute left
for j in index..26 {
temp[j - index] = left[j]
}
for j in 0..index {
temp[26 - index + j] = left[j]
}
mut store := temp[1]
for j in 2..14 {
temp[j - 1] = temp[j]
}
temp[13] = store
left = temp.bytestr()
// permute right
for j in index..26 {
temp[j - index] = right[j]
}
for j in 0..index {
temp[26 - index + j] = right[j]
}
store = temp[0]
for j in 1..26 {
temp[j - 1] = temp[j]
}
temp[25] = store
store = temp[2]
for j in 3..14 {
temp[j - 1] = temp[j]
}
temp[13] = store
right = temp.bytestr()
}
return e_text.bytestr()
}
fn main() {
plain_text := "WELLDONEISBETTERTHANWELLSAID"
println("The original plaintext is : $plain_text")
print("\nThe left and right alphabets after each permutation ")
println("during encryption are :\n")
cypher_text := chao(plain_text, encrypt, true)
println("\nThe ciphertext is : $cypher_text")
plain_text2 := chao(cypher_text, decrypt, false)
println("\nThe recovered plaintext is : $plain_text2")
}</syntaxhighlight>
 
{{out}}
<pre>Same as Kotlin Entry</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">class Chao {
static encrypt { 0 }
static decrypt { 1 }
Line 2,869 ⟶ 3,840:
System.print("\nThe ciphertext is : %(cipherText)")
var plainText2 = Chao.exec(cipherText, Chao.decrypt, false)
System.print("\nThe recovered plaintext is : %(plainText2)")</langsyntaxhighlight>
 
{{out}}
<pre>
The original plaintext is : WELLDONEISBETTERTHANWELLSAID
 
The left and right alphabets after each permutation during encryption are :
 
HXUCZVAMDSLKPEFJRIGTWOBNYQ PTLNBQDEOYSFAVZKGJRIHWXUMC
ONYQHXUCZVAMDBSLKPEFJRIGTW XUCPTLNBQDEOYMSFAVZKGJRIHW
ADBSLKPEFJRIGMTWONYQHXUCZV OYSFAVZKGJRIHMWXUCPTLNBQDE
HUCZVADBSLKPEXFJRIGMTWONYQ NBDEOYSFAVZKGQJRIHMWXUCPTL
QUCZVADBSLKPEHXFJRIGMTWONY NBEOYSFAVZKGQDJRIHMWXUCPTL
HFJRIGMTWONYQXUCZVADBSLKPE JRHMWXUCPTLNBIEOYSFAVZKGQD
CVADBSLKPEHFJZRIGMTWONYQXU YSAVZKGQDJRHMFWXUCPTLNBIEO
NQXUCVADBSLKPYEHFJZRIGMTWO BIOYSAVZKGQDJERHMFWXUCPTLN
YHFJZRIGMTWONEQXUCVADBSLKP RHFWXUCPTLNBIMOYSAVZKGQDJE
NQXUCVADBSLKPEYHFJZRIGMTWO MOSAVZKGQDJERYHFWXUCPTLNBI
XCVADBSLKPEYHUFJZRIGMTWONQ AVKGQDJERYHFWZXUCPTLNBIMOS
TONQXCVADBSLKWPEYHUFJZRIGM IMSAVKGQDJERYOHFWZXUCPTLNB
SKWPEYHUFJZRILGMTONQXCVADB RYHFWZXUCPTLNOBIMSAVKGQDJE
ZILGMTONQXCVARDBSKWPEYHUFJ LNBIMSAVKGQDJOERYHFWZXUCPT
JILGMTONQXCVAZRDBSKWPEYHUF LNIMSAVKGQDJOBERYHFWZXUCPT
RBSKWPEYHUFJIDLGMTONQXCVAZ RYFWZXUCPTLNIHMSAVKGQDJOBE
RSKWPEYHUFJIDBLGMTONQXCVAZ YFZXUCPTLNIHMWSAVKGQDJOBER
HFJIDBLGMTONQUXCVAZRSKWPEY LNHMWSAVKGQDJIOBERYFZXUCPT
JDBLGMTONQUXCIVAZRSKWPEYHF MWAVKGQDJIOBESRYFZXUCPTLNH
BGMTONQUXCIVALZRSKWPEYHFJD VKQDJIOBESRYFGZXUCPTLNHMWA
YFJDBGMTONQUXHCIVALZRSKWPE HMAVKQDJIOBESWRYFGZXUCPTLN
HIVALZRSKWPEYCFJDBGMTONQUX RYGZXUCPTLNHMFAVKQDJIOBESW
QXHIVALZRSKWPUEYCFJDBGMTON SWYGZXUCPTLNHRMFAVKQDJIOBE
KPUEYCFJDBGMTWONQXHIVALZRS NHMFAVKQDJIOBRESWYGZXUCPTL
SPUEYCFJDBGMTKWONQXHIVALZR NHFAVKQDJIOBRMESWYGZXUCPTL
OQXHIVALZRSPUNEYCFJDBGMTKW WYZXUCPTLNHFAGVKQDJIOBRMES
UEYCFJDBGMTKWNOQXHIVALZRSP GVQDJIOBRMESWKYZXUCPTLNHFA
JBGMTKWNOQXHIDVALZRSPUEYCF OBMESWKYZXUCPRTLNHFAGVQDJI
 
The ciphertext is : OAHQHCNYNXTSZJRRHJBYHQKSOUJY
 
The recovered plaintext is : WELLDONEISBETTERTHANWELLSAID
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \For StrLen, StrCopy, Print
 
func StrChar(Str, C);
char Str, C;
[loop [if Str(0) = 0 then return 0;
if Str(0) = C then return Str;
Str:= Str+1;
];
];
 
def \CMode\ ENCRYPT, DECRYPT;
char L_alphabet, R_alphabet;
 
proc Chao(In, Out, Mode, Show_steps);
char In, Out, Mode, Show_steps;
int Len, I, J, Index;
char Store, Left(27), Right(27), Temp(27);
[Len:= StrLen(In);
StrCopy(Left, L_alphabet);
StrCopy(Right, R_alphabet);
Temp(26):= 0;
 
for I:= 0 to Len-1 do
[if Show_steps then Print("%s %s\n", Left, Right);
if Mode = ENCRYPT then
[Index:= StrChar(Right, In(I)) - Right;
Out(I):= Left(Index);
]
else [Index:= StrChar(Left, In(I)) - Left;
Out(I):= Right(Index);
];
if I = Len-1 then return;
 
\Permute Left
for J:= Index to 26-1 do Temp(J-Index):= Left(J);
for J:= 0 to Index-1 do Temp(26-Index+J):= Left(J);
Store:= Temp(1);
for J:= 2 to 14-1 do Temp(J-1):= Temp(J);
Temp(13):= Store;
StrCopy(Left, Temp);
 
\Permute Right
for J:= Index to 26-1 do Temp(J-Index):= Right(J);
for J:= 0 to Index-1 do Temp(26-Index+J):= Right(J);
Store:= Temp(0);
for J:= 1 to 26-1 do Temp(J-1):= Temp(J);
Temp(25):= Store;
Store:= Temp(2);
for J:= 3 to 14-1 do Temp(J-1):= Temp(J);
Temp(13):= Store;
StrCopy(Right, Temp);
];
];
 
char Plain_text, Cipher_text, Plain_text2;
[L_alphabet:= "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
R_alphabet:= "PTLNBQDEOYSFAVZKGJRIHWXUMC";
Plain_text:= "WELLDONEISBETTERTHANWELLSAID";
Cipher_text:= MAlloc(StrLen(Plain_text) + 1);
Plain_text2:= MAlloc(StrLen(Plain_text) + 1);
Print("The original plaintext is : %s\n", Plain_text);
Print("\nThe left and right alphabets after each permutation during encryption are :\n\n");
Chao(Plain_text, Cipher_text, ENCRYPT, true);
Print("\nThe ciphertext is : %s\n", Cipher_text);
Chao(Cipher_text, Plain_text2, DECRYPT, false);
Print("\nThe recovered plaintext is : %s\n", Plain_text2);
Release(Cipher_text);
Release(Plain_text2);
]</syntaxhighlight>
{{out}}
<pre>
Line 2,913 ⟶ 3,995:
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">class Chao{
var [const private] lAlphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ",
rAlphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
Line 2,940 ⟶ 4,022:
right.insert(13,right.pop(2)); // rotate [2..13] once
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">plainText:="WELLDONEISBETTERTHANWELLSAID";
println("The original plaintext is : ",plainText);
println("\nThe left and right alphabets after each permutation"
Line 2,949 ⟶ 4,031:
 
plainText2:=Chao.decode(cipherText);
println("\nThe recovered plaintext is : ",plainText2);</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
1,982

edits