Chaocipher: Difference between revisions

Added Easylang
(Added QBasic)
(Added Easylang)
 
(4 intermediate revisions by 3 users not shown)
Line 1,186:
readln;
end.</syntaxhighlight>
 
=={{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}}==
Line 3,215 ⟶ 3,293:
=={{header|QBasic}}==
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION AlphaLeft$ (ct AS STRING$, pt AS STRING$, CharPos AS INTEGER!)
DECLARE FUNCTION AlphaRight$ (ct AS STRING$, pt AS STRING$, CharPos AS INTEGER!)
DECLARE FUNCTION EncodeDecode$ (Text AS STRING$, ct AS STRING$, pt AS STRING$)
DECLARE FUNCTION DecodeEncode$ (Text AS STRING$, ct AS STRING$, pt AS STRING$)
 
CLS
Line 3,224 ⟶ 3,302:
' 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
Line 3,231 ⟶ 3,309:
' 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$
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$
PRINT
 
IF eText$ = cText$ THEN PRINT "Successful" ELSE PRINT "Failed"
 
ctLeft$ = tLeft: ptRight = tRight$
ptRight$ = tRight$
DIM dText AS STRING: dText$ = Decode$(eText$, ctLeft$, ptRight$)
PRINT : PRINT " Plain Text: "; dText: PRINT
PRINT
PRINT : PRINT " Plain Text$: "; dText: PRINT$
PRINT
 
IF dText$ = pText$ THEN PRINT "Successful" ELSE PRINT "Failed"
END
 
' Left Alphabet
FUNCTION AlphaLeft$ (ct AS STRING$, pt AS STRING$, CharPos AS INTEGER)
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 "holeHole$"
DIM Hole AS STRING: Hole$ = MID$(tStr, 2, 1): 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)
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.,
Line 3,300 ⟶ 3,384:
' 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 "holeHole$".
DIM Hole AS STRING: Hole$ = MID$(tStr, 3, 1): 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 AS STRING$, ct AS STRING$, pt AS STRING$)
DIMtStr$ 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
 
FUNCTION Encode$ (Text AS STRING$, ct AS STRING$, pt AS STRING$)
DIMtStr$ 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</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Raku}}==
Line 3,703 ⟶ 3,787:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class Chao {
static encrypt { 0 }
static decrypt { 1 }
1,983

edits