Caesar cipher

Caesar cipher
You are encouraged to solve this task according to the task description, using any language you may know.

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.

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

For readability, input is in all caps.This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

function caesarCipher rot phrase local rotPhrase, lowerLetters, upperLetters put "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" into lowerLetters put "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" into upperLetters repeat for each char letter in phrase get charTonum(letter) if it >= 65 and it <= 90 then put char ((it + rot) - 64) of upperLetters after rotPhrase else if it >= 97 and it <= 122 then put char ((it + rot) - 96) of lowerLetters after rotPhrase else put letter after rotPhrase end if end repeat return rotPhraseend caesarCipher

function caesar(string s, integer key)integer ch, base for i=1 to length(s) do ch = s[i] base = alpha_b[ch] if base then s[i] = base+remainder(ch-base+key,26) end if end for return send functionstring s = "One fine day in the middle of the night, two dead men got up to fight. \n"& "Back to back they faced each other, drew their swords and shot each other. %^&*()[", e = caesar(s,5), r = caesar(e,26-5) ?e ?r

Here is an alternate and more advanced form of the encrypt procedure. It improves on the simple version in terms of speed, in case Caesar is using the cipher on some very long documents. It is meant to replace the encrypt procedure in the previous code and produces identical results.

To make a rotation, Ruby is worse than Perl 6. Ruby needs two extra conversions: the code [*"A".."Z"].rotate(key).join uses to_a to convert a Range to an Array, to access the Array#rotate method. Then join converts the rotated Array to a String, because String#tr needs a String, not an Array.

This code is roughly equivalent to the rot-13 cypher sed implementation, except that the conversion table is parameterized by a number and that the conversion done manually, instead of using `y///' command.

# use \n to mark character to converts/^/\n/# append conversion table to pattern spaceG: loop # look up converted character and place it instead of old one s/\n(.)(.*\n.*\1.{51}(.))/\n\3\2/ # advance \n even if prev. command fails, thus skip non-alphabetical characters /\n\n/! s/\n([^\n])/\1\n/t loops/\n\n.*//

ASCII didn't exit in 1948, and the task specification explicitly says we only need to convert Roman capitals; so we adopt a simpler encoding, representing the letters of the alphabet from A=0 to Z=25.

The program will convert one character at a time. Load the character code into storage address 20, and the key into address 19. The machine will halt with the cyphered encoding in the accumulator. A negative (two's complement) key decodes.

This is in fact a general solution that will work equally well with alphabets of more or fewer than 26 characters: simply replace the constant 26 in storage address 18 with 22 for Hebrew, 24 for Greek, 28 for Arabic, 33 for Russian, etc.

The strategy here, one of many possible ones, is to build, at run time,the arguments to be passed to deffilter to construct a pair of filters enc and dec for encoding and decoding. Filters are specified as tuples of strings.

The reification operator (-:) generates efficient code for applications like this given a table of inputs and outputs, which is obtained in this case by zipping the alphabet with itself rolled the right number of times, done separately for the upper and lower case letters and then combined.