Substitution cipher: Difference between revisions

m
no edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
mNo edit summary
 
(8 intermediate revisions by 4 users not shown)
Line 335:
end Cipher;
</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}}
Line 1,054 ⟶ 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,645 ⟶ 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}}==
29

edits