

Task

Implement a Caesar cipher, both encoding and decoding. 

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).

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. 

Also, Rot-13 is identical to Caesar cipher with key 13.



Related tasks

 Rot-13
 Substitution Cipher
 Vigenère Cipher/Cryptanalysis


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