Substitution cipher: Difference between revisions

m
no edit summary
(→‎{{header|Risc-V}}: update Risc-V -> RISC-V Assembly)
mNo edit summary
 
(12 intermediate revisions by 7 users not shown)
Line 48:
Decoded: The quick brown fox jumps over the lazy dog, who barks VERY loudly!
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program subscipher64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ BUFFSIZE, 50000 // buffer size
.equ O_RDWR, 0x0002 // open for reading and writing
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessInst: .asciz "use : subscipher inputfile outpufile E (encryt) or D (decript).\n"
szMessCode: .asciz "Code operation not = E or D !!\n"
szMessErrorOpen: .asciz "Error open input file .\n"
szMessErrorCreate: .asciz "Error create output file.\n"
szMessErrorClose: .asciz "Error close file.\n"
szMessErrorRead: .asciz "Error read file.\n"
szMessErrorWrite: .asciz "Write error to output file.\n"
szMessTrtOK: .asciz "Encoding/decoding OK.\n"
szCarriageReturn: .asciz "\n"
// ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^ _`abcdefghijklmnopqrstuvwxyz
szBufferKey: .asciz "VsciBjedgrzyHalvXZKtUPumGf[\]^ _`IwJxqOCFRApnDhQWobLkESYMTN"
.equ LGBUFFERKEY, . - szBufferKey
/************************************/
/* UnInitialized data */
/************************************/
.bss
.align 4
qAdrFicInput: .skip 8
qAdrFicOutput: .skip 8
sBufferRead: .skip BUFFSIZE
sBufferWrite: .skip BUFFSIZE
/************************************/
/* code section */
/************************************/
.text
.global main
main:
mov fp,sp // fp <- start address
ldr x4,[fp] // number of Command line arguments
cmp x4,#4 // test if number is ok
beq 1f
ldr x0,qAdrszMessInst // no -> display error
bl affichageMess
b 100f
1:
ldr x6,[fp,#16] // address input file name
ldr x20,[fp,#24] // address output file name
ldr x5,[fp,#32] // address code operation
ldrb w21,[x5] // loaf first code character
cmp x21,#'E' // control if code is OK
beq 2f
cmp x21,#'D'
beq 2f
ldr x0,qAdrszMessCode // no -> display error
bl affichageMess
b 100f
2:
mov x0,AT_FDCWD
mov x1,x6 // file name
mov x2,#O_RDWR // flags
mov x3,#0 // mode
mov x8,#OPEN // call system OPEN
svc #0
cmp x0,#0 // open error ?
ble 99f
mov x19,x0 // save FD
// file read
ldr x1,qAdrsBufferRead // buffer address
mov x2,#BUFFSIZE // buffer size
mov x8,READ
svc 0
cmp x0,#0 // read error ?
ble 98f
mov x22,x0 // length read characters
mov x0,x19 // Fd
mov x8,CLOSE
svc 0
cmp x0,#0 // close error ?
blt 97f
ldr x0,qAdrsBufferRead
mov x1,x22 // length read characters
ldr x2,qAdrszBufferKey
mov x3,#LGBUFFERKEY
ldr x4,qAdrsBufferWrite
mov x5,x21 // and x5 contains E or D
bl traitement
// write output file
mov x0,AT_FDCWD
mov x1,x20 // file output name
mov x2,O_CREAT|O_RDWR // flags
ldr x3,qFicMask1
mov x8, #OPEN // call system open file
svc 0
cmp x0,#0 // create error ?
ble 96f
mov x19,x0 // file descriptor
ldr x1,qAdrsBufferWrite
mov x2,x22 // length read characters
mov x8, #WRITE // select system call 'write'
svc #0 // perform the system call
cmp x0,#0 // error write ?
blt 95f
mov x0,x19 // Fd output file
mov x8,CLOSE
svc 0
cmp x0,#0 // close error ?
blt 97f
ldr x0,qAdrszMessTrtOK // end message
bl affichageMess
b 100f
95: // errors
ldr x0,qAdrszMessErrorWrite
bl affichageMess
b 100f
96:
ldr x0,qAdrszMessErrorCreate
bl affichageMess
b 100f
97:
ldr x0,qAdrszMessErrorClose
bl affichageMess
b 100f
98:
ldr x0,qAdrszMessErrorRead
bl affichageMess
b 100f
99:
ldr x0,qAdrszMessErrorOpen
bl affichageMess
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc 0 // perform the system call
 
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessInst: .quad szMessInst
qAdrszMessCode: .quad szMessCode
qAdrsBufferRead: .quad sBufferRead
qAdrsBufferWrite: .quad sBufferWrite
qAdrszBufferKey: .quad szBufferKey
qAdrszMessErrorOpen: .quad szMessErrorOpen
qAdrszMessErrorRead: .quad szMessErrorRead
qAdrszMessErrorClose: .quad szMessErrorClose
qAdrszMessErrorWrite: .quad szMessErrorWrite
qAdrszMessErrorCreate: .quad szMessErrorCreate
qAdrszMessTrtOK: .quad szMessTrtOK
qFicMask1: .quad 0644
/******************************************************************/
/* encoding or decoding buffer */
/******************************************************************/
/* x0 contains input file address */
/* x1 contains length buffer */
/* x2 contanis key buffer address */
/* x3 contains key buffer length */
/* x4 contains output file address */
/* x5 contains code E or D */
traitement:
stp x5,lr,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
cmp x5,#'D' // code ?
beq decoding
mov x5,#0 // init indice
1: // loop read characters buffer
ldrb w6,[x0,x5] // load une character
sub x6,x6,#0x41 // conv ascii -> numeric
cmp x6,#0 // < A
blt 2f
cmp x6,#0x3A // > z
bgt 2f
ldrb w7,[x2,x6] // load key character at index
b 3f
2:
add x7,x6,#0x41 // conv numeric -> ascii
3:
strb w7,[x4,x5] // store encoded character in output buffer
add x5,x5,#1 // increment indice
cmp x5,x1 // end ?
ble 1b
b 100f
decoding:
mov x5,#0 // init indice
4:
ldrb w6,[x0,x5] // load one character
cmp x6,#0x41 // < A
blt 6f
cmp x6,#0x7A // > z
bgt 6f
mov x8,#0 // init key indice
5:
ldrb w7,[x2,x8] // load key character
cmp x7,x6 // compare character
add x9,x8,#0x41 // if equal convert indice to ascii
csel x7,x9,x7,eq
beq 7f
add x8,x8,#1 // else increment key indice
cmp x8,x3 // end key ?
ble 5b // no -> loop
6:
mov x7,x6 // move input character in output
7:
strb w7,[x4,x5] // store decoded character in output buffer
add x5,x5,#1 // increment indice
cmp x5,x1 // end buffer ?
ble 4b
100:
ldp x8,x9,[sp],16 // restaur registers
ldp x6,x7,[sp],16 // restaur registers
ldp x5,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
~/.../rosetta/asm4 $ subscipher64 input.txt output.txt E
Encoding/decoding OK.
~/.../rosetta/asm4 $ more input.txt
The quick brown fox jumps over the lazy dog, who barks VERY loudly!
~/.../rosetta/asm4 $ more output.txt
tFq oERJp wbQYh OQM AEDWL QSqb kFq nINT xQC, YFQ wIbpL PBZG nQExnT!
~/.../rosetta/asm4 $ subscipher64 output.txt outputdec.txt D
Encoding/decoding OK.
~/.../rosetta/asm4 $ more outputdec.txt
The quick brown fox jumps over the lazy dog, who barks VERY loudly!
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">
Line 99 ⟶ 336:
</syntaxhighlight>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # subsitiution cipher #
# abcdefghijklmnopqrstuvwxyz #
STRING substitute lower = "dthnxkmqrwzseglyoaubjpcfiv";
STRING substitute upper = "TKXMGVUPOIRFDEJZNYWCAQSLBH";
# ABCDEFGHIJKLMNOPQRSTUVWXYZ #
 
PROC encrypt = ( STRING plain text )STRING:
BEGIN
PROC encode = ( CHAR c, base, STRING code )CHAR:
code[ ( ABS c - ABS base ) + LWB code ];
STRING result := plain text;
FOR pos FROM LWB result TO UPB result DO
CHAR c = result[ pos ];
IF c >= "A" AND c <= "Z" THEN
result[ pos ] := encode( c, "A", substitute upper )
ELIF c >= "a" AND c <= "z" THEN
result[ pos ] := encode( c, "a", substitute lower )
FI
OD;
result
END # encrypt # ;
 
PROC decrypt = ( STRING cipher text )STRING:
BEGIN
PROC decode = ( CHAR c, base, STRING code )CHAR:
BEGIN
INT c pos := 0;
char in string( c, c pos, code );
REPR ( ABS base + ( c pos - 1 ) )
END # decode # ;
STRING result := cipher text;
FOR pos FROM LWB result TO UPB result DO
CHAR c = result[ pos ];
IF c >= "A" AND c <= "Z" THEN
result[ pos ] := decode( c, "A", substitute upper )
ELIF c >= "a" AND c <= "z" THEN
result[ pos ] := decode( c, "a", substitute lower )
FI
OD;
result
END # decrypt # ;
 
PROC test cipher = ( STRING plain text )VOID:
IF STRING encoded = encrypt( plain text );
STRING decoded = decrypt( encoded );
print( ( plain text, " -> ", encoded, newline ) );
print( ( encoded, " -> ", decoded, newline ) );
decoded /= plain text
THEN
print( ( "**** encode/decode problem", newline ) )
FI # test cipher # ;
 
test cipher( "Sphinx of Black Quartz, judge my vow" );
test cipher( "ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba" )
 
END
</syntaxhighlight>
{{out}}
<pre>
Sphinx of Black Quartz, judge my vow -> Wyqrgf lk Ksdhz Njdabv, wjnmx ei plc
Wyqrgf lk Ksdhz Njdabv, wjnmx ei plc -> Sphinx of Black Quartz, judge my vow
ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba -> TKXMGVUPOIRFDEJZNYWCAQSLBHvifcpjbuaoylgeszwrqmkxnhtd
TKXMGVUPOIRFDEJZNYWCAQSLBHvifcpjbuaoylgeszwrqmkxnhtd -> ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program subscipher.s */
 
/************************************/
/* Constantes */
/************************************/
/* for constantes see task include a file in arm assembly */
.include "../constantes.inc"
.equ BUFFSIZE, 50000 @ buffer size
.equ READ, 3
.equ OPEN, 5
.equ CLOSE, 6
.equ CREATE, 8
.equ O_RDWR, 0x0002 @ open for reading and writing
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessInst: .asciz "use : subscipher inputfile outpufile E (encryt) or D (decript).\n"
szMessCode: .asciz "Code operation not = E or D !!\n"
szMessErrorOpen: .asciz "Error open input file .\n"
szMessErrorCreate: .asciz "Error create output file.\n"
szMessErrorClose: .asciz "Error close file.\n"
szMessErrorRead: .asciz "Error read file.\n"
szMessErrorWrite: .asciz "Write error to output file.\n"
szMessTrtOK: .asciz "Encoding/decoding OK.\n"
szCarriageReturn: .asciz "\n"
// ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^ _`abcdefghijklmnopqrstuvwxyz
szBufferKey: .asciz "VsciBjedgrzyHalvXZKtUPumGf[\]^ _`IwJxqOCFRApnDhQWobLkESYMTN"
.equ LGBUFFERKEY, . - szBufferKey
/************************************/
/* UnInitialized data */
/************************************/
.bss
.align 4
iAdrFicInput: .skip 4
iAdrFicOutput: .skip 4
sBufferRead: .skip BUFFSIZE
sBufferWrite: .skip BUFFSIZE
/************************************/
/* code section */
/************************************/
.text
.global main
main:
mov fp,sp @ fp <- start address
ldr r4,[fp] @ number of Command line arguments
cmp r4,#4 @ test if number is ok
beq 1f
ldr r0,iAdrszMessInst @ no -> display error
bl affichageMess
b 100f
1:
ldr r6,[fp,#8] @ address input file name
ldr r10,[fp,#12] @ address output file name
ldr r5,[fp,#16] @ address code operation
ldrb r5,[r5] @ loaf first code character
cmp r5,#'E' @ control if code is OK
beq 2f
cmp r5,#'D'
beq 2f
ldr r0,iAdrszMessCode @ no -> display error
bl affichageMess
b 100f
2:
mov r0,r6 @ file name
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7,#OPEN @ call system OPEN
svc #0
cmp r0,#0 @ open error ?
ble 99f
mov r8,r0 @ save FD
// file read
ldr r1,iAdrsBufferRead @ buffer address
mov r2,#BUFFSIZE @ buffer size
mov r7, #READ @ call system READ
svc 0
cmp r0,#0 @ read error ?
ble 98f
mov r9,r0 @ length read characters
mov r0,r8 @ Fd
mov r7,#CLOSE @ call system CLOSE
svc 0
cmp r0,#0 @ close error ?
blt 97f
ldr r0,iAdrsBufferRead
mov r1,r9 @ length read characters
ldr r2,iAdrszBufferKey
mov r3,#LGBUFFERKEY
ldr r4,iAdrsBufferWrite
@ and r5 contains E or D
bl traitement
@ write output file
mov r0,r10 @ file output name
ldr r1,iFicMask1 @ flags
mov r7, #CREATE @ call system create file
svc 0
cmp r0,#0 @ create error ?
ble 96f
mov r8,r0 @ file descriptor
ldr r1,iAdrsBufferWrite
mov r2,r9 @ length read characters
mov r7, #WRITE @ select system call 'write'
svc #0 @ perform the system call
cmp r0,#0 @ error write ?
blt 95f
mov r0,r8 @ Fd output file
mov r7,#CLOSE @ call system CLOSE
svc 0
cmp r0,#0 @ close error ?
blt 97f
ldr r0,iAdrszMessTrtOK @ end message
bl affichageMess
b 100f
95: @ errors
ldr r0,iAdrszMessErrorWrite
bl affichageMess
b 100f
96:
ldr r0,iAdrszMessErrorCreate
bl affichageMess
b 100f
97:
ldr r0,iAdrszMessErrorClose
bl affichageMess
b 100f
98:
ldr r0,iAdrszMessErrorRead
bl affichageMess
b 100f
99:
ldr r0,iAdrszMessErrorOpen
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
 
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessInst: .int szMessInst
iAdrszMessCode: .int szMessCode
iAdrsBufferRead: .int sBufferRead
iAdrsBufferWrite: .int sBufferWrite
iAdrszBufferKey: .int szBufferKey
iAdrszMessErrorOpen: .int szMessErrorOpen
iAdrszMessErrorRead: .int szMessErrorRead
iAdrszMessErrorClose: .int szMessErrorClose
iAdrszMessErrorWrite: .int szMessErrorWrite
iAdrszMessErrorCreate: .int szMessErrorCreate
iAdrszMessTrtOK: .int szMessTrtOK
iFicMask1: .octa 0644
/******************************************************************/
/* encoding or decoding buffer */
/******************************************************************/
/* r0 contains input file address */
/* r1 contains length buffer */
/* r2 contanis key buffer address */
/* r3 contains key buffer length */
/* r4 contains output file address */
/* r5 contains code E or D */
traitement:
push {r6-r8,lr} @ save registres
cmp r5,#'D' @ code ?
beq decoding
mov r5,#0 @ init indice
1: @ loop read characters buffer
ldrb r6,[r0,r5] @ load une character
sub r6,#0x41 @ conv ascii -> numeric
cmp r6,#0 @ < A
blt 2f
cmp r6,#0x3A @ > z
bgt 2f
ldrb r7,[r2,r6] @ load key character at index
b 3f
2:
add r7,r6,#0x41 @ conv numeric -> ascii
3:
strb r7,[r4,r5] @ store encoded character in output buffer
add r5,r5,#1 @ increment indice
cmp r5,r1 @ end ?
ble 1b
b 100f
decoding:
mov r5,#0 @ init indice
4:
ldrb r6,[r0,r5] @ load one character
cmp r6,#0x41 @ < A
blt 6f
cmp r6,#0x7A @ > z
bgt 6f
mov r8,#0 @ init key indice
5:
ldrb r7,[r2,r8] @ load key character
cmp r7,r6 @ compare character
addeq r7,r8,#0x41 @ if equal convert indice to ascii
beq 7f
add r8,r8,#1 @ else increment key indice
cmp r8,r3 @ end key ?
ble 5b @ no -> loop
6:
mov r7,r6 @ move input character in output
7:
strb r7,[r4,r5] @ store decoded character in output buffer
add r5,r5,#1 @ increment indice
cmp r5,r1 @ end buffer ?
ble 4b
100:
pop {r6-r8,pc} @ restaur 2 registres
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"
 
 
</syntaxhighlight>
{{Out}}
<pre>
~/.../rosetta/asm4 $ subscipher input.txt output.txt E
Encoding/decoding OK.
~/.../rosetta/asm4 $ more input.txt
test phrase AZ 1234 az
~/.../rosetta/asm4 $ more output.txt
kqLk WFbILq Vf 1234 IN
~/.../rosetta/asm4 $ subscipher output.txt outputdec.txt D
Encoding/decoding OK.
~/.../rosetta/asm4 $ more outputdec.txt
test phrase AZ 1234 az
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">key: {:]kYV}(!7P$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs"v*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\C1yxJ:}
Line 126 ⟶ 664:
<pre>encoded: 2bu]E,KHm].Tdc|]4d\]),8M>]dQuT]<bu]U31C]Idf_]cbd].3Tm>]+ZzL]Ud,IUCk
decoded: The quick brown fox jumps over the lazy dog, who barks VERY loudly!</pre>
 
=={{Header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
alfabeto := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
codebeto := "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
textToEncode := "Encrypt an input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file. This type of Encryption/Decryption scheme is often called a Substitution Cipher."
 
loop,parse,textToEncode
{
posit := InStr(alfabeto,a_loopfield,1)
if posit
textEncoded .= substr(codebeto,posit,1)
else
textEncoded .= A_LoopField
}
msgbox % "ENCODED TEXT: " . textEncoded
 
loop,parse,textEncoded
{
posit := InStr(codebeto,a_loopfield,1)
if posit
textDecoded .= substr(alfabeto,posit,1)
else
textDecoded .= A_LoopField
}
msgbox % "DECODED TEXT: " . textDecoded
ExitApp
 
~Esc::
ExitApp
</syntaxhighlight>
{{Out}}
<pre>
Substitution Cipher.ahk
---------------------------
ENCODED TEXT: BhJbTWk Ih RhWEk/LQEbJq ORnq wT bqWnIJRhC qSqbT EWWqb/nQYqb JILq InWFIwqkL QO kFq LQEbJq ORnq YRkF IhQkFqb WbqxqkqbDRhqx EWWqb/nQYqb JILq InWFIwqkL Qb LTDwQnL Ihx LISq Rk RhkQ IhQkFqb QEkWEk/qhJbTWkqx ORnq Ihx kFqh ICIRh JQhSqbk kFIk QEkWEk/qhJbTWkqx ORnq RhkQ QbRCRhIn/xqJbTWkqx ORnq. tFRL kTWq QO BhJbTWkRQh/iqJbTWkRQh LJFqDq RL QOkqh JInnqx I KEwLkRkEkRQh cRWFqb.
---------------------------
DECODED TEXT: Encrypt an input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file. This type of Encryption/Decryption scheme is often called a Substitution Cipher.
---------------------------
</pre>
 
=={{header|C}}==
Line 544 ⟶ 1,122:
type of Encryption/Decryption scheme is often called a
Substitution Cipher.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
alpha$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
key$ = "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
#
proc subst in$ . out$ a$ b$ .
out$ = ""
for c$ in strchars in$
p = strpos a$ c$
if p > 0
c$ = substr b$ p 1
.
out$ &= c$
.
.
func$ enc s$ .
subst s$ r$ alpha$ key$
return r$
.
func$ dec s$ .
subst s$ r$ key$ alpha$
return r$
.
c$ = enc "Hello world"
print c$
print dec c$
</syntaxhighlight>
 
{{out}}
<pre>
dqnnQ YQbnx
Hello world
</pre>
 
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(setq alphabet "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
(setq code "odrmqbjnwzvueghlacyfpktisxODRMQBJNWZVUEGHLACYFPKTISX")
 
(defun encode (text)
"Encode TEXT with simple substitution code.
Each letter is replaced with the a random letter.
A specific letter in the original will always be replaced
by the same coded letter."
(let* ((case-fold-search nil)
(text-length (length text))
(encoded-text "")
(current-letter "")
(coded-letter "")
(alphabet-position))
(dotimes (i text-length)
(setq current-letter (substring text i (+ 1 i))) ; find the next letter in TEXT
(if (not (string-match-p "[a-zA-Z]" current-letter)) ; IF it's not a letter
(setq coded-letter current-letter) ; pass the non-letter without change
(setq alphabet-position (string-match current-letter alphabet)) ; ELSE find where the letter is in ALPHABET
(setq coded-letter (substring code alphabet-position (+ 1 alphabet-position)))) ; AND pass the coded letter
(setq encoded-text (concat encoded-text coded-letter))) ; IN ANY CASE, add new letter to ENCODED-TEXT
encoded-text))
 
(defun decode (text)
"Decode TEXT encoded with simple substitution code.
Each coded letter is replaced with the corresponding
uncoded letter."
(let* ((case-fold-search nil)
(text-length (length text))
(uncoded-text "")
(current-letter "")
(uncoded-letter "")
(code-position))
(dotimes (i text-length)
(setq current-letter (substring text i (+ 1 i))) ; find the next letter in TEXT
(if (not (string-match-p "[a-zA-Z]" current-letter)) ; IF it's not a letter
(setq uncoded-letter current-letter) ; pass the non-letter without change
(setq code-position (string-match current-letter code)) ; ELSE find where the letter is in CODE
(setq uncoded-letter (substring alphabet code-position (+ 1 code-position)))) ; AND pass the uncoded letter
(setq uncoded-text (concat uncoded-text uncoded-letter))) ; IN ANY CASE, add new letter to UNCODED-TEXT
uncoded-text))
 
</syntaxhighlight>
 
{{out}}
(encode "Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. ")
<pre>
"Rouu eq Wyneoqu. Yheq sqocy ojh—gqkqc ewgm nht uhgj lcqrwyqus—nokwgj
uwffuq hc gh ehgqs wg es lpcyq, ogm ghfnwgj locfwrpuoc fh wgfqcqyf eq
hg ynhcq, W fnhpjnf W thpum yowu odhpf o uwffuq ogm yqq fnq tofqcs locf
hb fnq thcum. "
</pre>
(decode "Rouu eq Wyneoqu. Yheq sqocy ojh—gqkqc ewgm nht uhgj lcqrwyqus—nokwgj
uwffuq hc gh ehgqs wg es lpcyq, ogm ghfnwgj locfwrpuoc fh wgfqcqyf eq
hg ynhcq, W fnhpjnf W thpum yowu odhpf o uwffuq ogm yqq fnq tofqcs locf
hb fnq thcum. ")
<pre>
"Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. "
</pre>
 
 
=={{header|Factor}}==
Line 2,135 ⟶ 2,817:
udzebras: .ascii "ECGHBIJKLMNOPQRSTDFUVWXYZA"
</syntaxhighlight>
 
=={{header|RPL}}==
"]kYV}(!7P\$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\πv*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
'CipherKey' STO
« ""
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB NUM 31 -
CipherKey SWAP DUP SUB +
'''NEXT''' SWAP DROP
» '<span style="color:blue">→CIPHER</span>' STO
« ""
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB
CipherKey SWAP POS 31 + CHR +
'''NEXT''' SWAP DROP
» '<span style="color:blue">CIPHER→</span>' STO
« "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
<span style="color:blue">→CIPHER</span> DUP <span style="color:blue">CIPHER→</span>
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: "2bu]E,KHm].Tdc|]4d\]),8M>]dQuT]<bu]U31C]Idf_]cbd].3Tm>]+ZzL]Ud,IUCk"
1: "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
</pre>
 
=={{header|Ruby}}==
Line 2,447 ⟶ 3,156:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var key = "]kYV}(!7P$n5_0i R:?jOWtF/=-pe'AD&@r6\%ZXs\"v*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
 
var encode = Fn.new { |s|
27

edits