Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Caesar-cipher/00-META.yaml
Normal file
5
Task/Caesar-cipher/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
from: http://rosettacode.org/wiki/Caesar_cipher
|
||||
note: Encryption
|
||||
20
Task/Caesar-cipher/00-TASK.txt
Normal file
20
Task/Caesar-cipher/00-TASK.txt
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
;Task:
|
||||
Implement a [[wp:Caesar cipher|Caesar cipher]], both encoding and decoding. <br>
|
||||
The key is an integer from 1 to 25.
|
||||
|
||||
This cipher rotates (either towards left or right) the letters of the alphabet (A to Z).
|
||||
|
||||
The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A).
|
||||
<br>So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC".
|
||||
|
||||
This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys.
|
||||
|
||||
Caesar cipher is identical to [[Vigenère cipher]] with a key of length 1. <br>
|
||||
Also, [[Rot-13]] is identical to Caesar cipher with key 13.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Rot-13]]
|
||||
* [[Substitution Cipher]]
|
||||
* [[Vigenère Cipher/Cryptanalysis]]
|
||||
<br><br>
|
||||
20
Task/Caesar-cipher/11l/caesar-cipher.11l
Normal file
20
Task/Caesar-cipher/11l/caesar-cipher.11l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
F caesar(string, =key, decode = 0B)
|
||||
I decode
|
||||
key = 26 - key
|
||||
|
||||
V r = ‘ ’ * string.len
|
||||
L(c) string
|
||||
r[L.index] = S c
|
||||
‘a’..‘z’
|
||||
Char(code' (c.code - ‘a’.code + key) % 26 + ‘a’.code)
|
||||
‘A’..‘Z’
|
||||
Char(code' (c.code - ‘A’.code + key) % 26 + ‘A’.code)
|
||||
E
|
||||
c
|
||||
R r
|
||||
|
||||
V msg = ‘The quick brown fox jumped over the lazy dogs’
|
||||
print(msg)
|
||||
V enc = caesar(msg, 11)
|
||||
print(enc)
|
||||
print(caesar(enc, 11, decode' 1B))
|
||||
30
Task/Caesar-cipher/360-Assembly/caesar-cipher.360
Normal file
30
Task/Caesar-cipher/360-Assembly/caesar-cipher.360
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
* Caesar cypher 04/01/2019
|
||||
CAESARO PROLOG
|
||||
XPRNT PHRASE,L'PHRASE print phrase
|
||||
LH R3,OFFSET offset
|
||||
BAL R14,CYPHER call cypher
|
||||
LNR R3,R3 -offset
|
||||
BAL R14,CYPHER call cypher
|
||||
EPILOG
|
||||
CYPHER LA R4,REF(R3) @ref+offset
|
||||
MVC A,0(R4) for A to I
|
||||
MVC J,9(R4) for J to R
|
||||
MVC S,18(R4) for S to Z
|
||||
TR PHRASE,TABLE translate
|
||||
XPRNT PHRASE,L'PHRASE print phrase
|
||||
BR R14 return
|
||||
OFFSET DC H'22' between -25 and +25
|
||||
PHRASE DC CL37'THE FIVE BOXING WIZARDS JUMP QUICKLY'
|
||||
DC CL26'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
REF DC CL26'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
DC CL26'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
TABLE DC CL256' ' translate table for TR
|
||||
ORG TABLE+C'A'
|
||||
A DS CL9
|
||||
ORG TABLE+C'J'
|
||||
J DS CL9
|
||||
ORG TABLE+C'S'
|
||||
S DS CL8
|
||||
ORG
|
||||
YREGS
|
||||
END CAESARO
|
||||
23
Task/Caesar-cipher/8th/caesar-cipher.8th
Normal file
23
Task/Caesar-cipher/8th/caesar-cipher.8th
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
\ Ensure the output char is in the correct range:
|
||||
: modulate \ char base -- char
|
||||
tuck n:- 26 n:+ 26 n:mod n:+ ;
|
||||
|
||||
\ Symmetric Caesar cipher. Input is text and number of characters to advance
|
||||
\ (or retreat, if negative). That value should be in the range 1..26
|
||||
: caesar \ intext key -- outext
|
||||
>r
|
||||
(
|
||||
\ Ignore anything below '.' as punctuation:
|
||||
dup '. n:> if
|
||||
\ Do the conversion
|
||||
dup r@ n:+ swap
|
||||
\ Wrap appropriately
|
||||
'A 'Z between if 'A else 'a then modulate
|
||||
then
|
||||
) s:map rdrop ;
|
||||
|
||||
"The five boxing wizards jump quickly!"
|
||||
dup . cr
|
||||
1 caesar dup . cr
|
||||
-1 caesar . cr
|
||||
bye
|
||||
43
Task/Caesar-cipher/ALGOL-68/caesar-cipher.alg
Normal file
43
Task/Caesar-cipher/ALGOL-68/caesar-cipher.alg
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#!/usr/local/bin/a68g --script #
|
||||
|
||||
program caesar: BEGIN
|
||||
|
||||
MODE MODXXVI = SHORT SHORT INT; # MOD26 #
|
||||
|
||||
PROC to m26 = (CHAR c, offset)MODXXVI:
|
||||
BEGIN
|
||||
ABS c - ABS offset
|
||||
END #to m26#;
|
||||
|
||||
PROC to char = (MODXXVI value, CHAR offset)CHAR:
|
||||
BEGIN
|
||||
REPR ( ABS offset + value MOD 26 )
|
||||
END #to char#;
|
||||
|
||||
PROC encrypt = (STRING plain, MODXXVI key)STRING:
|
||||
BEGIN
|
||||
[UPB plain]CHAR ciph;
|
||||
FOR i TO UPB plain DO
|
||||
CHAR c = plain[i];
|
||||
ciph[i]:=
|
||||
IF "A" <= c AND c <= "Z" THEN
|
||||
to char(to m26(c, "A")+key, "A")
|
||||
ELIF "a" <= c AND c <= "z" THEN
|
||||
to char(to m26(c, "a")+key, "a")
|
||||
ELSE
|
||||
c
|
||||
FI
|
||||
OD;
|
||||
ciph
|
||||
END #encrypt#;
|
||||
|
||||
# caesar main program #
|
||||
STRING text := "The five boxing wizards jump quickly" # OR read string #;
|
||||
MODXXVI key := 3; # Default key from "Bello Gallico" #
|
||||
|
||||
printf(($gl$, "Plaintext ------------>" + text));
|
||||
text := encrypt(text, key);
|
||||
printf(($gl$, "Ciphertext ----------->" + text));
|
||||
printf(($gl$, "Decrypted Ciphertext ->" + encrypt(text, -key)))
|
||||
|
||||
END #caesar#
|
||||
7
Task/Caesar-cipher/APL/caesar-cipher.apl
Normal file
7
Task/Caesar-cipher/APL/caesar-cipher.apl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
∇CAESAR[⎕]∇
|
||||
∇
|
||||
[0] A←K CAESAR V
|
||||
[1] A←'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
|
||||
[2] ((,V∊A)/,V)←A[⎕IO+52|(2×K)+((A⍳,V)-⎕IO)~52]
|
||||
[3] A←V
|
||||
∇
|
||||
172
Task/Caesar-cipher/ARM-Assembly/caesar-cipher.arm
Normal file
172
Task/Caesar-cipher/ARM-Assembly/caesar-cipher.arm
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program caresarcode.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
.equ STRINGSIZE, 500
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessString: .asciz "String :\n"
|
||||
szMessEncrip: .asciz "\nEncrypted :\n"
|
||||
szMessDecrip: .asciz "\nDecrypted :\n"
|
||||
szString1: .asciz "Why study medicine because there is internet ?"
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
szString2: .skip STRINGSIZE
|
||||
szString3: .skip STRINGSIZE
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
ldr r0,iAdrszMessString @ display message
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1
|
||||
ldr r1,iAdrszString2
|
||||
mov r2,#20 @ key
|
||||
bl encrypt
|
||||
ldr r0,iAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString2 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString2
|
||||
ldr r1,iAdrszString3
|
||||
mov r2,#20 @ key
|
||||
bl decrypt
|
||||
ldr r0,iAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString3 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessString: .int szMessString
|
||||
iAdrszMessDecrip: .int szMessDecrip
|
||||
iAdrszMessEncrip: .int szMessEncrip
|
||||
iAdrszString1: .int szString1
|
||||
iAdrszString2: .int szString2
|
||||
iAdrszString3: .int szString2
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
/******************************************************************/
|
||||
/* encrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the string1 */
|
||||
/* r1 contains the address of the encrypted string */
|
||||
/* r2 contains the key (1-25) */
|
||||
encrypt:
|
||||
push {r3,r4,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string 1
|
||||
1:
|
||||
ldrb r4,[r0,r3] @ load byte string 1
|
||||
cmp r4,#0 @ zero final ?
|
||||
streqb r4,[r1,r3]
|
||||
moveq r0,r3
|
||||
beq 100f
|
||||
cmp r4,#65 @ < A ?
|
||||
strltb r4,[r1,r3]
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r4,#90 @ > Z
|
||||
bgt 2f
|
||||
add r4,r2 @ add key
|
||||
cmp r4,#90 @ > Z
|
||||
subgt r4,#26
|
||||
strb r4,[r1,r3]
|
||||
add r3,#1
|
||||
b 1b
|
||||
2:
|
||||
cmp r4,#97 @ < a ?
|
||||
strltb r4,[r1,r3]
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r4,#122 @> z
|
||||
strgtb r4,[r1,r3]
|
||||
addgt r3,#1
|
||||
bgt 1b
|
||||
add r4,r2
|
||||
cmp r4,#122
|
||||
subgt r4,#26
|
||||
strb r4,[r1,r3]
|
||||
add r3,#1
|
||||
b 1b
|
||||
|
||||
100:
|
||||
pop {r3,r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* decrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the encrypted string1 */
|
||||
/* r1 contains the address of the decrypted string */
|
||||
/* r2 contains the key (1-25) */
|
||||
decrypt:
|
||||
push {r3,r4,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string 1
|
||||
1:
|
||||
ldrb r4,[r0,r3] @ load byte string 1
|
||||
cmp r4,#0 @ zero final ?
|
||||
streqb r4,[r1,r3]
|
||||
moveq r0,r3
|
||||
beq 100f
|
||||
cmp r4,#65 @ < A ?
|
||||
strltb r4,[r1,r3]
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r4,#90 @ > Z
|
||||
bgt 2f
|
||||
sub r4,r2 @ substract key
|
||||
cmp r4,#65 @ < A
|
||||
addlt r4,#26
|
||||
strb r4,[r1,r3]
|
||||
add r3,#1
|
||||
b 1b
|
||||
2:
|
||||
cmp r4,#97 @ < a ?
|
||||
strltb r4,[r1,r3]
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r4,#122 @ > z
|
||||
strgtb r4,[r1,r3]
|
||||
addgt r3,#1
|
||||
bgt 1b
|
||||
sub r4,r2 @ substract key
|
||||
cmp r4,#97 @ < a
|
||||
addlt r4,#26
|
||||
strb r4,[r1,r3]
|
||||
add r3,#1
|
||||
b 1b
|
||||
|
||||
100:
|
||||
pop {r3,r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registers
|
||||
mov r2,#0 @ counter length */
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call system
|
||||
pop {r0,r1,r2,r7,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
42
Task/Caesar-cipher/AWK/caesar-cipher.awk
Normal file
42
Task/Caesar-cipher/AWK/caesar-cipher.awk
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
BEGIN {
|
||||
message = "My hovercraft is full of eels."
|
||||
key = 1
|
||||
|
||||
cypher = caesarEncode(key, message)
|
||||
clear = caesarDecode(key, cypher)
|
||||
|
||||
print "message: " message
|
||||
print " cypher: " cypher
|
||||
print " clear: " clear
|
||||
exit
|
||||
}
|
||||
|
||||
function caesarEncode(key, message) {
|
||||
return caesarXlat(key, message, "encode")
|
||||
}
|
||||
|
||||
function caesarDecode(key, message) {
|
||||
return caesarXlat(key, message, "decode")
|
||||
}
|
||||
|
||||
function caesarXlat(key, message, dir, plain, cypher, i, num, s) {
|
||||
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
cypher = substr(plain, key+1) substr(plain, 1, key)
|
||||
|
||||
if (toupper(substr(dir, 1, 1)) == "D") {
|
||||
s = plain
|
||||
plain = cypher
|
||||
cypher = s
|
||||
}
|
||||
|
||||
s = ""
|
||||
message = toupper(message)
|
||||
for (i = 1; i <= length(message); i++) {
|
||||
num = index(plain, substr(message, i, 1))
|
||||
if (num) s = s substr(cypher, num, 1)
|
||||
else s = s substr(message, i, 1)
|
||||
}
|
||||
return s
|
||||
}
|
||||
43
Task/Caesar-cipher/Action-/caesar-cipher.action
Normal file
43
Task/Caesar-cipher/Action-/caesar-cipher.action
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
CHAR FUNC Shift(CHAR c BYTE code)
|
||||
CHAR base
|
||||
|
||||
IF c>='a AND c<='z THEN
|
||||
base='a
|
||||
ELSEIF c>='A AND c<='Z THEN
|
||||
base='A
|
||||
ELSE
|
||||
RETURN (c)
|
||||
FI
|
||||
c==+code-base
|
||||
c==MOD 26
|
||||
RETURN (c+base)
|
||||
|
||||
PROC Encrypt(CHAR ARRAY in,out BYTE code)
|
||||
INT i
|
||||
|
||||
out(0)=in(0)
|
||||
FOR i=1 TO in(0)
|
||||
DO
|
||||
out(i)=Shift(in(i),code)
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Decrypt(CHAR ARRAY in,out BYTE code)
|
||||
Encrypt(in,out,26-code)
|
||||
RETURN
|
||||
|
||||
PROC Test(CHAR ARRAY in BYTE code)
|
||||
CHAR ARRAY enc(256),dec(256)
|
||||
|
||||
PrintE("Original:") PrintE(in)
|
||||
Encrypt(in,enc,code)
|
||||
PrintF("Encrypted code=%B:%E",code) PrintE(enc)
|
||||
Decrypt(enc,dec,code)
|
||||
PrintF("Decrypted code=%B:%E",code) PrintE(dec)
|
||||
PutE()
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("The quick brown fox jumps over the lazy dog.",23)
|
||||
Test("The quick brown fox jumps over the lazy dog.",5)
|
||||
RETURN
|
||||
45
Task/Caesar-cipher/Ada/caesar-cipher.ada
Normal file
45
Task/Caesar-cipher/Ada/caesar-cipher.ada
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Caesar is
|
||||
|
||||
type modulo26 is modulo 26;
|
||||
|
||||
function modulo26 (Character: Character; Output: Character) return modulo26 is
|
||||
begin
|
||||
return modulo26 (Character'Pos(Character)+Character'Pos(Output));
|
||||
end modulo26;
|
||||
|
||||
function Character(Val: in modulo26; Output: Character)
|
||||
return Character is
|
||||
begin
|
||||
return Character'Val(Integer(Val)+Character'Pos(Output));
|
||||
end Character;
|
||||
|
||||
function crypt (Playn: String; Key: modulo26) return String is
|
||||
Ciph: String(Playn'Range);
|
||||
|
||||
begin
|
||||
for I in Playn'Range loop
|
||||
case Playn(I) is
|
||||
when 'A' .. 'Z' =>
|
||||
Ciph(I) := Character(modulo26(Playn(I)+Key), 'A');
|
||||
when 'a' .. 'z' =>
|
||||
Ciph(I) := Character(modulo26(Playn(I)+Key), 'a');
|
||||
when others =>
|
||||
Ciph(I) := Playn(I);
|
||||
end case;
|
||||
end loop;
|
||||
return Ciph;
|
||||
end crypt;
|
||||
|
||||
Text: String := Ada.Text_IO.Get_Line;
|
||||
Key: modulo26 := 3; -- Default key from "Commentarii de Bello Gallico" shift cipher
|
||||
|
||||
begin -- encryption main program
|
||||
|
||||
Ada.Text_IO.Put_Line("Playn ------------>" & Text);
|
||||
Text := crypt(Text, Key);
|
||||
Ada.Text_IO.Put_Line("Ciphertext ----------->" & Text);
|
||||
Ada.Text_IO.Put_Line("Decrypted Ciphertext ->" & crypt(Text, -Key));
|
||||
|
||||
end Caesar;
|
||||
27
Task/Caesar-cipher/AppleScript/caesar-cipher-1.applescript
Normal file
27
Task/Caesar-cipher/AppleScript/caesar-cipher-1.applescript
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(* Only non-accented English letters are altered here. *)
|
||||
|
||||
on caesarDecipher(txt, |key|)
|
||||
return caesarEncipher(txt, -|key|)
|
||||
end caesarDecipher
|
||||
|
||||
on caesarEncipher(txt, |key|)
|
||||
set codePoints to id of txt
|
||||
set keyPlus25 to |key| mod 26 + 25
|
||||
repeat with thisCode in codePoints
|
||||
tell thisCode mod 32
|
||||
if ((it < 27) and (it > 0) and (thisCode div 64 is 1)) then ¬
|
||||
set thisCode's contents to thisCode - it + (it + keyPlus25) mod 26 + 1
|
||||
end tell
|
||||
end repeat
|
||||
|
||||
return string id codePoints
|
||||
end caesarEncipher
|
||||
|
||||
-- Test code:
|
||||
set txt to "ROMANES EUNT DOMUS!
|
||||
The quick brown fox jumps over the lazy dog."
|
||||
set |key| to 9
|
||||
|
||||
set enciphered to caesarEncipher(txt, |key|)
|
||||
set deciphered to caesarDecipher(enciphered, |key|)
|
||||
return "Text: '" & txt & ("'" & linefeed & "Key: " & |key| & linefeed & linefeed) & (enciphered & linefeed & deciphered)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
"Text: 'ROMANES EUNT DOMUS!
|
||||
The quick brown fox jumps over the lazy dog.'
|
||||
Key: 9
|
||||
|
||||
AXVJWNB NDWC MXVDB!
|
||||
Cqn zdrlt kaxfw oxg sdvyb xena cqn ujih mxp.
|
||||
ROMANES EUNT DOMUS!
|
||||
The quick brown fox jumps over the lazy dog."
|
||||
42
Task/Caesar-cipher/Applesoft-BASIC/caesar-cipher.basic
Normal file
42
Task/Caesar-cipher/Applesoft-BASIC/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
100 INPUT ""; T$
|
||||
|
||||
110 LET K% = RND(1) * 25 + 1
|
||||
120 PRINT "ENCODED WITH ";
|
||||
130 GOSUB 200ENCODED
|
||||
|
||||
140 LET K% = 26 - K%
|
||||
150 PRINT "DECODED WITH ";
|
||||
160 GOSUB 200DECODED
|
||||
|
||||
170 END
|
||||
|
||||
REM ENCODED/DECODED
|
||||
200 PRINT "CAESAR " K%;
|
||||
210 LET K$(1) = " (ROT-13)"
|
||||
220 PRINT K$(K% = 13)
|
||||
230 GOSUB 300CAESAR
|
||||
240 PRINT T$
|
||||
250 RETURN
|
||||
|
||||
REM CAESAR T$ K%
|
||||
300 FOR I = 1 TO LEN(T$)
|
||||
310 LET C$ = MID$(T$, I, 1)
|
||||
320 GOSUB 400ENCODE
|
||||
330 LET L = I - 1
|
||||
340 LET T$(0) = MID$(T$, 1, L)
|
||||
350 LET L = I + 1
|
||||
360 LET T$ = C$ + MID$(T$, L)
|
||||
370 LET T$ = T$(0) + T$
|
||||
380 NEXT I
|
||||
390 RETURN
|
||||
|
||||
REM ENCODE C$ K%
|
||||
400 LET C = ASC(C$)
|
||||
410 LET L = (C > 95) * 32
|
||||
420 LET C = C - L
|
||||
430 IF C < 65 THEN RETURN
|
||||
440 IF C > 90 THEN RETURN
|
||||
450 LET C = C + K%
|
||||
460 IF C > 90 THEN C = C - 26
|
||||
470 LET C$ = CHR$(C + L)
|
||||
480 RETURN
|
||||
20
Task/Caesar-cipher/Arc/caesar-cipher-1.arc
Normal file
20
Task/Caesar-cipher/Arc/caesar-cipher-1.arc
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(= rot (fn (L N)
|
||||
(if
|
||||
(and (<= 65 L) (>= 90 L))
|
||||
(do
|
||||
(= L (- L 65))
|
||||
(= L (mod (+ N L) 26))
|
||||
(= L (+ L 65)))
|
||||
(and (<= 97 L) (>= 122 L))
|
||||
(do
|
||||
(= L (- L 97))
|
||||
(= L (mod (+ N L) 26))
|
||||
(= L (+ L 97))))
|
||||
L))
|
||||
|
||||
(= caesar (fn (text (o shift))
|
||||
(unless shift (= shift 13))
|
||||
(= output (map [int _] (coerce text 'cons)))
|
||||
(= output (map [rot _ shift] output))
|
||||
(string output)
|
||||
))
|
||||
2
Task/Caesar-cipher/Arc/caesar-cipher-2.arc
Normal file
2
Task/Caesar-cipher/Arc/caesar-cipher-2.arc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(caesar "The quick brown fox jumps over the lazy dog.")
|
||||
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
|
||||
23
Task/Caesar-cipher/Arturo/caesar-cipher.arturo
Normal file
23
Task/Caesar-cipher/Arturo/caesar-cipher.arturo
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
ia: to :integer `a`
|
||||
iA: to :integer `A`
|
||||
lowAZ: `a`..`z`
|
||||
uppAZ: `A`..`Z`
|
||||
|
||||
caesar: function [s, xx][
|
||||
k: (not? null? attr 'decode)? -> 26-xx -> xx
|
||||
result: new ""
|
||||
loop s 'i [
|
||||
(in? i lowAZ)? -> 'result ++ to :char ia + (k + (to :integer i) - ia) % 26
|
||||
[
|
||||
(in? i uppAZ)? -> 'result ++ to :char iA + (k + (to :integer i) - iA) % 26
|
||||
-> 'result ++ i
|
||||
]
|
||||
]
|
||||
return result
|
||||
]
|
||||
|
||||
msg: "The quick brown fox jumped over the lazy dogs"
|
||||
print ["Original :" msg]
|
||||
enc: caesar msg 11
|
||||
print [" Encoded :" enc]
|
||||
print [" Decoded :" caesar.decode enc 11]
|
||||
13
Task/Caesar-cipher/Astro/caesar-cipher.astro
Normal file
13
Task/Caesar-cipher/Astro/caesar-cipher.astro
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
fun caesar(s, k, decode: false):
|
||||
if decode:
|
||||
k = 26 - k
|
||||
result = ''
|
||||
for i in s.uppercase() where 65 <= ord(i) <= 90:
|
||||
result.push! char(ord(i) - 65 + k) mod 26 + 65
|
||||
return result
|
||||
|
||||
let message = "The quick brown fox jumped over the lazy dogs"
|
||||
let encrypted = caesar(msg, 11)
|
||||
let decrypted = caesar(enc, 11, decode: true)
|
||||
|
||||
print(message, encrypted, decrypted, sep: '\n')
|
||||
6
Task/Caesar-cipher/AutoHotkey/caesar-cipher-1.ahk
Normal file
6
Task/Caesar-cipher/AutoHotkey/caesar-cipher-1.ahk
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
n=2
|
||||
s=HI
|
||||
t:=&s
|
||||
While *t
|
||||
o.=Chr(Mod(*t-65+n,26)+65),t+=2
|
||||
MsgBox % o
|
||||
13
Task/Caesar-cipher/AutoHotkey/caesar-cipher-2.ahk
Normal file
13
Task/Caesar-cipher/AutoHotkey/caesar-cipher-2.ahk
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Caesar(string, n){
|
||||
Loop Parse, string
|
||||
{
|
||||
If (Asc(A_LoopField) >= Asc("A") and Asc(A_LoopField) <= Asc("Z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("A")+n,26)+Asc("A"))
|
||||
Else If (Asc(A_LoopField) >= Asc("a") and Asc(A_LoopField) <= Asc("z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("a")+n,26)+Asc("a"))
|
||||
Else out .= A_LoopField
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
MsgBox % Caesar("h i", 2) "`n" Caesar("Hi", 20)
|
||||
32
Task/Caesar-cipher/AutoIt/caesar-cipher.autoit
Normal file
32
Task/Caesar-cipher/AutoIt/caesar-cipher.autoit
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
$Caesar = Caesar("Hi", 2, True)
|
||||
MsgBox(0, "Caesar", $Caesar)
|
||||
Func Caesar($String, $int, $encrypt = True)
|
||||
If Not IsNumber($int) Or Not StringIsDigit($int) Then Return SetError(1, 0, 0)
|
||||
If $int < 1 Or $int > 25 Then Return SetError(2, 0, 0)
|
||||
Local $sLetters, $x
|
||||
$String = StringUpper($String)
|
||||
$split = StringSplit($String, "")
|
||||
For $i = 1 To $split[0]
|
||||
If Asc($split[$i]) - 64 > 26 Or Asc($split[$i]) - 64 < 1 Then
|
||||
$sLetters &= $split[$i]
|
||||
ContinueLoop
|
||||
EndIf
|
||||
If $encrypt = True Then
|
||||
$move = Asc($split[$i]) - 64 + $int
|
||||
Else
|
||||
$move = Asc($split[$i]) - 64 - $int
|
||||
EndIf
|
||||
If $move > 26 Then
|
||||
$move -= 26
|
||||
ElseIf $move < 1 Then
|
||||
$move += 26
|
||||
EndIf
|
||||
While $move
|
||||
$x = Mod($move, 26)
|
||||
If $x = 0 Then $x = 26
|
||||
$sLetters &= Chr($x + 64)
|
||||
$move = ($move - $x) / 26
|
||||
WEnd
|
||||
Next
|
||||
Return $sLetters
|
||||
EndFunc ;==>Caesar
|
||||
30
Task/Caesar-cipher/BASIC256/caesar-cipher.basic
Normal file
30
Task/Caesar-cipher/BASIC256/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Caeser Cipher
|
||||
# basic256 1.1.4.0
|
||||
|
||||
dec$ = ""
|
||||
type$ = "cleartext "
|
||||
|
||||
input "If decrypting enter " + "<d> " + " -- else press enter > ",dec$ # it's a klooj I know...
|
||||
input "Enter offset > ", iOffset
|
||||
|
||||
if dec$ = "d" then
|
||||
iOffset = 26 - iOffset
|
||||
type$ = "ciphertext "
|
||||
end if
|
||||
|
||||
input "Enter " + type$ + "> ", str$
|
||||
|
||||
str$ = upper(str$) # a bit of a cheat really, however old school encryption is always upper case
|
||||
len = length(str$)
|
||||
|
||||
for i = 1 to len
|
||||
iTemp = asc(mid(str$,i,1))
|
||||
|
||||
if iTemp > 64 AND iTemp < 91 then
|
||||
iTemp = ((iTemp - 65) + iOffset) % 26
|
||||
print chr(iTemp + 65);
|
||||
else
|
||||
print chr(iTemp);
|
||||
end if
|
||||
|
||||
next i
|
||||
22
Task/Caesar-cipher/BBC-BASIC/caesar-cipher.basic
Normal file
22
Task/Caesar-cipher/BBC-BASIC/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
plaintext$ = "Pack my box with five dozen liquor jugs"
|
||||
PRINT plaintext$
|
||||
|
||||
key% = RND(25)
|
||||
cyphertext$ = FNcaesar(plaintext$, key%)
|
||||
PRINT cyphertext$
|
||||
|
||||
decyphered$ = FNcaesar(cyphertext$, 26-key%)
|
||||
PRINT decyphered$
|
||||
|
||||
END
|
||||
|
||||
DEF FNcaesar(text$, key%)
|
||||
LOCAL I%, C%
|
||||
FOR I% = 1 TO LEN(text$)
|
||||
C% = ASC(MID$(text$,I%))
|
||||
IF (C% AND &1F) >= 1 AND (C% AND &1F) <= 26 THEN
|
||||
C% = (C% AND &E0) OR (((C% AND &1F) + key% - 1) MOD 26 + 1)
|
||||
MID$(text$, I%, 1) = CHR$(C%)
|
||||
ENDIF
|
||||
NEXT
|
||||
= text$
|
||||
2
Task/Caesar-cipher/BQN/caesar-cipher-1.bqn
Normal file
2
Task/Caesar-cipher/BQN/caesar-cipher-1.bqn
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
o ← @‿'A'‿@‿'a'‿@ ⋄ m ← 5⥊↕2 ⋄ p ← m⊏∞‿26
|
||||
Rot ← {i←⊑"A[a{"⍋𝕩 ⋄ i⊑o+p|(𝕨×m)+𝕩-o}⎉0
|
||||
1
Task/Caesar-cipher/BQN/caesar-cipher-2.bqn
Normal file
1
Task/Caesar-cipher/BQN/caesar-cipher-2.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
3 Rot "We're no strangers to love // You know the rules and so do I"
|
||||
21
Task/Caesar-cipher/BaCon/caesar-cipher.bacon
Normal file
21
Task/Caesar-cipher/BaCon/caesar-cipher.bacon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
CONST lc$ = "abcdefghijklmnopqrstuvwxyz"
|
||||
CONST uc$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
CONST txt$ = "The quick brown fox jumps over the lazy dog."
|
||||
|
||||
FUNCTION Ceasar$(t$, k)
|
||||
|
||||
lk$ = MID$(lc$ & lc$, k+1, 26)
|
||||
uk$ = MID$(uc$ & uc$, k+1, 26)
|
||||
|
||||
RETURN REPLACE$(t$, lc$ & uc$, lk$ & uk$, 2)
|
||||
|
||||
END FUNCTION
|
||||
|
||||
tokey = RANDOM(25)+1
|
||||
PRINT "Encrypting text with key: ", tokey
|
||||
|
||||
en$ = Ceasar$(txt$, tokey)
|
||||
|
||||
PRINT "Encrypted: ", en$
|
||||
PRINT "Decrypted: ", Ceasar$(en$, 26-tokey)
|
||||
52
Task/Caesar-cipher/Babel/caesar-cipher.pb
Normal file
52
Task/Caesar-cipher/Babel/caesar-cipher.pb
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
((main
|
||||
{"The quick brown fox jumps over the lazy dog.\n"
|
||||
dup <<
|
||||
|
||||
17 caesar_enc !
|
||||
dup <<
|
||||
|
||||
17 caesar_dec !
|
||||
<<})
|
||||
|
||||
(caesar_enc
|
||||
{ 2 take
|
||||
{ caesar_enc_loop ! }
|
||||
nest })
|
||||
|
||||
(caesar_enc_loop {
|
||||
give
|
||||
<- str2ar
|
||||
{({ dup is_upper ! }
|
||||
{ 0x40 -
|
||||
-> dup <-
|
||||
encrypt !
|
||||
0x40 + }
|
||||
{ dup is_lower ! }
|
||||
{ 0x60 -
|
||||
-> dup <-
|
||||
encrypt !
|
||||
0x60 + }
|
||||
{ 1 }
|
||||
{fnord})
|
||||
cond}
|
||||
eachar
|
||||
collect !
|
||||
ls2lf ar2str})
|
||||
|
||||
(collect { -1 take })
|
||||
|
||||
(encrypt { + 1 - 26 % 1 + })
|
||||
|
||||
(caesar_dec { <- 26 -> - caesar_enc ! })
|
||||
|
||||
(is_upper
|
||||
{ dup
|
||||
<- 0x40 cugt ->
|
||||
0x5b cult
|
||||
cand })
|
||||
|
||||
(is_lower
|
||||
{ dup
|
||||
<- 0x60 cugt ->
|
||||
0x7b cult
|
||||
cand }))
|
||||
33
Task/Caesar-cipher/Beads/caesar-cipher.beads
Normal file
33
Task/Caesar-cipher/Beads/caesar-cipher.beads
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
beads 1 program 'Caesar cipher'
|
||||
calc main_init
|
||||
var str = "The five boxing wizards (🤖) jump quickly."
|
||||
log "Plain: {str}"
|
||||
str = Encrypt(str, 3)
|
||||
log "Encrypted: {str}"
|
||||
str = Decrypt(str, 3)
|
||||
log "Decrypted: {str}"
|
||||
|
||||
// encrypt a string by shifting the letters over by a number of slots
|
||||
// pass through any characters that are not in the Roman alphabet
|
||||
calc Encrypt(
|
||||
input:str --- string to encrypt
|
||||
nshift --- number of characters to slide over
|
||||
) : str --- encrypted string
|
||||
|
||||
var newStr = ""
|
||||
loop from:1 to:str_len(input) count:myCount
|
||||
var unicode : num = from_char(subset(input, from:myCount, len:1))
|
||||
if unicode >= 65 and unicode <= 90
|
||||
unicode = mod((unicode - 65 + nshift), 26) + 65
|
||||
elif unicode >= 97 and unicode <= 122
|
||||
unicode = mod((unicode - 97 + nshift), 26) + 97
|
||||
newStr = newStr & to_char(unicode)
|
||||
return newStr
|
||||
|
||||
// undo the encryption
|
||||
calc Decrypt(
|
||||
input:str
|
||||
nshift
|
||||
) : str
|
||||
// we could also just shift by 26-nshift, same as going in reverse
|
||||
return Encrypt(input, -nshift)
|
||||
3
Task/Caesar-cipher/Befunge/caesar-cipher-1.bf
Normal file
3
Task/Caesar-cipher/Befunge/caesar-cipher-1.bf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<<
|
||||
"`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`
|
||||
**-"A"-::0\`\55*`+#^_\0g+"4"+4^>\`*48
|
||||
3
Task/Caesar-cipher/Befunge/caesar-cipher-2.bf
Normal file
3
Task/Caesar-cipher/Befunge/caesar-cipher-2.bf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<<
|
||||
"`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`
|
||||
**-"A"-::0\`\55*`+#^_\0g-"4"+4^>\`*48
|
||||
217
Task/Caesar-cipher/Brainf---/caesar-cipher-1.bf
Normal file
217
Task/Caesar-cipher/Brainf---/caesar-cipher-1.bf
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
Author: Ettore Forigo | Hexwell
|
||||
|
||||
+ start the key input loop
|
||||
[
|
||||
memory: | c | 0 | cc | key |
|
||||
^
|
||||
|
||||
, take one character of the key
|
||||
|
||||
:c : condition for further ifs
|
||||
!= ' ' (subtract 32 (ascii for ' '))
|
||||
--------------------------------
|
||||
|
||||
(testing for the condition deletes it so duplication is needed)
|
||||
[>>+<+<-] duplicate
|
||||
> |
|
||||
[<+>-] |
|
||||
> | :cc : copy of :c
|
||||
|
||||
[ if :cc
|
||||
|
|
||||
> | :key : already converted digits
|
||||
|
|
||||
[>++++++++++<-] | multiply by 10
|
||||
> | |
|
||||
[<+>-] | |
|
||||
< | |
|
||||
|
|
||||
< | :cc
|
||||
[-] | clear (making the loop an if)
|
||||
] |
|
||||
|
||||
<< :c
|
||||
|
||||
[>>+<+<-] duplicate
|
||||
> |
|
||||
[<+>-] |
|
||||
> | :cc
|
||||
|
||||
[ if :cc
|
||||
---------------- | subtract 16 (difference between ascii for ' ' (already subtracted before) and ascii for '0'
|
||||
| making '0' 0; '1' 1; etc to transform ascii to integer)
|
||||
|
|
||||
[>+<-] | move/add :cc to :key
|
||||
] |
|
||||
|
||||
<< :c
|
||||
]
|
||||
|
||||
memory: | 0 | 0 | 0 | key |
|
||||
^
|
||||
|
||||
>>> :key
|
||||
|
||||
[<<<+>>>-] move key
|
||||
|
||||
memory: | key | 0 | 0 | 0 |
|
||||
^
|
||||
< ^
|
||||
+ start the word input loop
|
||||
[
|
||||
memory: | key | 0 | c | 0 | cc |
|
||||
^
|
||||
|
||||
, take one character of the word
|
||||
|
||||
:c : condition for further if
|
||||
!= ' ' (subtract 32 (ascii for ' '))
|
||||
--------------------------------
|
||||
|
||||
[>>+<+<-] duplicate
|
||||
> |
|
||||
[<+>-] |
|
||||
> | :cc : copy of :c
|
||||
|
||||
[ if :cc
|
||||
| subtract 65 (difference between ascii for ' ' (already subtracted before) and ascii for 'a'; making a 0; b 1; etc)
|
||||
-----------------------------------------------------------------
|
||||
|
|
||||
<<<< | :key
|
||||
|
|
||||
[>>>>+<<<+<-] | add :key to :cc := :shifted
|
||||
> | |
|
||||
[<+>-] | |
|
||||
|
|
||||
| memory: | key | 0 | c | 0 | cc/shifted | 0 | 0 | 0 | 0 | 0 | divisor |
|
||||
| ^
|
||||
|
|
||||
>>>>>>>>> | :divisor
|
||||
++++++++++++++++++++++++++ | = 26
|
||||
|
|
||||
<<<<<< | :shifted
|
||||
|
|
||||
| memory: | shifted/remainder | 0 | 0 | 0 | 0 | 0 | divisor |
|
||||
| ^
|
||||
|
|
||||
| shifted % divisor
|
||||
[ | | while :remainder
|
||||
| | |
|
||||
| | | memory: | shifted | 0 | 0 | 0 | 0 | 0 | divisor | 0 |
|
||||
| | | ^
|
||||
| | |
|
||||
[>>>>>>>+<<<<+<<<-] | | | duplicate :cshifted : copy of shifted
|
||||
| | |
|
||||
| | | memory: | 0 | 0 | 0 | shifted | 0 | 0 | divisor | cshifted |
|
||||
| | | ^
|
||||
>>>>>> | | | :divisor ^
|
||||
| | |
|
||||
[<<+>+>-] | | | duplicate
|
||||
< | | | |
|
||||
[>+<-] | | | |
|
||||
< | | | | :cdiv : copy of divisor
|
||||
| | |
|
||||
| | | memory: | 0 | 0 | 0 | shifted | cdiv | 0 | divisor | cshifted |
|
||||
| | | ^
|
||||
| | |
|
||||
| | | subtract :cdiv from :shifted until :shifted is 0
|
||||
[ | | | | while :cdiv
|
||||
< | | | | | :shifted
|
||||
| | | | |
|
||||
[<<+>+>-] | | | | | duplicate
|
||||
< | | | | | |
|
||||
[>+<-] | | | | | |
|
||||
< | | | | | | :csh
|
||||
| | | | |
|
||||
| | | | | memory: | 0 | csh | 0 | shifted/remainder | cdiv | 0 | divisor | cshifted |
|
||||
| | | | | ^
|
||||
| | | | |
|
||||
| | | | | subtract 1 from :shifted if not 0
|
||||
[ | | | | | | if :csh
|
||||
>>-<< | | | | | | | subtract 1 from :shifted
|
||||
[-] | | | | | | | clear
|
||||
] | | | | | | |
|
||||
| | | | |
|
||||
>>> | | | | | :cdiv
|
||||
- | | | | | subtract 1
|
||||
] | | | | |
|
||||
| | |
|
||||
| | |
|
||||
| | | memory: | 0 | 0 | 0 | remainder | 0 | 0 | divisor | cshifted |
|
||||
| | | ^
|
||||
< | | | :remainder ^
|
||||
| | |
|
||||
[>+<<<<+>>>-] | | | duplicate
|
||||
| | |
|
||||
| | | memory: | remainder | 0 | 0 | 0 | crem | 0 | divisor | shifted/modulus |
|
||||
| | | ^
|
||||
> | | | :crem ^
|
||||
| | |
|
||||
| | | clean up modulus if a remainder is left
|
||||
[ | | | if :crem
|
||||
>>>[-]<<< | | | | clear :modulus
|
||||
[-] | | | | clear
|
||||
] | | | |
|
||||
| | |
|
||||
| | | if subtracting :cdiv from :shifted left a remainder we need to do continue subtracting;
|
||||
| | | otherwise modulus is the modulus between :divisor and :shifted
|
||||
| | |
|
||||
<<<< | | | :remainder
|
||||
] | | |
|
||||
|
|
||||
| memory: | 0 | 0 | 0 | 0 | 0 | 0 | divisor | modulus | 0 | cmod | eq26 |
|
||||
| ^
|
||||
|
|
||||
>>>>>>> | :modulus
|
||||
|
|
||||
[>>+<+<-] | duplicate
|
||||
> | |
|
||||
[<+>-] | |
|
||||
> | | :cmod : copy of :modulus
|
||||
|
|
||||
| memory: | 0 | 0 | 0 | 0 | 0 | 0 | divisor | modulus | 0 | cmod | eq26 |
|
||||
| ^
|
||||
|
|
||||
-------------------------- | subtract 26
|
||||
|
|
||||
> | :eq26 : condition equal to 26
|
||||
+ | add 1 (set true)
|
||||
|
|
||||
< | :cmod
|
||||
[ | if :cmod not equal 26
|
||||
>-< | | subtract 1 from :eq26 (set false)
|
||||
[-] | | clear
|
||||
] | |
|
||||
|
|
||||
> | :eq26
|
||||
|
|
||||
[ | if :eq26
|
||||
<<<[-]>>> | | clear :modulus
|
||||
[-] | | clear
|
||||
] | |
|
||||
|
|
||||
| the modulus operation above gives 26 as a valid modulus; so this is a workaround for setting a
|
||||
| modulus value of 26 to 0
|
||||
|
|
||||
<<<< |
|
||||
[-] | clear :divisor
|
||||
|
|
||||
| memory: | c | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | modulus |
|
||||
| ^
|
||||
> | :modulus ^
|
||||
[<<<<<<<+>>>>>>>-] | move :modulus
|
||||
|
|
||||
| memory: | c | 0 | modulus/cc | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
|
||||
| ^
|
||||
<<<<<<< | :modulus/cc ^
|
||||
|
|
||||
| add 97 (ascii for 'a'; making 0 a; 1 b; etc)
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
. | print
|
||||
[-] | clear
|
||||
] |
|
||||
|
||||
memory: | c | 0 | modulus/cc |
|
||||
^
|
||||
<< :c ^
|
||||
]
|
||||
1
Task/Caesar-cipher/Brainf---/caesar-cipher-2.bf
Normal file
1
Task/Caesar-cipher/Brainf---/caesar-cipher-2.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 abc
|
||||
1
Task/Caesar-cipher/Brainf---/caesar-cipher-3.bf
Normal file
1
Task/Caesar-cipher/Brainf---/caesar-cipher-3.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
16 klm
|
||||
45
Task/Caesar-cipher/C++/caesar-cipher-1.cpp
Normal file
45
Task/Caesar-cipher/C++/caesar-cipher-1.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
class MyTransform {
|
||||
private :
|
||||
int shift ;
|
||||
public :
|
||||
MyTransform( int s ) : shift( s ) { }
|
||||
|
||||
char operator( )( char c ) {
|
||||
if ( isspace( c ) )
|
||||
return ' ' ;
|
||||
else {
|
||||
static std::string letters( "abcdefghijklmnopqrstuvwxyz" ) ;
|
||||
std::string::size_type found = letters.find(tolower( c )) ;
|
||||
int shiftedpos = ( static_cast<int>( found ) + shift ) % 26 ;
|
||||
if ( shiftedpos < 0 ) //in case of decryption possibly
|
||||
shiftedpos = 26 + shiftedpos ;
|
||||
char shifted = letters[shiftedpos] ;
|
||||
return shifted ;
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
||||
int main( ) {
|
||||
std::string input ;
|
||||
std::cout << "Which text is to be encrypted ?\n" ;
|
||||
getline( std::cin , input ) ;
|
||||
std::cout << "shift ?\n" ;
|
||||
int myshift = 0 ;
|
||||
std::cin >> myshift ;
|
||||
std::cout << "Before encryption:\n" << input << std::endl ;
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "encrypted:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
myshift *= -1 ; //decrypting again
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "Decrypted again:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
return 0 ;
|
||||
}
|
||||
54
Task/Caesar-cipher/C++/caesar-cipher-2.cpp
Normal file
54
Task/Caesar-cipher/C++/caesar-cipher-2.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* caesar cipher */
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
|
||||
int main( ) {
|
||||
|
||||
using namespace std;
|
||||
|
||||
string input ;
|
||||
int key = 0;
|
||||
|
||||
// lambda functions
|
||||
|
||||
auto encrypt = [&](char c, int key ) {
|
||||
char A = ( islower(c) )? 'a': 'A';
|
||||
c = (isalpha(c))? (c - A + key) % 26 + A : c;
|
||||
return (char) c;
|
||||
};
|
||||
|
||||
auto decrypt = [&](char c, int key ) {
|
||||
char A = ( islower(c) )? 'a': 'A';
|
||||
c = (isalpha(c))? (c - A + (26 - key) ) % 26 + A : c;
|
||||
return (char) c;
|
||||
};
|
||||
|
||||
|
||||
cout << "Enter a line of text.\n";
|
||||
getline( cin , input );
|
||||
|
||||
cout << "Enter an integer to shift text.\n";
|
||||
cin >> key;
|
||||
|
||||
while ( (key < 1) || (key > 25) )
|
||||
{
|
||||
cout << "must be an integer between 1 and 25 -->" << endl;
|
||||
cin >> key;
|
||||
}
|
||||
|
||||
cout << "Plain: \t" << input << endl ;
|
||||
|
||||
for ( auto & cp : input) // use & for mutability
|
||||
cp = encrypt(cp, key);
|
||||
|
||||
cout << "Encrypted:\t" << input << endl;
|
||||
|
||||
for ( auto & cp : input)
|
||||
cp = decrypt(cp, key);
|
||||
|
||||
cout << "Decrypted:\t" << input << endl;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
40
Task/Caesar-cipher/C-sharp/caesar-cipher.cs
Normal file
40
Task/Caesar-cipher/C-sharp/caesar-cipher.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace CaesarCypher
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static char Encrypt(char ch, int code)
|
||||
{
|
||||
if (!char.IsLetter(ch)) return ch;
|
||||
|
||||
char offset = char.IsUpper(ch) ? 'A' : 'a';
|
||||
return (char)((ch + code - offset) % 26 + offset);
|
||||
}
|
||||
|
||||
static string Encrypt(string input, int code)
|
||||
{
|
||||
return new string(input.Select(ch => Encrypt(ch, code)).ToArray());
|
||||
}
|
||||
|
||||
static string Decrypt(string input, int code)
|
||||
{
|
||||
return Encrypt(input, 26 - code);
|
||||
}
|
||||
|
||||
const string TestCase = "Pack my box with five dozen liquor jugs.";
|
||||
|
||||
static void Main()
|
||||
{
|
||||
string str = TestCase;
|
||||
|
||||
Console.WriteLine(str);
|
||||
str = Encrypt(str, 5);
|
||||
Console.WriteLine("Encrypted: " + str);
|
||||
str = Decrypt(str, 5);
|
||||
Console.WriteLine("Decrypted: " + str);
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Task/Caesar-cipher/C/caesar-cipher.c
Normal file
52
Task/Caesar-cipher/C/caesar-cipher.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define caesar(x) rot(13, x)
|
||||
#define decaesar(x) rot(13, x)
|
||||
#define decrypt_rot(x, y) rot((26-x), y)
|
||||
|
||||
void rot(int c, char *str)
|
||||
{
|
||||
int l = strlen(str);
|
||||
|
||||
const char* alpha_low = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
const char* alpha_high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
|
||||
char subst; /* substitution character */
|
||||
int idx; /* index */
|
||||
|
||||
int i; /* loop var */
|
||||
|
||||
for (i = 0; i < l; i++) /* for each letter in string */
|
||||
{
|
||||
if( 0 == isalpha(str[i]) ) continue; /* not alphabet character */
|
||||
|
||||
idx = (int) (tolower(str[i]) - 'a') + c) % 26; /* compute index */
|
||||
|
||||
if( isupper(str[i]) )
|
||||
subst = alpha_high[idx];
|
||||
else
|
||||
subst = alpha_low[idx];
|
||||
|
||||
str[i] = subst;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char str[] = "This is a top secret text message!";
|
||||
|
||||
printf("Original: %s\n", str);
|
||||
caesar(str);
|
||||
printf("Encrypted: %s\n", str);
|
||||
decaesar(str);
|
||||
printf("Decrypted: %s\n", str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
37
Task/Caesar-cipher/COBOL/caesar-cipher-1.cobol
Normal file
37
Task/Caesar-cipher/COBOL/caesar-cipher-1.cobol
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
identification division.
|
||||
program-id. caesar.
|
||||
data division.
|
||||
1 msg pic x(50)
|
||||
value "The quick brown fox jumped over the lazy dog.".
|
||||
1 offset binary pic 9(4) value 7.
|
||||
1 from-chars pic x(52).
|
||||
1 to-chars pic x(52).
|
||||
1 tabl.
|
||||
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
|
||||
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
|
||||
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
procedure division.
|
||||
begin.
|
||||
display msg
|
||||
perform encrypt
|
||||
display msg
|
||||
perform decrypt
|
||||
display msg
|
||||
stop run
|
||||
.
|
||||
|
||||
encrypt.
|
||||
move tabl (1:52) to from-chars
|
||||
move tabl (1 + offset:52) to to-chars
|
||||
inspect msg converting from-chars
|
||||
to to-chars
|
||||
.
|
||||
|
||||
decrypt.
|
||||
move tabl (1 + offset:52) to from-chars
|
||||
move tabl (1:52) to to-chars
|
||||
inspect msg converting from-chars
|
||||
to to-chars
|
||||
.
|
||||
end program caesar.
|
||||
86
Task/Caesar-cipher/COBOL/caesar-cipher-2.cobol
Normal file
86
Task/Caesar-cipher/COBOL/caesar-cipher-2.cobol
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
>>SOURCE FORMAT IS FREE
|
||||
PROGRAM-ID. caesar-cipher.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION encrypt
|
||||
FUNCTION decrypt
|
||||
.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 plaintext PIC X(50).
|
||||
01 offset PIC 99.
|
||||
|
||||
01 encrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY "Enter a message to encrypt: " NO ADVANCING
|
||||
ACCEPT plaintext
|
||||
DISPLAY "Enter the amount to shift by: " NO ADVANCING
|
||||
ACCEPT offset
|
||||
|
||||
MOVE FUNCTION encrypt(offset, plaintext) TO encrypted-str
|
||||
DISPLAY "Encrypted: " encrypted-str
|
||||
DISPLAY "Decrypted: " FUNCTION decrypt(offset, encrypted-str)
|
||||
.
|
||||
END PROGRAM caesar-cipher.
|
||||
|
||||
|
||||
FUNCTION-ID. encrypt.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i PIC 9(3).
|
||||
|
||||
01 a PIC 9(3).
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 offset PIC 99.
|
||||
01 str PIC X(50).
|
||||
|
||||
01 encrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION USING offset, str RETURNING encrypted-str.
|
||||
MOVE str TO encrypted-str
|
||||
PERFORM VARYING i FROM 1 BY 1 UNTIL i > FUNCTION LENGTH(str)
|
||||
IF encrypted-str (i:1) IS NOT ALPHABETIC OR encrypted-str (i:1) = SPACE
|
||||
EXIT PERFORM CYCLE
|
||||
END-IF
|
||||
|
||||
IF encrypted-str (i:1) IS ALPHABETIC-UPPER
|
||||
MOVE FUNCTION ORD("A") TO a
|
||||
ELSE
|
||||
MOVE FUNCTION ORD("a") TO a
|
||||
END-IF
|
||||
|
||||
MOVE FUNCTION CHAR(FUNCTION MOD(FUNCTION ORD(encrypted-str (i:1))
|
||||
- a + offset, 26) + a)
|
||||
TO encrypted-str (i:1)
|
||||
END-PERFORM
|
||||
.
|
||||
END FUNCTION encrypt.
|
||||
|
||||
|
||||
FUNCTION-ID. decrypt.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION encrypt
|
||||
.
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 decrypt-offset PIC 99.
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 offset PIC 99.
|
||||
01 str PIC X(50).
|
||||
|
||||
01 decrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION USING offset, str RETURNING decrypted-str.
|
||||
SUBTRACT 26 FROM offset GIVING decrypt-offset
|
||||
MOVE FUNCTION encrypt(decrypt-offset, str) TO decrypted-str
|
||||
.
|
||||
END FUNCTION decrypt.
|
||||
25
Task/Caesar-cipher/Chipmunk-Basic/caesar-cipher.basic
Normal file
25
Task/Caesar-cipher/Chipmunk-Basic/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
10 rem Caesar cipher
|
||||
20 cls
|
||||
30 dec$ = ""
|
||||
40 type$ = "cleartext "
|
||||
50 print "If decrypting enter "+"<d> "+" -- else press enter "; : input dec$
|
||||
60 input "Enter offset > ",ioffset
|
||||
70 if dec$ = "d" then
|
||||
80 ioffset = 26-ioffset
|
||||
90 type$ = "ciphertext "
|
||||
100 endif
|
||||
110 print "Enter "+type$+"> ";
|
||||
120 input cad$
|
||||
130 cad$ = ucase$(cad$)
|
||||
140 longitud = len(cad$)
|
||||
150 for i = 1 to longitud
|
||||
160 itemp = asc(mid$(cad$,i,1))
|
||||
170 if itemp > 64 and itemp < 91 then
|
||||
180 itemp = ((itemp-65)+ioffset) mod 26
|
||||
190 print chr$(itemp+65);
|
||||
200 else
|
||||
210 print chr$(itemp);
|
||||
220 endif
|
||||
230 next i
|
||||
240 print
|
||||
250 end
|
||||
23
Task/Caesar-cipher/Clojure/caesar-cipher-1.clj
Normal file
23
Task/Caesar-cipher/Clojure/caesar-cipher-1.clj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(defn encrypt-character [offset c]
|
||||
(if (Character/isLetter c)
|
||||
(let [v (int c)
|
||||
base (if (>= v (int \a))
|
||||
(int \a)
|
||||
(int \A))
|
||||
offset (mod offset 26)] ;works with negative offsets too!
|
||||
(char (+ (mod (+ (- v base) offset) 26)
|
||||
base)))
|
||||
c))
|
||||
|
||||
(defn encrypt [offset text]
|
||||
(apply str (map #(encrypt-character offset %) text)))
|
||||
|
||||
(defn decrypt [offset text]
|
||||
(encrypt (- 26 offset) text))
|
||||
|
||||
|
||||
(let [text "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
enc (encrypt -1 text)]
|
||||
(print "Original text:" text "\n")
|
||||
(print "Encryption:" enc "\n")
|
||||
(print "Decryption:" (decrypt -1 enc) "\n"))
|
||||
7
Task/Caesar-cipher/Clojure/caesar-cipher-2.clj
Normal file
7
Task/Caesar-cipher/Clojure/caesar-cipher-2.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defn encode [k s]
|
||||
(let [f #(take 26 (drop %3 (cycle (range (int %1) (inc (int %2))))))
|
||||
a #(map char (concat (f \a \z %) (f \A \Z %)))]
|
||||
(apply str (replace (zipmap (a 0) (a k)) s))))
|
||||
|
||||
(defn decode [k s]
|
||||
(encode (- 26 k) s))
|
||||
10
Task/Caesar-cipher/CoffeeScript/caesar-cipher.coffee
Normal file
10
Task/Caesar-cipher/CoffeeScript/caesar-cipher.coffee
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
cipher = (msg, rot) ->
|
||||
msg.replace /([a-z|A-Z])/g, ($1) ->
|
||||
c = $1.charCodeAt(0)
|
||||
String.fromCharCode \
|
||||
if c >= 97
|
||||
then (c + rot + 26 - 97) % 26 + 97
|
||||
else (c + rot + 26 - 65) % 26 + 65
|
||||
|
||||
console.log cipher "Hello World", 2
|
||||
console.log cipher "azAz %^&*()", 3
|
||||
47
Task/Caesar-cipher/Commodore-BASIC/caesar-cipher.basic
Normal file
47
Task/Caesar-cipher/Commodore-BASIC/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
1 rem caesar cipher
|
||||
2 rem rosetta code
|
||||
10 print chr$(147);chr$(14);
|
||||
15 input "Enter a key value from 1 to 25";kv
|
||||
20 if kv<1 or kv>25 then print "Out of range.":goto 15
|
||||
25 gosub 1000
|
||||
30 print chr$(147);"Enter a message to translate."
|
||||
35 print:print "Press CTRL-Z when finished.":print
|
||||
40 mg$="":gosub 2000
|
||||
45 print chr$(147);"Processing...":gosub 3000
|
||||
50 print chr$(147);"The translated message is:"
|
||||
55 print:print cm$
|
||||
100 print:print "Do another one? ";
|
||||
110 get k$:if k$<>"y" and k$<>"n" then 110
|
||||
120 print k$:if k$="y" then goto 10
|
||||
130 end
|
||||
|
||||
1000 rem generate encoding table
|
||||
1010 ec$=""
|
||||
1015 rem lower case
|
||||
1020 for i=kv to 26:ec$=ec$+chr$(i+64):next i
|
||||
1021 for i=1 to kv-1:ec$=ec$+chr$(i+64):next i
|
||||
1025 rem upper case
|
||||
1030 for i=kv to 26:ec$=ec$+chr$(i+192):next i
|
||||
1031 for i=1 to kv-1:ec$=ec$+chr$(i+192):next i
|
||||
1099 return
|
||||
|
||||
2000 rem get user input routine
|
||||
2005 print chr$(18);" ";chr$(146);chr$(157);
|
||||
2010 get k$:if k$="" then 2010
|
||||
2012 if k$=chr$(13) then print " ";chr$(157);
|
||||
2015 print k$;
|
||||
2020 if k$=chr$(20) then mg$=left$(mg$,len(mg$)-1):goto 2040
|
||||
2025 if len(mg$)=255 or k$=chr$(26) then return
|
||||
2030 mg$=mg$+k$
|
||||
2040 goto 2005
|
||||
|
||||
3000 rem translate message
|
||||
3005 cm$=""
|
||||
3010 for i=1 to len(mg$)
|
||||
3015 c=asc(mid$(mg$,i,1))
|
||||
3020 if c<65 or (c>90 and c<193) or c>218 then cm$=cm$+chr$(c):goto 3030
|
||||
3025 if c>=65 and c<=90 then c=c-64
|
||||
3030 if c>=193 and c<=218 then c=(c-192)+26
|
||||
3035 cm$=cm$+mid$(ec$,c,1)
|
||||
3040 next i
|
||||
3050 return
|
||||
19
Task/Caesar-cipher/Common-Lisp/caesar-cipher-1.lisp
Normal file
19
Task/Caesar-cipher/Common-Lisp/caesar-cipher-1.lisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(defun encipher-char (ch key)
|
||||
(let* ((c (char-code ch)) (la (char-code #\a)) (ua (char-code #\A))
|
||||
(base (cond ((<= la c (char-code #\z)) la)
|
||||
((<= ua c (char-code #\Z)) ua)
|
||||
(nil))))
|
||||
(if base (code-char (+ (mod (+ (- c base) key) 26) base)) ch)))
|
||||
|
||||
(defun caesar-cipher (str key)
|
||||
(map 'string #'(lambda (c) (encipher-char c key)) str))
|
||||
|
||||
(defun caesar-decipher (str key) (caesar-cipher str (- key)))
|
||||
|
||||
(let* ((original-text "The five boxing wizards jump quickly")
|
||||
(key 3)
|
||||
(cipher-text (caesar-cipher original-text key))
|
||||
(recovered-text (caesar-decipher cipher-text key)))
|
||||
(format t " Original: ~a ~%" original-text)
|
||||
(format t "Encrypted: ~a ~%" cipher-text)
|
||||
(format t "Decrypted: ~a ~%" recovered-text))
|
||||
9
Task/Caesar-cipher/Common-Lisp/caesar-cipher-2.lisp
Normal file
9
Task/Caesar-cipher/Common-Lisp/caesar-cipher-2.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun caesar-encipher (s k)
|
||||
(map 'string #'(lambda (c) (z c k)) s))
|
||||
|
||||
(defun caesar-decipher (s k) (caesar-encipher s (- k)))
|
||||
|
||||
(defun z (h k &aux (c (char-code h))
|
||||
(b (or (when (<= 97 c 122) 97)
|
||||
(when (<= 65 c 90) 65))))
|
||||
(if b (code-char (+ (mod (+ (- c b) k) 26) b)) h))
|
||||
9
Task/Caesar-cipher/Common-Lisp/caesar-cipher-3.lisp
Normal file
9
Task/Caesar-cipher/Common-Lisp/caesar-cipher-3.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defconstant +a+ "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")
|
||||
|
||||
(defun caesar (txt offset)
|
||||
(map 'string
|
||||
#'(lambda (c)
|
||||
(if (find c +a+)
|
||||
(char +a+ (mod (+ (position c +a+) (* 2 offset)) 52))
|
||||
c))
|
||||
txt))
|
||||
11
Task/Caesar-cipher/Crystal/caesar-cipher.crystal
Normal file
11
Task/Caesar-cipher/Crystal/caesar-cipher.crystal
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class String
|
||||
ALPHABET = ("A".."Z").to_a
|
||||
|
||||
def caesar_cipher(num)
|
||||
self.tr(ALPHABET.join, ALPHABET.rotate(num).join)
|
||||
end
|
||||
end
|
||||
|
||||
# demo
|
||||
encrypted = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG".caesar_cipher(5)
|
||||
decrypted = encrypted.caesar_cipher(-5)
|
||||
58
Task/Caesar-cipher/Cubescript/caesar-cipher-1.cube
Normal file
58
Task/Caesar-cipher/Cubescript/caesar-cipher-1.cube
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
alias modn [ mod (+ (mod $arg1 $arg2) $arg2) $arg2 ]
|
||||
//Cubescript's built-in mod will fail on negative numbers
|
||||
|
||||
alias cipher [
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (+ (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
]
|
||||
|
||||
alias decipher [
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (- (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2 ) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
]
|
||||
4
Task/Caesar-cipher/Cubescript/caesar-cipher-2.cube
Normal file
4
Task/Caesar-cipher/Cubescript/caesar-cipher-2.cube
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>>> cipher "The Quick Brown Fox Jumps Over The Lazy Dog." 5
|
||||
> Ymj Vznhp Gwtbs Ktc Ozrux Tajw Ymj Qfed Itl.
|
||||
>>> decipher "Ymj Vznhp Gwtbs Ktc Ozrux Tajw Ymj Qfed Itl." 5
|
||||
> The Quick Brown Fox Jumps Over The Lazy Dog.
|
||||
22
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
22
Task/Caesar-cipher/D/caesar-cipher-1.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
S rot(S)(in S s, in int key) pure nothrow @safe
|
||||
if (isSomeString!S) {
|
||||
auto res = s.dup;
|
||||
|
||||
foreach (immutable i, ref c; res) {
|
||||
if ('a' <= c && c <= 'z')
|
||||
c = ((c - 'a' + key) % 26 + 'a');
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
c = ((c - 'A' + key) % 26 + 'A');
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void main() @safe {
|
||||
enum key = 3;
|
||||
immutable txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
}
|
||||
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
20
Task/Caesar-cipher/D/caesar-cipher-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.ascii;
|
||||
|
||||
void inplaceRot(char[] txt, in int key) pure nothrow {
|
||||
foreach (ref c; txt) {
|
||||
if (isLower(c))
|
||||
c = (c - 'a' + key) % 26 + 'a';
|
||||
else if (isUpper(c))
|
||||
c = (c - 'A' + key) % 26 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum key = 3;
|
||||
auto txt = "The five boxing wizards jump quickly".dup;
|
||||
writeln("Original: ", txt);
|
||||
txt.inplaceRot(key);
|
||||
writeln("Encrypted: ", txt);
|
||||
txt.inplaceRot(26 - key);
|
||||
writeln("Decrypted: ", txt);
|
||||
}
|
||||
17
Task/Caesar-cipher/D/caesar-cipher-3.d
Normal file
17
Task/Caesar-cipher/D/caesar-cipher-3.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.ascii, std.string, std.algorithm;
|
||||
|
||||
string rot(in string s, in int key) pure nothrow @safe {
|
||||
auto uppr = uppercase.dup.representation;
|
||||
bringToFront(uppr[0 .. key], uppr[key .. $]);
|
||||
auto lowr = lowercase.dup.representation;
|
||||
bringToFront(lowr[0 .. key], lowr[key .. $]);
|
||||
return s.translate(makeTrans(letters, assumeUTF(uppr ~ lowr)));
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum key = 3;
|
||||
immutable txt = "The five boxing wizards jump quickly";
|
||||
writeln("Original: ", txt);
|
||||
writeln("Encrypted: ", txt.rot(key));
|
||||
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
|
||||
}
|
||||
64
Task/Caesar-cipher/Dart/caesar-cipher.dart
Normal file
64
Task/Caesar-cipher/Dart/caesar-cipher.dart
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
class Caesar {
|
||||
int _key;
|
||||
|
||||
Caesar(this._key);
|
||||
|
||||
int _toCharCode(String s) {
|
||||
return s.charCodeAt(0);
|
||||
}
|
||||
|
||||
String _fromCharCode(int ch) {
|
||||
return new String.fromCharCodes([ch]);
|
||||
}
|
||||
|
||||
String _process(String msg, int offset) {
|
||||
StringBuffer sb=new StringBuffer();
|
||||
for(int i=0;i<msg.length;i++) {
|
||||
int ch=msg.charCodeAt(i);
|
||||
if(ch>=_toCharCode('A')&&ch<=_toCharCode('Z')) {
|
||||
sb.add(_fromCharCode(_toCharCode("A")+(ch-_toCharCode("A")+offset)%26));
|
||||
}
|
||||
else if(ch>=_toCharCode('a')&&ch<=_toCharCode('z')) {
|
||||
sb.add(_fromCharCode(_toCharCode("a")+(ch-_toCharCode("a")+offset)%26));
|
||||
} else {
|
||||
sb.add(msg[i]);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String encrypt(String msg) {
|
||||
return _process(msg, _key);
|
||||
}
|
||||
|
||||
String decrypt(String msg) {
|
||||
return _process(msg, 26-_key);
|
||||
}
|
||||
}
|
||||
|
||||
void trip(String msg) {
|
||||
Caesar cipher=new Caesar(10);
|
||||
|
||||
String enc=cipher.encrypt(msg);
|
||||
String dec=cipher.decrypt(enc);
|
||||
print("\"$msg\" encrypts to:");
|
||||
print("\"$enc\" decrypts to:");
|
||||
print("\"$dec\"");
|
||||
Expect.equals(msg,dec);
|
||||
}
|
||||
|
||||
main() {
|
||||
Caesar c2=new Caesar(2);
|
||||
print(c2.encrypt("HI"));
|
||||
Caesar c20=new Caesar(20);
|
||||
print(c20.encrypt("HI"));
|
||||
|
||||
// try a few roundtrips
|
||||
|
||||
trip("");
|
||||
trip("A");
|
||||
trip("z");
|
||||
trip("Caesar cipher");
|
||||
trip(".-:/\"\\!");
|
||||
trip("The Quick Brown Fox Jumps Over The Lazy Dog.");
|
||||
}
|
||||
26
Task/Caesar-cipher/Dyalect/caesar-cipher.dyalect
Normal file
26
Task/Caesar-cipher/Dyalect/caesar-cipher.dyalect
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
func Char.Encrypt(code) {
|
||||
if !this.IsLetter() {
|
||||
return this
|
||||
}
|
||||
var offset = (this.IsUpper() ? 'A' : 'a').Order()
|
||||
return Char((this.Order() + code - offset) % 26 + offset)
|
||||
}
|
||||
|
||||
func String.Encrypt(code) {
|
||||
var xs = []
|
||||
for c in this {
|
||||
xs.Add(c.Encrypt(code))
|
||||
}
|
||||
return String.Concat(values: xs)
|
||||
}
|
||||
|
||||
func String.Decrypt(code) {
|
||||
this.Encrypt(26 - code);
|
||||
}
|
||||
|
||||
var str = "Pack my box with five dozen liquor jugs."
|
||||
print(str)
|
||||
str = str.Encrypt(5)
|
||||
print("Encrypted: \(str)")
|
||||
str = str.Decrypt(5)
|
||||
print("Decrypted: \(str)")
|
||||
215
Task/Caesar-cipher/EDSAC-order-code/caesar-cipher.edsac
Normal file
215
Task/Caesar-cipher/EDSAC-order-code/caesar-cipher.edsac
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
[Caesar cipher for Rosetta Code.
|
||||
EDSAC program, Initial Orders 2.]
|
||||
|
||||
[Table for converting alphabetic position 0..25 to EDSAC code.
|
||||
The EDSAC code is in the high 5 bits.]
|
||||
T 54 K [access table via C parameter]
|
||||
P 56 F
|
||||
T 56 K
|
||||
AFBFCFDFEFFFGFHFIFJFKFLFMFNFOFPFQFRFSFTFUFVFWFXFYFZF
|
||||
|
||||
[Table for converting 5-bit EDSAC code to alphabetic position 0..25.
|
||||
Computed at run time (so the programmer doesn't need to know the EDSAC codes).
|
||||
32 entries; entry is -1 if the EDSAC code does not represent a letter.]
|
||||
T 53 K [access table via B parameter]
|
||||
P 82 F
|
||||
|
||||
[Subroutine to read string from input and
|
||||
store with char codes in high 5 bits.
|
||||
String is terminated by blank row of tape, which is stored.
|
||||
Input: 0F holds address of string in address field (bits 1..11).
|
||||
22 locations; workspace: 4D]
|
||||
T 114 K
|
||||
GKA3FT19@AFA20@T10@I5FA4DR16FT4DA4FUFE14@A21@G18@T5FA10@A2FE4@T5FEFUFK2048F
|
||||
|
||||
[Subroutine to print string with character codes in high 5 bits.
|
||||
String is terminated by blank row of tape, which is not printed.
|
||||
Input: 0F holds address of string in address field (bits 1..11).
|
||||
18 locations; orkspace: 4F]
|
||||
T 136 K
|
||||
GKA3FT16@AFA2@T5@AFU4FE10@A17@G15@O4FT4FA5@A2FG4@TFEFK2048F
|
||||
|
||||
[Define start of user message]
|
||||
T 47 K [access message via M parameter]
|
||||
P 350 F [address of message]
|
||||
|
||||
T 154 K
|
||||
G K
|
||||
[Constants]
|
||||
[0] P M [address of user message]
|
||||
[1] A B
|
||||
[2] A C
|
||||
[3] T B
|
||||
[4] P 25 F
|
||||
[5] P 26 F
|
||||
[6] P 31 F
|
||||
[7] K 2048 F [(1) letter shift (2) used in test for
|
||||
blank row of tape at end of message]
|
||||
[8] @ F [carriage return]
|
||||
[9] & F [line feed]
|
||||
[10] K 4096 F [null char]
|
||||
|
||||
[Constant messages. Each includes new line at the end.]
|
||||
[11] TF EF SF TF IF NF GF @F &F K4096F
|
||||
[21] P 11 @
|
||||
[22] EF NF TF EF RF !F MF EF SF SF AF GF EF @F &F K4096F
|
||||
[38] P 22 @
|
||||
[39] MF UF SF TF !F SF TF AF RF TF !F WF IF TF HF !F BF !F TF OF !F ZF @F &F K4096F
|
||||
[64] P 39 @
|
||||
|
||||
[Subroutine to convert EDSAC code to alphabetic position.
|
||||
Input: 4F holds EDSAC code in high 5 bits
|
||||
Output: 4F holds alphabetic position (0..25, or -1 if not a letter).
|
||||
Workspace: 5F]
|
||||
[65] A 3 F [make jump for return]
|
||||
T 75 @ [plant in code]
|
||||
A 4 F [load EDSAC code]
|
||||
R 512 F [shift code into address field]
|
||||
T 5 F [temp store]
|
||||
C 5 F [acc := address bits only]
|
||||
A 1 @ [make order to load alphabetic position]
|
||||
T 73 @ [plant in code]
|
||||
[73] A B [load alphabetic position]
|
||||
T 4 F [store in 4F]
|
||||
[75] E F [return]
|
||||
|
||||
[Subroutine to encipher or decipher a message by Caesar shift.
|
||||
Input: Message is accessed by the M parameter, and
|
||||
terminated by a blank row of tape.
|
||||
Output: 0F = error flag: 0 if OK, < 0 if error (bad message prefix)
|
||||
Workspace 4F.]
|
||||
[76] A 3 F [make jump for return]
|
||||
T 119 @ [plant in code]
|
||||
A 80 @ [load order to access first char]
|
||||
T 95 @ [plant in code]
|
||||
[80] A M [load first char of message]
|
||||
T 4 F [pass to subroutine]
|
||||
[82] A 82 @ [get alphabetic position]
|
||||
G 65 @
|
||||
A 4 F [load alphabetic position]
|
||||
U F [to 0F for use as shift]
|
||||
S 2 F [check it's not 0 or -1]
|
||||
G 118 @ [error exit if it is]
|
||||
T 1 F [clear acc]
|
||||
S F [load negative of shift]
|
||||
G 108 @ [jump to store first char
|
||||
and convert rest of message]
|
||||
[Here after skipping non-letter]
|
||||
[91] T 4 F [clear acc]
|
||||
[Here after converting letter]
|
||||
[92] A 95 @ [load order to read character]
|
||||
A 2 F [inc address to next character]
|
||||
T 95 @ [store back]
|
||||
[95] A M [load char from message (in top 5 bits)]
|
||||
E 100 @ [if >= 0 then not blank row]
|
||||
A 7 @ [if < 0, test for blank row]
|
||||
G 117 @ [jump out if so]
|
||||
S 7 @ [restore after test]
|
||||
[100] T 4 F [character to 4F for subroutine]
|
||||
[101] A 101 @ [for return from subroutine]
|
||||
G 65 @ [sets 4F := alphabetic position]
|
||||
A 4 F [to acc]
|
||||
G 91 @ [if < 0 then not a letter; don't change it]
|
||||
A F [apply Caesar shift]
|
||||
[Subtract 26 if required, so result is in range 0..25]
|
||||
S 5 @ [subtract 26]
|
||||
E 109 @ [skip next if result >= 0]
|
||||
[108] A 5 @ [add 26]
|
||||
[109] A 2 @ [make order to read EDSAC letter at that position]
|
||||
T 114 @ [plant in code]
|
||||
A 95 @ [load A order from above]
|
||||
A 14 C [add 'O F' to make T order]
|
||||
T 115 @ [plant in code]
|
||||
[114] A C [load enciphered letter]
|
||||
[115] T M [overwrite original letter]
|
||||
E 92 @ [loop back]
|
||||
[117] T F [flag = 0 for OK]
|
||||
[118] T F [flag to 0F: 0 if OK, < 0 if error]
|
||||
[119] E F [return with acc = 0]
|
||||
|
||||
[Subroutine to print encipered or deciphered message, plus new line]
|
||||
[120] A 3 F [make jump order for return]
|
||||
T 128 @ [plant in code]
|
||||
A @ [load address of message]
|
||||
T F [to 0F for print subroutine]
|
||||
[124] A 124 @
|
||||
G 136 F [print message, clears acc]
|
||||
O 8 @ [print new line]
|
||||
O 9 @
|
||||
[128] E F [return with acc = 0]
|
||||
|
||||
[Main routine]
|
||||
[129] O 7 @ [set letter shift]
|
||||
H 6 @ [mult reg has this value throughout;
|
||||
selects bits 1..5 of 17-bit value]
|
||||
[Build inverse table from direct table
|
||||
First initialize all 32 locations to -1]
|
||||
A 6 @ [work backwards]
|
||||
[132] A 3 @ [make T order for inverse table]
|
||||
T 135 @ [plant in code]
|
||||
S 2 F [make -1 in address field]
|
||||
[135] T B [store in inverse table]
|
||||
A 135 @ [get T order]
|
||||
S 2 F [dec address]
|
||||
S 3 @ [compare with start of table]
|
||||
E 132 @ [loop till done]
|
||||
[Now fill in entries by reversing direct table]
|
||||
T F [clear acc]
|
||||
A 4 @ [work backwards]
|
||||
[142] U 5 F [index in 5F]
|
||||
A 2 @ [make A order for direct table]
|
||||
T 145 @ [plant in code]
|
||||
[145] A C [load entry from direct table, code in top 5 bits]
|
||||
R 512 F [shift 11 right, 5-bit code to address field]
|
||||
T 4 F [temp store]
|
||||
C 4 F [pick out 5-bit code]
|
||||
A 3 @ [make T order for inverse table]
|
||||
T 152 @ [plant in code]
|
||||
A 5 F [load index]
|
||||
[152] T B [store in inverse table]
|
||||
A 5 F [load index again]
|
||||
S 2 F [update index]
|
||||
E 142 @ [loop back]
|
||||
[Here when inverse table is complete]
|
||||
[156] T F [clear acc]
|
||||
[157] A 21 @ [load address of "testing" message]
|
||||
T F [to 0F for print subroutine]
|
||||
[159] A 159 @
|
||||
G 136 F [print "testing message"]
|
||||
E 168 @ [jump to read from end of this file]
|
||||
[Loop back here to get message from user]
|
||||
[162] A 38 @ [load address of prompt]
|
||||
T F [to 0F for print subroutine]
|
||||
[164] A 164 @
|
||||
G 136 F [print prompt]
|
||||
O 10 @ [print null to flush teleprinter buffer]
|
||||
Z F [stop]
|
||||
[First time here, read message from end of this file.
|
||||
Later times, read message from file chosen by the user.]
|
||||
[168] A @ [load address of message]
|
||||
T F [to 0F for input]
|
||||
[170] A 170 @
|
||||
G 114 F [read message]
|
||||
[172] A 172 @
|
||||
G 120 @ [print message]
|
||||
[174] A 174 @
|
||||
G 76 @ [call Caesar shift subroutine]
|
||||
A F [load error flag]
|
||||
E 184 @ [jump if OK]
|
||||
T F [error, clear acc]
|
||||
A 64 @ [load address of error message]
|
||||
T F
|
||||
[181] A 181 @
|
||||
G 136 F [print error message]
|
||||
E 162 @ [back to try again]
|
||||
[Here if message was enciphered without error]
|
||||
[184] A 184 @
|
||||
G 120 @ [print enciphered message]
|
||||
[186] A 186 @
|
||||
G 76 @ [call Caesar shift subroutine]
|
||||
[188] A 188 @
|
||||
G 120 @ [print deciphered message]
|
||||
E 162 @ [back for another message]
|
||||
E 129 Z [define entry point]
|
||||
P F [acc = 0 on entry]
|
||||
DGAZA!FREQUENS!LIBYCUM!DUXIT!KARTHAGO!TRIUMPHUM.
|
||||
29
Task/Caesar-cipher/ERRE/caesar-cipher.erre
Normal file
29
Task/Caesar-cipher/ERRE/caesar-cipher.erre
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
PROGRAM CAESAR
|
||||
|
||||
!$INCLUDE="PC.LIB"
|
||||
|
||||
PROCEDURE CAESAR(TEXT$,KY%->CY$)
|
||||
LOCAL I%,C%
|
||||
FOR I%=1 TO LEN(TEXT$) DO
|
||||
C%=ASC(MID$(TEXT$,I%))
|
||||
IF (C% AND $1F)>=1 AND (C% AND $1F)<=26 THEN
|
||||
C%=(C% AND $E0) OR (((C% AND $1F)+KY%-1) MOD 26+1)
|
||||
CHANGE(TEXT$,I%,CHR$(C%)->TEXT$)
|
||||
END IF
|
||||
END FOR
|
||||
CY$=TEXT$
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
RANDOMIZE(TIMER)
|
||||
PLAINTEXT$="Pack my box with five dozen liquor jugs"
|
||||
PRINT(PLAINTEXT$)
|
||||
|
||||
KY%=1+INT(25*RND(1)) ! generates random between 1 and 25
|
||||
CAESAR(PLAINTEXT$,KY%->CYPHERTEXT$)
|
||||
PRINT(CYPHERTEXT$)
|
||||
|
||||
CAESAR(CYPHERTEXT$,26-KY%->DECYPHERED$)
|
||||
PRINT(DECYPHERED$)
|
||||
|
||||
END PROGRAM
|
||||
53
Task/Caesar-cipher/Eiffel/caesar-cipher.e
Normal file
53
Task/Caesar-cipher/Eiffel/caesar-cipher.e
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
s: STRING_32
|
||||
do
|
||||
s := "The tiny tiger totally taunted the tall Till."
|
||||
print ("%NString to encode: " + s)
|
||||
print ("%NEncoded string: " + encode (s, 12))
|
||||
print ("%NDecoded string (after encoding and decoding): " + decode (encode (s, 12), 12))
|
||||
end
|
||||
|
||||
feature -- Basic operations
|
||||
|
||||
decode (to_be_decoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Decode `to be decoded' according to `offset'.
|
||||
do
|
||||
Result := encode (to_be_decoded, 26 - offset)
|
||||
end
|
||||
|
||||
encode (to_be_encoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Encode `to be encoded' according to `offset'.
|
||||
local
|
||||
l_offset: INTEGER
|
||||
l_char_code: INTEGER
|
||||
do
|
||||
create Result.make_empty
|
||||
l_offset := (offset \\ 26) + 26
|
||||
across to_be_encoded as tbe loop
|
||||
if tbe.item.is_alpha then
|
||||
if tbe.item.is_upper then
|
||||
l_char_code := ('A').code + (tbe.item.code - ('A').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
else
|
||||
l_char_code := ('a').code + (tbe.item.code - ('a').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
end
|
||||
else
|
||||
Result.append_character (tbe.item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
28
Task/Caesar-cipher/Ela/caesar-cipher.ela
Normal file
28
Task/Caesar-cipher/Ela/caesar-cipher.ela
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
open number char monad io string
|
||||
|
||||
chars = "ABCDEFGHIJKLMOPQRSTUVWXYZ"
|
||||
|
||||
caesar _ _ [] = ""
|
||||
caesar op key (x::xs) = check shifted ++ caesar op key xs
|
||||
where orig = indexOf (string.upper $ toString x) chars
|
||||
shifted = orig `op` key
|
||||
check val | orig == -1 = x
|
||||
| val > 24 = trans $ val - 25
|
||||
| val < 0 = trans $ 25 + val
|
||||
| else = trans shifted
|
||||
trans idx = chars:idx
|
||||
|
||||
cypher = caesar (+)
|
||||
decypher = caesar (-)
|
||||
|
||||
key = 2
|
||||
|
||||
do
|
||||
putStrLn "A string to encode:"
|
||||
str <- readStr
|
||||
putStr "Encoded string: "
|
||||
cstr <- return <| cypher key str
|
||||
put cstr
|
||||
putStrLn ""
|
||||
putStr "Decoded string: "
|
||||
put $ decypher key cstr
|
||||
75
Task/Caesar-cipher/Elena/caesar-cipher.elena
Normal file
75
Task/Caesar-cipher/Elena/caesar-cipher.elena
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import system'routines;
|
||||
import system'math;
|
||||
import extensions;
|
||||
import extensions'text;
|
||||
|
||||
const string Letters = "abcdefghijklmnopqrstuvwxyz";
|
||||
const string BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const string TestText = "Pack my box with five dozen liquor jugs.";
|
||||
const int Key = 12;
|
||||
|
||||
class Encrypting : Enumerator
|
||||
{
|
||||
int theKey;
|
||||
Enumerator theEnumerator;
|
||||
|
||||
constructor(int key, string text)
|
||||
{
|
||||
theKey := key;
|
||||
theEnumerator := text.enumerator();
|
||||
}
|
||||
|
||||
bool next() => theEnumerator;
|
||||
|
||||
reset() => theEnumerator;
|
||||
|
||||
enumerable() => theEnumerator;
|
||||
|
||||
get()
|
||||
{
|
||||
var ch := theEnumerator.get();
|
||||
|
||||
var index := Letters.indexOf(0, ch);
|
||||
|
||||
if (-1 < index)
|
||||
{
|
||||
^ Letters[(theKey+index).mod:26]
|
||||
}
|
||||
else
|
||||
{
|
||||
index := BigLetters.indexOf(0, ch);
|
||||
if (-1 < index)
|
||||
{
|
||||
^ BigLetters[(theKey+index).mod:26]
|
||||
}
|
||||
else
|
||||
{
|
||||
^ ch
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension encryptOp
|
||||
{
|
||||
encrypt(key)
|
||||
= new Encrypting(key, self).summarize(new StringWriter());
|
||||
|
||||
decrypt(key)
|
||||
= new Encrypting(26 - key, self).summarize(new StringWriter());
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
console.printLine("Original text :",TestText);
|
||||
|
||||
var encryptedText := TestText.encrypt:Key;
|
||||
|
||||
console.printLine("Encrypted text:",encryptedText);
|
||||
|
||||
var decryptedText := encryptedText.decrypt:Key;
|
||||
|
||||
console.printLine("Decrypted text:",decryptedText);
|
||||
|
||||
console.readChar()
|
||||
}
|
||||
18
Task/Caesar-cipher/Elixir/caesar-cipher.elixir
Normal file
18
Task/Caesar-cipher/Elixir/caesar-cipher.elixir
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
defmodule Caesar_cipher do
|
||||
defp set_map(map, range, key) do
|
||||
org = Enum.map(range, &List.to_string [&1])
|
||||
{a, b} = Enum.split(org, key)
|
||||
Enum.zip(org, b ++ a) |> Enum.into(map)
|
||||
end
|
||||
|
||||
def encode(text, key) do
|
||||
map = Map.new |> set_map(?a..?z, key) |> set_map(?A..?Z, key)
|
||||
String.graphemes(text) |> Enum.map_join(fn c -> Map.get(map, c, c) end)
|
||||
end
|
||||
end
|
||||
|
||||
text = "The five boxing wizards jump quickly"
|
||||
key = 3
|
||||
IO.puts "Original: #{text}"
|
||||
IO.puts "Encrypted: #{enc = Caesar_cipher.encode(text, key)}"
|
||||
IO.puts "Decrypted: #{Caesar_cipher.encode(enc, -key)}"
|
||||
33
Task/Caesar-cipher/Erlang/caesar-cipher-1.erl
Normal file
33
Task/Caesar-cipher/Erlang/caesar-cipher-1.erl
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
%% Ceasar cypher in Erlang for the rosetta code wiki.
|
||||
%% Implemented by J.W. Luiten
|
||||
|
||||
-module(ceasar).
|
||||
-export([main/2]).
|
||||
|
||||
%% rot: rotate Char by Key places
|
||||
rot(Char,Key) when (Char >= $A) and (Char =< $Z) or
|
||||
(Char >= $a) and (Char =< $z) ->
|
||||
Offset = $A + Char band 32,
|
||||
N = Char - Offset,
|
||||
Offset + (N + Key) rem 26;
|
||||
rot(Char, _Key) ->
|
||||
Char.
|
||||
|
||||
%% key: normalize key.
|
||||
key(Key) when Key < 0 ->
|
||||
26 + Key rem 26;
|
||||
key(Key) when Key > 25 ->
|
||||
Key rem 26;
|
||||
key(Key) ->
|
||||
Key.
|
||||
|
||||
main(PlainText, Key) ->
|
||||
Encode = key(Key),
|
||||
Decode = key(-Key),
|
||||
|
||||
io:format("Plaintext ----> ~s~n", [PlainText]),
|
||||
|
||||
CypherText = lists:map(fun(Char) -> rot(Char, Encode) end, PlainText),
|
||||
io:format("Cyphertext ---> ~s~n", [CypherText]),
|
||||
|
||||
PlainText = lists:map(fun(Char) -> rot(Char, Decode) end, CypherText).
|
||||
1
Task/Caesar-cipher/Erlang/caesar-cipher-2.erl
Normal file
1
Task/Caesar-cipher/Erlang/caesar-cipher-2.erl
Normal file
|
|
@ -0,0 +1 @@
|
|||
ceasar:main("The five boxing wizards jump quickly", 3).
|
||||
54
Task/Caesar-cipher/Euphoria/caesar-cipher.euphoria
Normal file
54
Task/Caesar-cipher/Euphoria/caesar-cipher.euphoria
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
--caesar cipher for Rosetta Code wiki
|
||||
--User:Lnettnay
|
||||
|
||||
--usage eui caesar ->default text, key and encode flag
|
||||
--usage eui caesar 'Text with spaces and punctuation!' 5 D
|
||||
--If text has imbedded spaces must use apostophes instead of quotes so all punctuation works
|
||||
--key = integer from 1 to 25, defaults to 13
|
||||
--flag = E (Encode) or D (Decode), defaults to E
|
||||
--no error checking is done on key or flag
|
||||
|
||||
include std/get.e
|
||||
include std/types.e
|
||||
|
||||
|
||||
sequence cmd = command_line()
|
||||
sequence val
|
||||
-- default text for encryption
|
||||
sequence text = "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
atom key = 13 -- default to Rot-13
|
||||
sequence flag = "E" -- default to Encrypt
|
||||
atom offset
|
||||
atom num_letters = 26 -- number of characters in alphabet
|
||||
|
||||
--get text
|
||||
if length(cmd) >= 3 then
|
||||
text = cmd[3]
|
||||
end if
|
||||
|
||||
--get key value
|
||||
if length(cmd) >= 4 then
|
||||
val = value(cmd[4])
|
||||
key = val[2]
|
||||
end if
|
||||
|
||||
--get Encrypt/Decrypt flag
|
||||
if length(cmd) = 5 then
|
||||
flag = cmd[5]
|
||||
if compare(flag, "D") = 0 then
|
||||
key = 26 - key
|
||||
end if
|
||||
end if
|
||||
|
||||
for i = 1 to length(text) do
|
||||
if t_alpha(text[i]) then
|
||||
if t_lower(text[i]) then
|
||||
offset = 'a'
|
||||
else
|
||||
offset = 'A'
|
||||
end if
|
||||
text[i] = remainder(text[i] - offset + key, num_letters) + offset
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"%s\n",{text})
|
||||
13
Task/Caesar-cipher/F-Sharp/caesar-cipher.fs
Normal file
13
Task/Caesar-cipher/F-Sharp/caesar-cipher.fs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
module caesar =
|
||||
open System
|
||||
|
||||
let private cipher n s =
|
||||
let shift c =
|
||||
if Char.IsLetter c then
|
||||
let a = (if Char.IsLower c then 'a' else 'A') |> int
|
||||
(int c - a + n) % 26 + a |> char
|
||||
else c
|
||||
String.map shift s
|
||||
|
||||
let encrypt n = cipher n
|
||||
let decrypt n = cipher (26 - n)
|
||||
18
Task/Caesar-cipher/Factor/caesar-cipher.factor
Normal file
18
Task/Caesar-cipher/Factor/caesar-cipher.factor
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
USING: io kernel locals math sequences unicode.categories ;
|
||||
IN: rosetta-code.caesar-cipher
|
||||
|
||||
:: cipher ( n s -- s' )
|
||||
[| c |
|
||||
c Letter? [
|
||||
c letter? CHAR: a CHAR: A ? :> a
|
||||
c a - n + 26 mod a +
|
||||
]
|
||||
[ c ] if
|
||||
] :> shift
|
||||
s [ shift call ] map ;
|
||||
|
||||
: encrypt ( n s -- s' ) cipher ;
|
||||
: decrypt ( n s -- s' ) [ 26 swap - ] dip cipher ;
|
||||
|
||||
11 "Esp bftnv mczhy qzi ufxapo zgpc esp wlkj ozr." decrypt print
|
||||
11 "The quick brown fox jumped over the lazy dog." encrypt print
|
||||
52
Task/Caesar-cipher/Fantom/caesar-cipher.fantom
Normal file
52
Task/Caesar-cipher/Fantom/caesar-cipher.fantom
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
class Main
|
||||
{
|
||||
static Int shift (Int char, Int key)
|
||||
{
|
||||
newChar := char + key
|
||||
if (char >= 'a' && char <= 'z')
|
||||
{
|
||||
if (newChar - 'a' < 0) { newChar += 26 }
|
||||
if (newChar - 'a' >= 26) { newChar -= 26 }
|
||||
}
|
||||
else if (char >= 'A' && char <= 'Z')
|
||||
{
|
||||
if (newChar - 'A' < 0) { newChar += 26 }
|
||||
if (newChar - 'A' >= 26) { newChar -= 26 }
|
||||
}
|
||||
else // not alphabetic, so keep as is
|
||||
{
|
||||
newChar = char
|
||||
}
|
||||
return newChar
|
||||
}
|
||||
|
||||
static Str shiftStr (Str msg, Int key)
|
||||
{
|
||||
res := StrBuf()
|
||||
msg.each { res.addChar (shift(it, key)) }
|
||||
return res.toStr
|
||||
}
|
||||
|
||||
static Str encode (Str msg, Int key)
|
||||
{
|
||||
return shiftStr (msg, key)
|
||||
}
|
||||
|
||||
static Str decode (Str msg, Int key)
|
||||
{
|
||||
return shiftStr (msg, -key)
|
||||
}
|
||||
|
||||
static Void main (Str[] args)
|
||||
{
|
||||
if (args.size == 2)
|
||||
{
|
||||
msg := args[0]
|
||||
key := Int(args[1])
|
||||
|
||||
echo ("$msg with key $key")
|
||||
echo ("Encode: ${encode(msg, key)}")
|
||||
echo ("Decode: ${decode(encode(msg, key), key)}")
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Task/Caesar-cipher/Fhidwfe/caesar-cipher.fhidwfe
Normal file
42
Task/Caesar-cipher/Fhidwfe/caesar-cipher.fhidwfe
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
lowers = ['a','z']
|
||||
uppers = ['A','Z']
|
||||
function void caesar_encode(message:ptr key:uint) {
|
||||
len = strlen$ message
|
||||
for uint [0u,len) with index {
|
||||
temp = deref_ubyte$ + message index
|
||||
if in temp lowers {
|
||||
temp = + temp key// shift lowercase letters
|
||||
if > temp 'z' {
|
||||
temp = - temp 26ub
|
||||
} ;
|
||||
} {
|
||||
if in temp uppers {
|
||||
temp = + temp key// shift uppercase letters
|
||||
if > temp 'Z' {
|
||||
temp = - temp 26ub
|
||||
} ;
|
||||
} ;//don't shift other characters
|
||||
}
|
||||
put_ubyte$ + message index temp
|
||||
}
|
||||
}
|
||||
function void caesar_decode(message:ptr key:uint) {
|
||||
caesar_encode$ message - 26u key
|
||||
}
|
||||
|
||||
key = 1u
|
||||
response = malloc$ 256u
|
||||
puts$ "plaintext: "
|
||||
getline$ response
|
||||
caesar_encode$ response key
|
||||
puts$ "cyphertext: "
|
||||
puts$ response
|
||||
putln$
|
||||
caesar_decode$ response key
|
||||
puts$ "decoded: "
|
||||
puts$ response
|
||||
free$ response
|
||||
free$ lowers // ranges are objects and must be freed
|
||||
free$ uppers
|
||||
|
||||
//this compiles with only 6 warnings!
|
||||
19
Task/Caesar-cipher/Forth/caesar-cipher.fth
Normal file
19
Task/Caesar-cipher/Forth/caesar-cipher.fth
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
: ceasar ( c n -- c )
|
||||
over 32 or [char] a -
|
||||
dup 0 26 within if
|
||||
over + 25 > if 26 - then +
|
||||
else 2drop then ;
|
||||
|
||||
: ceasar-string ( n str len -- )
|
||||
over + swap do i c@ over ceasar i c! loop drop ;
|
||||
|
||||
: ceasar-inverse ( n -- 'n ) 26 swap - 26 mod ;
|
||||
|
||||
2variable test
|
||||
s" The five boxing wizards jump quickly!" test 2!
|
||||
|
||||
3 test 2@ ceasar-string
|
||||
test 2@ cr type
|
||||
|
||||
3 ceasar-inverse test 2@ ceasar-string
|
||||
test 2@ cr type
|
||||
43
Task/Caesar-cipher/Fortran/caesar-cipher.f
Normal file
43
Task/Caesar-cipher/Fortran/caesar-cipher.f
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
program Caesar_Cipher
|
||||
implicit none
|
||||
|
||||
integer, parameter :: key = 3
|
||||
character(43) :: message = "The five boxing wizards jump quickly"
|
||||
|
||||
write(*, "(2a)") "Original message = ", message
|
||||
call encrypt(message)
|
||||
write(*, "(2a)") "Encrypted message = ", message
|
||||
call decrypt(message)
|
||||
write(*, "(2a)") "Decrypted message = ", message
|
||||
|
||||
contains
|
||||
|
||||
subroutine encrypt(text)
|
||||
character(*), intent(inout) :: text
|
||||
integer :: i
|
||||
|
||||
do i = 1, len(text)
|
||||
select case(text(i:i))
|
||||
case ('A':'Z')
|
||||
text(i:i) = achar(modulo(iachar(text(i:i)) - 65 + key, 26) + 65)
|
||||
case ('a':'z')
|
||||
text(i:i) = achar(modulo(iachar(text(i:i)) - 97 + key, 26) + 97)
|
||||
end select
|
||||
end do
|
||||
end subroutine
|
||||
|
||||
subroutine decrypt(text)
|
||||
character(*), intent(inout) :: text
|
||||
integer :: i
|
||||
|
||||
do i = 1, len(text)
|
||||
select case(text(i:i))
|
||||
case ('A':'Z')
|
||||
text(i:i) = achar(modulo(iachar(text(i:i)) - 65 - key, 26) + 65)
|
||||
case ('a':'z')
|
||||
text(i:i) = achar(modulo(iachar(text(i:i)) - 97 - key, 26) + 97)
|
||||
end select
|
||||
end do
|
||||
end subroutine
|
||||
|
||||
end program Caesar_Cipher
|
||||
41
Task/Caesar-cipher/FreeBASIC/caesar-cipher.basic
Normal file
41
Task/Caesar-cipher/FreeBASIC/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub Encrypt(s As String, key As Integer)
|
||||
Dim c As Integer
|
||||
For i As Integer = 0 To Len(s)
|
||||
Select Case As Const s[i]
|
||||
Case 65 To 90
|
||||
c = s[i] + key
|
||||
If c > 90 Then c -= 26
|
||||
s[i] = c
|
||||
Case 97 To 122
|
||||
c = s[i] + key
|
||||
If c > 122 Then c -= 26
|
||||
s[i] = c
|
||||
End Select
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Sub Decrypt(s As String, key As Integer)
|
||||
Dim c As Integer
|
||||
For i As Integer = 0 To Len(s)
|
||||
Select Case As Const s[i]
|
||||
Case 65 To 90
|
||||
c = s[i] - key
|
||||
If c < 65 Then c += 26
|
||||
s[i] = c
|
||||
Case 97 To 122
|
||||
c = s[i] - key
|
||||
If c < 97 Then c += 26
|
||||
s[i] = c
|
||||
End Select
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Dim As String s = "Bright vixens jump; dozy fowl quack."
|
||||
Print "Plain text : "; s
|
||||
Encrypt s, 8
|
||||
Print "Encrypted : "; s
|
||||
Decrypt s, 8
|
||||
Print "Decrypted : "; s
|
||||
Sleep
|
||||
26
Task/Caesar-cipher/GAP/caesar-cipher.gap
Normal file
26
Task/Caesar-cipher/GAP/caesar-cipher.gap
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
CaesarCipher := function(s, n)
|
||||
local r, c, i, lower, upper;
|
||||
lower := "abcdefghijklmnopqrstuvwxyz";
|
||||
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
r := "";
|
||||
for c in s do
|
||||
i := Position(lower, c);
|
||||
if i <> fail then
|
||||
Add(r, lower[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
i := Position(upper, c);
|
||||
if i <> fail then
|
||||
Add(r, upper[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
Add(r, c);
|
||||
fi;
|
||||
fi;
|
||||
od;
|
||||
return r;
|
||||
end;
|
||||
|
||||
CaesarCipher("IBM", 25);
|
||||
# "HAL"
|
||||
|
||||
CaesarCipher("Vgg cphvi wzdibn vmz wjmi amzz viy zlpvg di ydbidot viy mdbcon.", 5);
|
||||
# "All human beings are born free and equal in dignity and rights."
|
||||
36
Task/Caesar-cipher/GFA-Basic/caesar-cipher.basic
Normal file
36
Task/Caesar-cipher/GFA-Basic/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
'
|
||||
' Caesar cypher
|
||||
'
|
||||
OPENW 1 ! Creates a window for handling input/output
|
||||
CLEARW 1
|
||||
INPUT "string to encrypt ";text$
|
||||
INPUT "encryption key ";key%
|
||||
encrypted$=@encrypt$(UPPER$(text$),key%)
|
||||
PRINT "Encrypted: ";encrypted$
|
||||
PRINT "Decrypted: ";@decrypt$(encrypted$,key%)
|
||||
'
|
||||
PRINT "(Press any key to end program.)"
|
||||
~INP(2)
|
||||
CLOSEW 1
|
||||
'
|
||||
FUNCTION encrypt$(text$,key%)
|
||||
LOCAL result$,i%,c%
|
||||
result$=""
|
||||
FOR i%=1 TO LEN(text$)
|
||||
c%=ASC(MID$(text$,i%))
|
||||
IF c%<ASC("A") OR c%>ASC("Z") ! don't encrypt non A-Z
|
||||
result$=result$+CHR$(c%)
|
||||
ELSE
|
||||
c%=c%+key%
|
||||
IF c%>ASC("Z")
|
||||
c%=c%-26
|
||||
ENDIF
|
||||
result$=result$+CHR$(c%)
|
||||
ENDIF
|
||||
NEXT i%
|
||||
RETURN result$
|
||||
ENDFUNC
|
||||
'
|
||||
FUNCTION decrypt$(text$,key%)
|
||||
RETURN @encrypt$(text$,26-key%)
|
||||
ENDFUNC
|
||||
20
Task/Caesar-cipher/Gambas/caesar-cipher.gambas
Normal file
20
Task/Caesar-cipher/Gambas/caesar-cipher.gambas
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
Public Sub Main()
|
||||
Dim byKey As Byte = 3 'The key (Enter 26 to get the same output as input)
|
||||
Dim byCount As Byte 'Counter
|
||||
Dim sCeasar As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" 'Used to calculate the cipher
|
||||
Dim sString As String = "The five boxing wizards jump quickly" 'Phrase to encrypt
|
||||
Dim sCoded, sTemp As String 'Various strings
|
||||
|
||||
For byCount = 1 To Len(sString) 'Count through each letter in the phrase
|
||||
If Mid(sString, byCount, 1) = " " Then 'If it's a space..
|
||||
sCoded &= " " 'Keep it a space
|
||||
Continue 'Jump to the next iteration of the loop
|
||||
Endif
|
||||
sTemp = Mid(sCeasar, InStr(sCeasar, Mid(UCase(sString), byCount, 1)) + byKey, 1) 'Get the new 'coded' letter
|
||||
If Asc(Mid(sString, byCount, 1)) > 96 Then sTemp = Chr(Asc(sTemp) + 32) 'If the original was lower case then make the new 'coded' letter lower case
|
||||
sCoded &= sTemp 'Add the result to the code string
|
||||
Next
|
||||
|
||||
Print sString & gb.NewLine & sCoded 'Print the result
|
||||
|
||||
End
|
||||
59
Task/Caesar-cipher/Go/caesar-cipher-1.go
Normal file
59
Task/Caesar-cipher/Go/caesar-cipher-1.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ckey struct {
|
||||
enc, dec func(rune) rune
|
||||
}
|
||||
|
||||
func newCaesar(k int) (*ckey, bool) {
|
||||
if k < 1 || k > 25 {
|
||||
return nil, false
|
||||
}
|
||||
rk := rune(k)
|
||||
return &ckey{
|
||||
enc: func(c rune) rune {
|
||||
if c >= 'a' && c <= 'z'-rk || c >= 'A' && c <= 'Z'-rk {
|
||||
return c + rk
|
||||
} else if c > 'z'-rk && c <= 'z' || c > 'Z'-rk && c <= 'Z' {
|
||||
return c + rk - 26
|
||||
}
|
||||
return c
|
||||
},
|
||||
dec: func(c rune) rune {
|
||||
if c >= 'a'+rk && c <= 'z' || c >= 'A'+rk && c <= 'Z' {
|
||||
return c - rk
|
||||
} else if c >= 'a' && c < 'a'+rk || c >= 'A' && c < 'A'+rk {
|
||||
return c - rk + 26
|
||||
}
|
||||
return c
|
||||
},
|
||||
}, true
|
||||
}
|
||||
|
||||
func (ck ckey) encipher(pt string) string {
|
||||
return strings.Map(ck.enc, pt)
|
||||
}
|
||||
|
||||
func (ck ckey) decipher(ct string) string {
|
||||
return strings.Map(ck.dec, ct)
|
||||
}
|
||||
|
||||
func main() {
|
||||
pt := "The five boxing wizards jump quickly"
|
||||
fmt.Println("Plaintext:", pt)
|
||||
for _, key := range []int{0, 1, 7, 25, 26} {
|
||||
ck, ok := newCaesar(key)
|
||||
if !ok {
|
||||
fmt.Println("Key", key, "invalid")
|
||||
continue
|
||||
}
|
||||
ct := ck.encipher(pt)
|
||||
fmt.Println("Key", key)
|
||||
fmt.Println(" Enciphered:", ct)
|
||||
fmt.Println(" Deciphered:", ck.decipher(ct))
|
||||
}
|
||||
}
|
||||
57
Task/Caesar-cipher/Go/caesar-cipher-2.go
Normal file
57
Task/Caesar-cipher/Go/caesar-cipher-2.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type ckey struct {
|
||||
enc, dec unicode.SpecialCase
|
||||
}
|
||||
|
||||
func newCaesar(k int) (*ckey, bool) {
|
||||
if k < 1 || k > 25 {
|
||||
return nil, false
|
||||
}
|
||||
i := uint32(k)
|
||||
r := rune(k)
|
||||
return &ckey{
|
||||
unicode.SpecialCase{
|
||||
{'A', 'Z' - i, [3]rune{r}},
|
||||
{'Z' - i + 1, 'Z', [3]rune{r - 26}},
|
||||
{'a', 'z' - i, [3]rune{r}},
|
||||
{'z' - i + 1, 'z', [3]rune{r - 26}},
|
||||
},
|
||||
unicode.SpecialCase{
|
||||
{'A', 'A' + i - 1, [3]rune{26 - r}},
|
||||
{'A' + i, 'Z', [3]rune{-r}},
|
||||
{'a', 'a' + i - 1, [3]rune{26 - r}},
|
||||
{'a' + i, 'z', [3]rune{-r}},
|
||||
},
|
||||
}, true
|
||||
}
|
||||
|
||||
func (ck ckey) encipher(pt string) string {
|
||||
return strings.ToUpperSpecial(ck.enc, pt)
|
||||
}
|
||||
|
||||
func (ck ckey) decipher(ct string) string {
|
||||
return strings.ToUpperSpecial(ck.dec, ct)
|
||||
}
|
||||
|
||||
func main() {
|
||||
pt := "The five boxing wizards jump quickly"
|
||||
fmt.Println("Plaintext:", pt)
|
||||
for _, key := range []int{0, 1, 7, 25, 26} {
|
||||
ck, ok := newCaesar(key)
|
||||
if !ok {
|
||||
fmt.Println("Key", key, "invalid")
|
||||
continue
|
||||
}
|
||||
ct := ck.encipher(pt)
|
||||
fmt.Println("Key", key)
|
||||
fmt.Println(" Enciphered:", ct)
|
||||
fmt.Println(" Deciphered:", ck.decipher(ct))
|
||||
}
|
||||
}
|
||||
13
Task/Caesar-cipher/Groovy/caesar-cipher-1.groovy
Normal file
13
Task/Caesar-cipher/Groovy/caesar-cipher-1.groovy
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def caesarEncode(cipherKey, text) {
|
||||
def builder = new StringBuilder()
|
||||
text.each { character ->
|
||||
int ch = character[0] as char
|
||||
switch(ch) {
|
||||
case 'a'..'z': ch = ((ch - 97 + cipherKey) % 26 + 97); break
|
||||
case 'A'..'Z': ch = ((ch - 65 + cipherKey) % 26 + 65); break
|
||||
}
|
||||
builder << (ch as char)
|
||||
}
|
||||
builder as String
|
||||
}
|
||||
def caesarDecode(cipherKey, text) { caesarEncode(26 - cipherKey, text) }
|
||||
7
Task/Caesar-cipher/Groovy/caesar-cipher-2.groovy
Normal file
7
Task/Caesar-cipher/Groovy/caesar-cipher-2.groovy
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def caesarEncode(cipherKey, text) {
|
||||
text.chars.collect { c ->
|
||||
int off = c.isUpperCase() ? 'A' : 'a'
|
||||
c.isLetter() ? (((c as int) - off + cipherKey) % 26 + off) as char : c
|
||||
}.join()
|
||||
}
|
||||
def caesarDecode(cipherKey, text) { caesarEncode(26 - cipherKey, text) }
|
||||
4
Task/Caesar-cipher/Groovy/caesar-cipher-3.groovy
Normal file
4
Task/Caesar-cipher/Groovy/caesar-cipher-3.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def caesarEncode(k, text) {
|
||||
(text as int[]).collect { it==' ' ? ' ' : (((it & 0x1f) + k - 1) % 26 + 1 | it & 0xe0) as char }.join()
|
||||
}
|
||||
def caesarDecode(k, text) { caesarEncode(26 - k, text) }
|
||||
4
Task/Caesar-cipher/Groovy/caesar-cipher-4.groovy
Normal file
4
Task/Caesar-cipher/Groovy/caesar-cipher-4.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def caesarEncode(k, text) {
|
||||
text.tr('a-zA-Z', ((('a'..'z')*2)[k..(k+25)] + (('A'..'Z')*2)[k..(k+25)]).join())
|
||||
}
|
||||
def caesarDecode(cipherKey, text) { caesarEncode(26 - cipherKey, text) }
|
||||
5
Task/Caesar-cipher/Groovy/caesar-cipher-5.groovy
Normal file
5
Task/Caesar-cipher/Groovy/caesar-cipher-5.groovy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def caesarEncode(k, text) {
|
||||
def c = { (it*2)[k..(k+25)].join() }
|
||||
text.tr('a-zA-Z', c('a'..'z') + c('A'..'Z'))
|
||||
}
|
||||
def caesarDecode(cipherKey, text) { caesarEncode(26 - cipherKey, text) }
|
||||
10
Task/Caesar-cipher/Groovy/caesar-cipher-6.groovy
Normal file
10
Task/Caesar-cipher/Groovy/caesar-cipher-6.groovy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def plainText = "The Quick Brown Fox jumped over the lazy dog"
|
||||
def cipherKey = 12
|
||||
def cipherText = caesarEncode(cipherKey, plainText)
|
||||
def decodedText = caesarDecode(cipherKey, cipherText)
|
||||
|
||||
println "plainText: $plainText"
|
||||
println "cypherText($cipherKey): $cipherText"
|
||||
println "decodedText($cipherKey): $decodedText"
|
||||
|
||||
assert plainText == decodedText
|
||||
16
Task/Caesar-cipher/Haskell/caesar-cipher-1.hs
Normal file
16
Task/Caesar-cipher/Haskell/caesar-cipher-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
module Caesar (caesar, uncaesar) where
|
||||
|
||||
import Data.Char
|
||||
|
||||
caesar, uncaesar :: (Integral a) => a -> String -> String
|
||||
caesar k = map f
|
||||
where f c = case generalCategory c of
|
||||
LowercaseLetter -> addChar 'a' k c
|
||||
UppercaseLetter -> addChar 'A' k c
|
||||
_ -> c
|
||||
uncaesar k = caesar (-k)
|
||||
|
||||
addChar :: (Integral a) => Char -> a -> Char -> Char
|
||||
addChar b o c = chr $ fromIntegral (b' + (c' - b' + o) `mod` 26)
|
||||
where b' = fromIntegral $ ord b
|
||||
c' = fromIntegral $ ord c
|
||||
24
Task/Caesar-cipher/Haskell/caesar-cipher-2.hs
Normal file
24
Task/Caesar-cipher/Haskell/caesar-cipher-2.hs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import Data.Bool (bool)
|
||||
import Data.Char (chr, isAlpha, isUpper, ord)
|
||||
|
||||
---------------------- CAESAR CIPHER ---------------------
|
||||
|
||||
caesar, uncaesar :: Int -> String -> String
|
||||
caesar = fmap . tr
|
||||
uncaesar = caesar . negate
|
||||
|
||||
tr :: Int -> Char -> Char
|
||||
tr offset c
|
||||
| isAlpha c =
|
||||
chr
|
||||
. ((+) <*> (flip mod 26 . (-) (offset + ord c)))
|
||||
$ bool 97 65 (isUpper c)
|
||||
| otherwise = c
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = do
|
||||
let k = -114
|
||||
cipher = caesar k
|
||||
plain = "Curio, Cesare venne, e vide e vinse ? "
|
||||
mapM_ putStrLn $ [cipher, uncaesar k . cipher] <*> [plain]
|
||||
32
Task/Caesar-cipher/Haskell/caesar-cipher-3.hs
Normal file
32
Task/Caesar-cipher/Haskell/caesar-cipher-3.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{-# LANGUAGE LambdaCase #-}
|
||||
module Main where
|
||||
|
||||
import Control.Error (tryRead, tryAt)
|
||||
import Control.Monad.Trans (liftIO)
|
||||
import Control.Monad.Trans.Except (ExceptT, runExceptT)
|
||||
|
||||
import Data.Char
|
||||
import System.Exit (die)
|
||||
import System.Environment (getArgs)
|
||||
|
||||
main :: IO ()
|
||||
main = runExceptT parseKey >>= \case
|
||||
Left err -> die err
|
||||
Right k -> interact $ caesar k
|
||||
|
||||
parseKey :: (Read a, Integral a) => ExceptT String IO a
|
||||
parseKey = liftIO getArgs >>=
|
||||
flip (tryAt "Not enough arguments") 0 >>=
|
||||
tryRead "Key is not a valid integer"
|
||||
|
||||
caesar :: (Integral a) => a -> String -> String
|
||||
caesar k = map f
|
||||
where f c = case generalCategory c of
|
||||
LowercaseLetter -> addChar 'a' k c
|
||||
UppercaseLetter -> addChar 'A' k c
|
||||
_ -> c
|
||||
|
||||
addChar :: (Integral a) => Char -> a -> Char -> Char
|
||||
addChar b o c = chr $ fromIntegral (b' + (c' - b' + o) `mod` 26)
|
||||
where b' = fromIntegral $ ord b
|
||||
c' = fromIntegral $ ord c
|
||||
9
Task/Caesar-cipher/Hoon/caesar-cipher.hoon
Normal file
9
Task/Caesar-cipher/Hoon/caesar-cipher.hoon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|%
|
||||
++ enc
|
||||
|= [msg=tape key=@ud]
|
||||
^- tape
|
||||
(turn `(list @)`msg |=(n=@ud (add (mod (add (sub n 'A') key) 26) 'A')))
|
||||
++ dec
|
||||
|= [msg=tape key=@ud]
|
||||
(enc msg (sub 26 key))
|
||||
--
|
||||
41
Task/Caesar-cipher/IS-BASIC/caesar-cipher.basic
Normal file
41
Task/Caesar-cipher/IS-BASIC/caesar-cipher.basic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
100 PROGRAM "CaesarCi.bas"
|
||||
110 STRING M$*254
|
||||
120 INPUT PROMPT "String: ":M$
|
||||
130 DO
|
||||
140 INPUT PROMPT "Key (1-25): ":KEY
|
||||
150 LOOP UNTIL KEY>0 AND KEY<26
|
||||
160 PRINT "Original message: ";M$
|
||||
170 CALL ENCRYPT(M$,KEY)
|
||||
180 PRINT "Encrypted message: ";M$
|
||||
190 CALL DECRYPT(M$,KEY)
|
||||
200 PRINT "Decrypted message: ";M$
|
||||
210 DEF ENCRYPT(REF M$,KEY)
|
||||
220 STRING T$*254
|
||||
230 LET T$=""
|
||||
240 FOR I=1 TO LEN(M$)
|
||||
250 SELECT CASE M$(I)
|
||||
260 CASE "A" TO "Z"
|
||||
270 LET T$=T$&CHR$(65+MOD(ORD(M$(I))-65+KEY,26))
|
||||
280 CASE "a" TO "z"
|
||||
290 LET T$=T$&CHR$(97+MOD(ORD(M$(I))-97+KEY,26))
|
||||
300 CASE ELSE
|
||||
310 LET T$=T$&M$(I)
|
||||
320 END SELECT
|
||||
330 NEXT
|
||||
340 LET M$=T$
|
||||
350 END DEF
|
||||
360 DEF DECRYPT(REF M$,KEY)
|
||||
370 STRING T$*254
|
||||
380 LET T$=""
|
||||
390 FOR I=1 TO LEN(M$)
|
||||
400 SELECT CASE M$(I)
|
||||
410 CASE "A" TO "Z"
|
||||
420 LET T$=T$&CHR$(65+MOD(ORD(M$(I))-39-KEY,26))
|
||||
430 CASE "a" TO "z"
|
||||
440 LET T$=T$&CHR$(97+MOD(ORD(M$(I))-71-KEY,26))
|
||||
450 CASE ELSE
|
||||
460 LET T$=T$&M$(I)
|
||||
470 END SELECT
|
||||
480 NEXT
|
||||
490 LET M$=T$
|
||||
500 END DEF
|
||||
16
Task/Caesar-cipher/Icon/caesar-cipher.icon
Normal file
16
Task/Caesar-cipher/Icon/caesar-cipher.icon
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
procedure main()
|
||||
ctext := caesar(ptext := map("The quick brown fox jumped over the lazy dog"))
|
||||
dtext := caesar(ctext,,"decrypt")
|
||||
write("Plain text = ",image(ptext))
|
||||
write("Encphered text = ",image(ctext))
|
||||
write("Decphered text = ",image(dtext))
|
||||
end
|
||||
|
||||
procedure caesar(text,k,mode) #: mono-alphabetic shift cipher
|
||||
/k := 3
|
||||
k := (((k % *&lcase) + *&lcase) % *&lcase) + 1
|
||||
case mode of {
|
||||
&null|"e"|"encrypt": return map(text,&lcase,(&lcase||&lcase)[k+:*&lcase])
|
||||
"d"|"decrypt" : return map(text,(&lcase||&lcase)[k+:*&lcase],&lcase)
|
||||
}
|
||||
end
|
||||
2
Task/Caesar-cipher/J/caesar-cipher-1.j
Normal file
2
Task/Caesar-cipher/J/caesar-cipher-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
cndx=: [: , 65 97 +/ 26 | (i.26)&+
|
||||
caesar=: (cndx 0)}&a.@u:@cndx@[ {~ a.i.]
|
||||
2
Task/Caesar-cipher/J/caesar-cipher-2.j
Normal file
2
Task/Caesar-cipher/J/caesar-cipher-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
2 caesar 'This simple "monoalphabetic substitution cipher" provides almost no security, ...'
|
||||
Vjku ukorng "oqpqcnrjcdgvke uwduvkvwvkqp ekrjgt" rtqxkfgu cnoquv pq ugewtkva, ...
|
||||
1
Task/Caesar-cipher/J/caesar-cipher-3.j
Normal file
1
Task/Caesar-cipher/J/caesar-cipher-3.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
CAESAR=:1 :'(26|m&+)&.((26{.64}.a.)&i.)'
|
||||
2
Task/Caesar-cipher/J/caesar-cipher-4.j
Normal file
2
Task/Caesar-cipher/J/caesar-cipher-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
20 CAESAR 'HI'
|
||||
BC
|
||||
28
Task/Caesar-cipher/Janet/caesar-cipher.janet
Normal file
28
Task/Caesar-cipher/Janet/caesar-cipher.janet
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(def alphabet "abcdefghijklmnopqrstuvwxyz")
|
||||
|
||||
(defn rotate
|
||||
"shift bytes given x"
|
||||
[str x]
|
||||
(let [r (% x (length alphabet))
|
||||
b (string/bytes str)
|
||||
abyte (get (string/bytes "a") 0)
|
||||
zbyte (get (string/bytes "z") 0)]
|
||||
(var result @[])
|
||||
(for i 0 (length b)
|
||||
(let [ch (bor (get b i) 0x20)] # convert uppercase to lowercase
|
||||
(when (and (<= abyte ch) (<= ch zbyte))
|
||||
(array/push result (get alphabet (% (+ (+ (+ (- zbyte abyte) 1) (- ch abyte)) r) (length alphabet)))))))
|
||||
(string/from-bytes ;result)))
|
||||
|
||||
(defn code
|
||||
"encodes and decodes str given argument type"
|
||||
[str rot type]
|
||||
(case type
|
||||
:encode (rotate str rot)
|
||||
:decode (rotate str (* rot -1))))
|
||||
|
||||
(defn main [& args]
|
||||
(let [cipher (code "The quick brown fox jumps over the lazy dog" 23 :encode)
|
||||
str (code cipher 23 :decode)]
|
||||
(print cipher)
|
||||
(print str)))
|
||||
30
Task/Caesar-cipher/Java/caesar-cipher.java
Normal file
30
Task/Caesar-cipher/Java/caesar-cipher.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
public class Cipher {
|
||||
public static void main(String[] args) {
|
||||
|
||||
String str = "The quick brown fox Jumped over the lazy Dog";
|
||||
|
||||
System.out.println( Cipher.encode( str, 12 ));
|
||||
System.out.println( Cipher.decode( Cipher.encode( str, 12), 12 ));
|
||||
}
|
||||
|
||||
public static String decode(String enc, int offset) {
|
||||
return encode(enc, 26-offset);
|
||||
}
|
||||
|
||||
public static String encode(String enc, int offset) {
|
||||
offset = offset % 26 + 26;
|
||||
StringBuilder encoded = new StringBuilder();
|
||||
for (char i : enc.toCharArray()) {
|
||||
if (Character.isLetter(i)) {
|
||||
if (Character.isUpperCase(i)) {
|
||||
encoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
|
||||
} else {
|
||||
encoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
|
||||
}
|
||||
} else {
|
||||
encoded.append(i);
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
}
|
||||
11
Task/Caesar-cipher/JavaScript/caesar-cipher-1.js
Normal file
11
Task/Caesar-cipher/JavaScript/caesar-cipher-1.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function caesar (text, shift) {
|
||||
return text.toUpperCase().replace(/[^A-Z]/g,'').replace(/./g, function(a) {
|
||||
return String.fromCharCode(65+(a.charCodeAt(0)-65+shift)%26);
|
||||
});
|
||||
}
|
||||
|
||||
// Tests
|
||||
var text = 'veni, vidi, vici';
|
||||
for (var i = 0; i<26; i++) {
|
||||
console.log(i+': '+caesar(text,i));
|
||||
}
|
||||
5
Task/Caesar-cipher/JavaScript/caesar-cipher-2.js
Normal file
5
Task/Caesar-cipher/JavaScript/caesar-cipher-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var caesar = (text, shift) => text
|
||||
.toUpperCase()
|
||||
.replace(/[^A-Z]/g, '')
|
||||
.replace(/./g, a =>
|
||||
String.fromCharCode(65 + (a.charCodeAt(0) - 65 + shift) % 26));
|
||||
43
Task/Caesar-cipher/JavaScript/caesar-cipher-3.js
Normal file
43
Task/Caesar-cipher/JavaScript/caesar-cipher-3.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
((key, strPlain) => {
|
||||
|
||||
// Int -> String -> String
|
||||
let caesar = (k, s) => s.split('')
|
||||
.map(c => tr(
|
||||
inRange(['a', 'z'], c) ? 'a' :
|
||||
inRange(['A', 'Z'], c) ? 'A' : 0,
|
||||
k, c
|
||||
))
|
||||
.join('');
|
||||
|
||||
// Int -> String -> String
|
||||
let unCaesar = (k, s) => caesar(26 - (k % 26), s);
|
||||
|
||||
// Char -> Int -> Char -> Char
|
||||
let tr = (base, offset, char) =>
|
||||
base ? (
|
||||
String.fromCharCode(
|
||||
ord(base) + (
|
||||
ord(char) - ord(base) + offset
|
||||
) % 26
|
||||
)
|
||||
) : char;
|
||||
|
||||
// [a, a] -> a -> b
|
||||
let inRange = ([min, max], v) => !(v < min || v > max);
|
||||
|
||||
// Char -> Int
|
||||
let ord = c => c.charCodeAt(0);
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
let range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// TEST
|
||||
let strCipher = caesar(key, strPlain),
|
||||
strDecode = unCaesar(key, strCipher);
|
||||
|
||||
return [strCipher, ' -> ', strDecode];
|
||||
|
||||
})(114, 'Curio, Cesare venne, e vide e vinse ? ');
|
||||
30
Task/Caesar-cipher/Jq/caesar-cipher.jq
Normal file
30
Task/Caesar-cipher/Jq/caesar-cipher.jq
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
def encrypt(key):
|
||||
. as $s
|
||||
| explode as $xs
|
||||
| (key % 26) as $offset
|
||||
| if $offset == 0 then .
|
||||
else reduce range(0; length) as $i ( {chars: []};
|
||||
$xs[$i] as $c
|
||||
| .d = $c
|
||||
| if ($c >= 65 and $c <= 90) # 'A' to 'Z'
|
||||
then .d = $c + $offset
|
||||
| if (.d > 90) then .d += -26 else . end
|
||||
else if ($c >= 97 and $c <= 122) # 'a' to 'z'
|
||||
then .d = $c + $offset
|
||||
| if (.d > 122)
|
||||
then .d += -26
|
||||
else .
|
||||
end
|
||||
else .
|
||||
end
|
||||
end
|
||||
| .chars[$i] = .d )
|
||||
| .chars | implode
|
||||
end ;
|
||||
|
||||
def decrypt(key): encrypt(26 - key);
|
||||
|
||||
"Bright vixens jump; dozy fowl quack."
|
||||
| .,
|
||||
(encrypt(8)
|
||||
| ., decrypt(8) )
|
||||
33
Task/Caesar-cipher/Jsish/caesar-cipher.jsish
Normal file
33
Task/Caesar-cipher/Jsish/caesar-cipher.jsish
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* Caesar cipher, in Jsish */
|
||||
"use strict";
|
||||
|
||||
function caesarCipher(input:string, key:number):string {
|
||||
return input.replace(/([a-z])/g,
|
||||
function(mat, p1, ofs, str) {
|
||||
return Util.fromCharCode((p1.charCodeAt(0) + key + 26 - 97) % 26 + 97);
|
||||
}).replace(/([A-Z])/g,
|
||||
function(mat, p1, ofs, str) {
|
||||
return Util.fromCharCode((p1.charCodeAt(0) + key + 26 - 65) % 26 + 65);
|
||||
});
|
||||
}
|
||||
|
||||
provide('caesarCipher', 1);
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
var str = 'The five boxing wizards jump quickly';
|
||||
; str;
|
||||
; 'Enciphered:';
|
||||
; caesarCipher(str, 3);
|
||||
; 'Enciphered then deciphered';
|
||||
; caesarCipher(caesarCipher(str, 3), -3);
|
||||
}
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
str ==> The five boxing wizards jump quickly
|
||||
'Enciphered:'
|
||||
caesarCipher(str, 3) ==> Wkh ilyh eralqj zlcdugv mxps txlfnob
|
||||
'Enciphered then deciphered'
|
||||
caesarCipher(caesarCipher(str, 3), -3) ==> The five boxing wizards jump quickly
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
32
Task/Caesar-cipher/Julia/caesar-cipher.julia
Normal file
32
Task/Caesar-cipher/Julia/caesar-cipher.julia
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# Caeser cipher
|
||||
# Julia 1.5.4
|
||||
# author: manuelcaeiro | https://github.com/manuelcaeiro
|
||||
|
||||
function csrcipher(text, key)
|
||||
ciphtext = ""
|
||||
for l in text
|
||||
numl = Int(l)
|
||||
ciphnuml = numl + key
|
||||
if numl in 65:90
|
||||
if ciphnuml > 90
|
||||
rotciphnuml = ciphnuml - 26
|
||||
ciphtext = ciphtext * Char(rotciphnuml)
|
||||
else
|
||||
ciphtext = ciphtext * Char(ciphnuml)
|
||||
end
|
||||
elseif numl in 97:122
|
||||
if ciphnuml > 122
|
||||
rotciphnuml = ciphnuml - 26
|
||||
ciphtext = ciphtext * Char(rotciphnuml)
|
||||
else
|
||||
ciphtext = ciphtext * Char(ciphnuml)
|
||||
end
|
||||
else
|
||||
ciphtext = ciphtext * Char(numl)
|
||||
end
|
||||
end
|
||||
return ciphtext
|
||||
end
|
||||
|
||||
text = "Magic Encryption"; key = 13
|
||||
csrcipher(text, key)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue