Caesar cipher

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

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 "monoalphabetic 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.

See also


Ada

<lang Ada>with Ada.Text_IO;

procedure Caesar is

  type M26 is mod 26;
  function To_M26(C: Character; Offset: Character) return M26 is
  begin
     return M26(Character'Pos(C)-Character'Pos(Offset));
  end To_M26;
  function To_Character(Value:   in  M26; Offset: Character)
                       return Character is
  begin
     return Character'Val(Integer(Value)+Character'Pos(Offset));
  end To_Character;
  function Encrypt (Plain: String; Key: M26) return String is
     Ciph: String(Plain'Range);
  begin
     for I in Plain'Range loop
        case Plain(I) is
           when 'A' .. 'Z' =>
              Ciph(I) := To_Character(To_M26(Plain(I), 'A')+Key, 'A');
           when 'a' .. 'z' =>
              Ciph(I) := To_Character(To_M26(Plain(I), 'a')+Key, 'a');
           when others =>
              Ciph(I) := Plain(I);
        end case;
     end loop;
     return Ciph;
  end Encrypt;
  Text:  String := Ada.Text_IO.Get_Line;
  Key: M26 := 3; -- Default key from "Commentarii de Bello Gallico"

begin -- Caesar main program

  Ada.Text_IO.Put_Line("Plaintext ------------>" & Text);
  Text := Encrypt(Text, Key);
  Ada.Text_IO.Put_Line("Ciphertext ----------->" & Text);
  Ada.Text_IO.Put_Line("Decrypted Ciphertext ->" & Encrypt(Text, -Key));

end Caesar;</lang>

Output:
> ./caesar 
The five boxing wizards jump quickly
Plaintext ------------>The five boxing wizards jump quickly
Ciphertext ----------->Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted Ciphertext ->The five boxing wizards jump quickly

ALGOL 68

Translation of: Ada

Note: This specimen retains the original Ada coding style.

Works with: ALGOL 68 version Revision 1 - no extensions to language used.
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny.

<lang algol68>#!/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#;
  1. 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#</lang>

Output:
Plaintext ------------>The five boxing wizards jump quickly
Ciphertext ----------->Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted Ciphertext ->The five boxing wizards jump quickly

AutoHotkey

This ungodly solution is an attempt at code-golf. It requires input to be all-caps alphabetic, only works on AutoHotkey_L Unicode, and might not run on x64 <lang AutoHotkey>n=2 s=HI t:=&s While *t o.=Chr(Mod(*t-65+n,26)+65),t+=2 MsgBox % o</lang> This next one is much more sane and handles input very well, including case. <lang AutoHotkey>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)</lang>

Output:
j k
Bc

AutoIt

The Ceasar Funktion can enrcypt and decrypt, standart is Encryption, to Decrypt set third parameter to False <lang autoit> $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 </lang>

AWK

<lang awk>

  1. !/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

} </lang>

Output:
message: My hovercraft is full of eels.
 cypher: NZ IPWFSDSBGU JT GVMM PG FFMT.
  clear: MY HOVERCRAFT IS FULL OF EELS.

Babel

<lang babel>((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 }))</lang>
Output:
The quick brown fox jumps over the lazy dog.
Kyv hlztb sifne wfo aldgj fmvi kyv crqp ufx.
The quick brown fox jumps over the lazy dog.

BBC BASIC

<lang bbcbasic> 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$

</lang>

Output:
Pack my box with five dozen liquor jugs
Zkmu wi lyh gsdr psfo nyjox vsaeyb teqc
Pack my box with five dozen liquor jugs

Befunge

Almost direct copy of the Vigenère cipher, although the code has been reversed and the first line eliminated because of the simpler key initialisation.

The text to encrypt is read from stdin, and the key is the first integer on the stack - 11 (65+) in the example below.

<lang befunge>65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<< "`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`

    • -"A"-::0\`\55*`+#^_\0g+"4"+4^>\`*48</lang>
Output:
The quick brown fox jumped over the lazy dogs
ESPBFTNVMCZHYQZIUFXAPOZGPCESPWLKJOZRD

The decrypter is essentially identical, except for a change of sign on the last line.

<lang befunge>65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<< "`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`

    • -"A"-::0\`\55*`+#^_\0g-"4"+4^>\`*48</lang>
Output:
ESPBFTNVMCZHYQZIUFXAPOZGPCESPWLKJOZRD
THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGS

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>
  1. define caesar(x) rot(13, x)
  2. define decaesar(x) rot(13, x)
  3. define decrypt_rot(x, y) rot((26-x), y)

void rot(int c, char *str) { int l = strlen(str); const char *alpha[2] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

int i; for (i = 0; i < l; i++) { if (!isalpha(str[i])) continue;

str[i] = alpha[isupper(str[i])][((int)(tolower(str[i])-'a')+c)%26]; } }


int main() { 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; }</lang>

C++

<lang Cpp>#include <string>

  1. include <iostream>
  2. include <algorithm>
  3. 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 ;

}</lang>

Output:
Which text is to be encrypted ?
this is an interesting text
shift ?
3
Before encryption:
this is an interesting text
encrypted:
wklv lv dq lqwhuhvwlqj whaw
Decrypted again:
this is an interesting text

C#

<lang csharp>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.ToCharArray().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: {0}", str);
           str = Decrypt(str, 5);
           Console.WriteLine("Decrypted: {0}", str);
           Console.ReadKey();
       }
   }

}</lang>

Output:
Pack my box with five dozen liquor jugs.
Encrypted: Ufhp rd gtc bnym knaj itejs qnvztw ozlx.
Decrypted: Pack my box with five dozen liquor jugs.

Clojure

<lang Clojure>(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"))</lang>
Output:
Original text: The Quick Brown Fox Jumps Over The Lazy Dog. 
Encryption: Sgd Pthbj Aqnvm Enw Itlor Nudq Sgd Kzyx Cnf. 
Decryption: The Quick Brown Fox Jumps Over The Lazy Dog.

COBOL

Works with: OpenCOBOL version 2.0

<lang cobol> >>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.</lang>

Output:
Enter a message to encrypt: The quick brown fox jumps over the lazy dog.
Enter the amount to shift by: 7
Encrypted: Aol xbpjr iyvdu mve qbtwz vcly aol shgf kvn.      
Decrypted: The quick brown fox jumps over the lazy dog.     

CoffeeScript

<lang coffeescript>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</lang>

Output:
> coffee foo.coffee 
Jgnnq Yqtnf
dcDc %^&*()

Common Lisp

<lang lisp>(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))</lang>
Output:
 Original: The five boxing wizards jump quickly 
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob 
Decrypted: The five boxing wizards jump quickly

Cubescript

<lang cubescript>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 ]</lang>

Usage: <lang>>>> 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.</lang>

D

<lang d>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));

}</lang>

Output:
Original:  The five boxing wizards jump quickly
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted: The five boxing wizards jump quickly

Simpler in-place version (same output): <lang d>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);

}</lang>

A version that uses the standard library (same output): <lang d>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));

}</lang>

Dart

<lang dart>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.");

}</lang>

Output:
JK
BC
"" encrypts to:
"" decrypts to:
""
"A" encrypts to:
"K" decrypts to:
"A"
"z" encrypts to:
"j" decrypts to:
"z"
"Caesar cipher" encrypts to:
"Mkockb mszrob" decrypts to:
"Caesar cipher"
".-:/"\!" encrypts to:
".-:/"\!" decrypts to:
".-:/"\!"
"The Quick Brown Fox Jumps Over The Lazy Dog." encrypts to:
"Dro Aesmu Lbygx Pyh Tewzc Yfob Dro Vkji Nyq." decrypts to:
"The Quick Brown Fox Jumps Over The Lazy Dog."


Eiffel

<lang eiffel> 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 </lang>

Output:
String to encode: The tiny tiger totally taunted the tall Till.
Encoded string: Ftq fuzk fusqd fafmxxk fmgzfqp ftq fmxx Fuxx.
Decoded string (after encoding and decoding): The tiny tiger totally taunted the tall Till.

Elena

<lang elena>#define system.

  1. define system'routines.
  2. define system'math.
  3. define extensions.
  1. symbol Letters = "abcdefghijklmnopqrstuvwxyz".
  2. symbol BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
  3. symbol TestText = "Pack my box with five dozen liquor jugs.".
  4. symbol Key = 12.
  1. class Encrypting :: Enumerator

{

   #field theKey.
   #field theEnumerator.
   
   #constructor new &key:aKey &text:aText
   [
       theKey := aKey.
       theEnumerator := aText enumerator.
   ]
   
   #method next => theEnumerator.
   
   #method reset => theEnumerator.
   
   #method get
   [
       #var aChar := theEnumerator get.
       
       #var anIndex := Letters indexOf:0:aChar.
       
       (-1 < anIndex)
           ? [
               ^ Letters @ ((theKey+anIndex) mod:26).
           ]
           ! [
               anIndex := BigLetters indexOf:0:aChar.
               (-1 < anIndex)
                   ? [
                       ^ BigLetters @ ((theKey+anIndex) mod:26).
                   ]
                   ! [
                       ^ aChar.
                   ].
           ].
   ]

}

  1. class(extension)encryptOp

{

   #method encrypt : aKey
       = Encrypting new &key:aKey &text:self summarize:(String new). 
   #method decrypt :aKey
       = Encrypting new &key:(26 - aKey) &text:self summarize:(String new). 

}

  1. symbol program =

[

   console writeLine:"Original text :" :TestText.
       
   #var anEncryptedText := TestText encrypt:Key.
   console writeLine:"Encrypted text:" :anEncryptedText.
   #var aDecryptedText := anEncryptedText decrypt:Key.
   console writeLine:"Decrypted text:" :aDecryptedText.
   console readChar.

].</lang>

Output:
Original text :Pack my box with five dozen liquor jugs.
Encrypted text:Bmow yk naj iuft ruhq palqz xucgad vgse.
Decrypted text:Pack my box with five dozen liquor jugs.

Elixir

<lang elixir>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.codepoints(text) |> Enum.map_join(fn c -> Dict.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)}"</lang>

Output:
Original:  The five boxing wizards jump quickly
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted: The five boxing wizards jump quickly

Erlang

<lang Erlang> %% 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).

</lang> Command: <lang Erlang>ceasar:main("The five boxing wizards jump quickly", 3).</lang>

Output:
Plaintext ----> The five boxing wizards jump quickly
Cyphertext ---> Wkh ilyh eralqj zlcdugv mxps txlfnob
"The five boxing wizards jump quickly"

ERRE

<lang ERRE> 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 </lang>

Output:
Pack my box with five dozen liquor jugs
Qbdl nz cpy xjui gjwf epafo mjrvps kvht
Pack my box with five dozen liquor jugs

Euphoria

Works with: Euphoria version 4.0.0

<lang Euphoria> --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})

</lang>

Output:
"The Quick Brown Fox Jumps Over The Lazy Dog." encrypts to:
"Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt." decrypts to:
"The Quick Brown Fox Jumps Over The Lazy Dog." 

F#

<lang fsharp>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)</lang>
> caesar.encrypt 2 "HI";;
val it : string = "JK"
> caesar.encrypt 20 "HI";;
val it : string = "BC"
> let c = caesar.encrypt 13 "The quick brown fox jumps over the lazy dog.";;
val c : string = "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
> caesar.decrypt 13 c;;
val it : string = "The quick brown fox jumps over the lazy dog."

Fantom

Shifts upper/lower case letters, leaves other characters as they are.

<lang fantom> 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)}")
   }
 }

} </lang>

Example:

$ fan caesar.fan "Encrypt - With ! Case," 1
Encrypt - With ! Case, with key 1
Encode: Fodszqu - Xjui ! Dbtf,
Decode: Encrypt - With ! Case,

$ fan caesar.fan "Encrypt - With ! Case," 5
Encrypt - With ! Case, with key 5
Encode: Jshwduy - Bnym ! Hfxj,
Decode: Encrypt - With ! Case,

$ fan caesar.fan "Encrypt - With ! Case," 10
Encrypt - With ! Case, with key 10
Encode: Oxmbizd - Gsdr ! Mkco,
Decode: Encrypt - With ! Case,

Forth

<lang forth>: 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</lang>

Fortran

<lang fortran>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</lang>

Output:
Original message  = The five boxing wizards jump quickly
Encrypted message = Wkh ilyh eralgj zlcdugv mxps txlfnob
Decrypted message = The five boxing wizards jump quickly

GAP

<lang gap>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);

  1. "HAL"

CaesarCipher("Vgg cphvi wzdibn vmz wjmi amzz viy zlpvg di ydbidot viy mdbcon.", 5);

  1. "All human beings are born free and equal in dignity and rights."</lang>

Go

Obvious solution with explicit testing for character ranges: <lang go>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))
   }

}</lang> Data driven version using functions designed for case conversion. (And for method using % operator, see Vigenère_cipher#Go.) <lang go>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))
   }

}</lang>

Output:

(either version)

Plaintext: The five boxing wizards jump quickly
Key 0 invalid
Key 1
  Enciphered: Uif gjwf cpyjoh xjabset kvnq rvjdlmz
  Deciphered: The five boxing wizards jump quickly
Key 7
  Enciphered: Aol mpcl ivepun dpghykz qbtw xbpjrsf
  Deciphered: The five boxing wizards jump quickly
Key 25
  Enciphered: Sgd ehud anwhmf vhyzqcr itlo pthbjkx
  Deciphered: The five boxing wizards jump quickly
Key 26 invalid

Groovy

Java style: <lang groovy>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) }</lang>

Functional style: <lang groovy>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) }</lang>

Ninja style: <lang groovy>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) }</lang>

Using built in 'tr' function and a replacement alphabet: <lang groovy>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) }</lang> and the same with closures for somewhat better readability: <lang groovy>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) }</lang> Test code: <lang groovy> 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 </lang>

Output:
plainText: The Quick Brown Fox jumped over the lazy dog
cypherText(12): Ftq Cguow Ndaiz Raj vgybqp ahqd ftq xmlk pas
decodedText(12): The Quick Brown Fox jumped over the lazy dog

Haskell

<lang haskell>import Data.Char

numLetters :: (Integral a) => a numLetters = 26

caesar :: (Integral a) => a -> String -> String caesar k = map f

   where 
       f c
           | isAsciiLower c = tr 'a' k c
           | isAsciiUpper c = tr 'A' k c
           | otherwise = c

unCaesar :: (Integral a) => a -> String -> String unCaesar k = caesar (-k)

-- char addition tr :: (Integral a) => Char -> a -> Char -> Char tr base offset char = let ob = fromIntegral $ ord base

                         oc = fromIntegral $ ord char in
                         chr $ fromIntegral (ob + (oc - ob + offset) `mod` numLetters)</lang>

And trying it out in GHCi:

*Main> caesar 1 "hal"
"ibm"
*Main> unCaesar 1 "ibm"
"hal"

Icon and Unicon

Strictly speaking a Ceasar Cipher is a shift of 3 (the default in this case). <lang Icon>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</lang>

Output:
Plain text  = "the quick brown fox jumped over the lazy dog"
Encphered text = "wkh txlfn eurzq ira mxpshg ryhu wkh odcb grj"
Decphered text = "the quick brown fox jumped over the lazy dog"

J

If we assume that the task also requires us to leave non-alphabetic characters alone: <lang j>cndx=: [: , 65 97 +/ 26 | (i.26)&+ caesar=: (cndx 0)}&a.@u:@cndx@[ {~ a.i.]</lang> Example use:<lang j> 2 caesar 'This simple "monoalphabetic substitution cipher" provides almost no security, ...' Vjku ukorng "oqpqcnrjcdgvke uwduvkvwvkqp ekrjgt" rtqxkfgu cnoquv pq ugewtkva, ...</lang> If we instead assume the task only requires we treat upper case characters: <lang j>CAESAR=:1 :'(26|m&+)&.((26{.64}.a.)&i.)'</lang> Example use:<lang j> 20 CAESAR 'HI' BC</lang>

Java

Works with: Java version 1.5+

<lang java5>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();
   }

}</lang>

Output:
Ftq cguow ndaiz raj Vgybqp ahqd ftq xmlk Pas
The quick brown fox Jumped over the lazy Dog

JavaScript

<lang javascript><html><head><title>Caesar</title></head>

<body>


<script type="application/javascript"> function disp(x) { var e = document.createTextNode(x + '\n'); document.getElementById('x').appendChild(e); }

function trans(msg, rot) { return msg.replace(/([a-z])/ig, function($1) { var c = $1.charCodeAt(0); return String.fromCharCode( c >= 97 ? (c + rot + 26 - 97) % 26 + 97 : (c + rot + 26 - 65) % 26 + 65); }); }

var msg = "The quick brown f0x Jumped over the lazy Dog 123"; var enc = trans(msg, 3); var dec = trans(enc, -3);

disp("Original:" + msg + "\nEncoded: " + enc + "\nDecoded: " + dec); </script></body></html></lang>

Or, translating from Haskell:

<lang JavaScript>(function (key, strPlain) {


 // Int -> String -> String
 function caesar(k, s) {
   return s.split().map(function (c) {
     return tr(
       inRange(['a', 'z'], c) ? 'a' :
       inRange(['A', 'Z'], c) ? 'A' : 0,
       k, c
     )
   }).join();
 }
 // Int -> String -> String
 function unCaesar(k, s) {
   return caesar(26 - (k % 26), s);
 }
 // Char -> Int -> Char -> Char
 function tr(base, offset, char) {
   return base ? (
     String.fromCharCode(
       ord(base) + (
         ord(char) - ord(base) + offset
       ) % 26
     )
   ) : char;
 }
 // [a, a] -> a -> b
 function inRange([min, max], v) {
   return !(v < min || v > max);
 }
 // Char -> Int
 function ord(c) {
   return c.charCodeAt(0);
 }
 // [m..n]
 function range(m, n) {
   return Array.apply(null, Array(n - m + 1)).map(
     function (x, i) {
       return m + i;
     }
   );
 }
 // TEST
 var strCipher = caesar(key, strPlain),
   strDecode = unCaesar(key, strCipher);
 return [strCipher, ' -> ', strDecode];

})(114, 'Curio, Cesare venne, e vide e vinse ? ');</lang>

<lang JavaScript>["Mebsy, Mockbo foxxo, o fsno o fsxco ? ", " -> ", "Curio, Cesare venne, e vide e vinse ? "]</lang>

Julia

<lang julia> function rot(s::String, key::Integer)

   map(s) do c
       if 'a' <= c <= 'z'
           char( mod(c - 'a' + key, 26) + 'a')
       elseif 'A' <= c <= 'Z'
           char( mod(c - 'A' + key, 26) + 'A')
       else
           c
       end
   end

end

key = 3 txt = "The five boxing wizards jump quickly"

println("Original: ", txt); println("Encrypted: ", rot(txt, key)) println("Decrypted: ", rot(rot(txt, key), 26 - key)) </lang> Based on the D code.

LabVIEW

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.

Liberty BASIC

<lang lb>key = 7

Print "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

'Encrypt the text Print CaesarCypher$("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", key)

'Decrypt the text by changing the key to (26 - key) Print CaesarCypher$(CaesarCypher$("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", key), (26 - key))

Function CaesarCypher$(string$, key)

   If (key < 0) Or (key > 25) Then _
   CaesarCypher$ = "Key is Ouside of Bounds" : Exit Function
   For i = 1 To Len(string$)
       rotate = Asc(Mid$(string$, i, 1))
       rotate = (rotate + key)
       If Asc(Mid$(string$, i, 1)) > Asc("Z") Then
           If rotate > Asc("z") Then rotate = (Asc("a") + (rotate - Asc("z")) - 1)
       Else
           If rotate > Asc("Z") Then rotate = (Asc("A") + (rotate - Asc("Z")) - 1)
       End If
       CaesarCypher$ = (CaesarCypher$ + Chr$(rotate))
   Next i

End Function</lang>

LiveCode

<lang LiveCode>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 rotPhrase

end caesarCipher</lang>

Translation of: Common Lisp
Works with: UCB Logo

<lang logo>; some useful constants make "lower_a ascii "a make "lower_z ascii "z make "upper_a ascii "A make "upper_z ascii "Z

encipher a single character

to encipher_char :char :key

local "code make "code ascii :char
local "base make "base 0
ifelse [and (:code >= :lower_a) (:code <= :lower_z)] [make "base :lower_a] [
    if [and (:code >= :upper_a) (:code <= :upper_z)] [make "base :upper_a] ]
ifelse [:base > 0] [
  output char (:base + (modulo ( :code - :base + :key ) 26 ))
] [
  output :char
]

end

encipher a whole string

to caesar_cipher :string :key

 output map [encipher_char ? :key] :string

end

Demo

make "plaintext "|The five boxing wizards jump quickly| make "key 3 make "ciphertext caesar_cipher :plaintext :key make "recovered caesar_cipher :ciphertext -:key

print sentence "| Original:| :plaintext print sentence "|Encrypted:| :ciphertext print sentence "|Recovered:| :recovered bye</lang>

Output:
 Original: The five boxing wizards jump quickly
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob
Recovered: The five boxing wizards jump quickly

Lua

<lang Lua>local function encrypt(text, key) return text:gsub("%a", function(t) local base = (t:lower() == t and string.byte('a') or string.byte('A'))

local r = t:byte() - base r = r + key r = r%26 -- works correctly even if r is negative r = r + base return string.char(r) end) end

local function decrypt(text, key) return encrypt(text, -key) end

caesar = { encrypt = encrypt, decrypt = decrypt, }

-- test do local text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz" local encrypted = caesar.encrypt(text, 7) local decrypted = caesar.decrypt(encrypted, 7) print("Original text: ", text) print("Encrypted text: ", encrypted) print("Decrypted text: ", decrypted) end </lang>

Maple

<lang Maple> > StringTools:-Encode( "The five boxing wizards jump quickly", encoding = alpharot[3] );

                      "Wkh ilyh eralqj zlcdugv mxps txlfnob"

> StringTools:-Encode( %, encoding = alpharot[ 23 ] );

                      "The five boxing wizards jump quickly"

</lang> (The symbol % refers the the last (non-NULL) value computed.)

Mathematica / Wolfram Language

<lang Mathematica>cypher[mesg_String,n_Integer]:=StringReplace[mesg,Flatten[Thread[Rule[#,RotateLeft[#,n]]]&/@CharacterRange@@@{{"a","z"},{"A","Z"}}]]</lang>

Output:
cypher["The five boxing wizards jump quickly",3]
-> Wkh ilyh eralqj zlcdugv mxps txlfnob

MATLAB / Octave

<lang Matlab> function s = cipherCaesar(s, key)

         s = char( mod(s - 'A' + key, 25 ) + 'A');
  end; 	 
  function s = decipherCaesar(s, key) 
         s = char( mod(s - 'A' - key, 25 ) + 'A');
  end; </lang>

Here is a test: <lang Matlab> decipherCaesar(cipherCaesar('ABC',4),4)

  ans = ABC </lang>

ML

mLite

In this implementation, the offset can be positive or negative and is wrapped around if greater than 25 or less than -25. <lang ocaml>fun readfile () = readfile []

          | x = let val ln = readln ()
                in if eof ln then
                     rev x
                   else
                     readfile ` ln :: x
                end

local val lower_a = ord #"a"; val lower_z = ord #"z"; val upper_a = ord #"A"; val upper_z = ord #"Z";

fun which (c_upper c) = (upper_a, upper_z) | _ = (lower_a, lower_z) ;

fun scale (c, az) where (c > #1 az) = scale( (#0 az + (c - #1 az - 1)), az) | (c, az) = c

in fun encipher ([], offset, t) = implode ` rev t | (x :: xs, offset, t) where (c_alphabetic x) = encipher (xs, offset, (chr ` scale (ord x + offset, which x)) :: t) | (x :: xs, offset, t) = encipher (xs, offset, x :: t) | (s, offset) = if (offset < 0) then encipher (explode s, 26 + (offset rem 26), []) else encipher (explode s, offset rem 26, []) end

fun default (false, y) = y | (x, _) = x

map println ` map (fn s = encipher (s,ston ` default (argv 0, "1"))) ` readfile (); </lang> The string for encoding is supplied as an input stream, and the offset supplied on the command line (defaults to 1). For example

echo The cat sat on the mat | mlite -f caecip.m ~5

Output:

Ocz xvo nvo ji ocz hvo

NetRexx

The cipher code in this sample is also used in the Rot-13 – NetRexx task. <lang NetRexx>/* NetRexx */

options replace format comments java crossref savelog symbols nobinary

messages = [ -

 'The five boxing wizards jump quickly', -
 'Attack at dawn!', -
 'HI']

keys = [1, 2, 20, 25, 13]

loop m_ = 0 to messages.length - 1

 in = messages[m_]
 loop k_ = 0 to keys.length - 1
   say 'Caesar cipher, key:' keys[k_].right(3)
   ec = caesar_encipher(in, keys[k_])
   dc = caesar_decipher(ec, keys[k_])
   say in
   say ec
   say dc
   say
   end k_
 say 'Rot-13:'
 ec = rot13(in)
 dc = rot13(ec)
 say in
 say ec
 say dc
 say
 end m_

return

method rot13(input) public static signals IllegalArgumentException

 return caesar(input, 13, isFalse)

method caesar(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException

 if idx < 1 | idx > 25 then signal IllegalArgumentException()
 --      12345678901234567890123456
 itab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 shift = itab.length - idx
 parse itab tl +(shift) tr
 otab = tr || tl
 if caps then input = input.upper
 cipher = input.translate(itab || itab.lower, otab || otab.lower)
 return cipher

method caesar_encipher(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException

 return caesar(input, idx, caps)

method caesar_decipher(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException

 return caesar(input, int(26) - idx, isFalse)

method caesar_encipher(input = Rexx, idx = int) public static signals IllegalArgumentException

 return caesar(input, idx, isFalse)

method caesar_decipher(input = Rexx, idx = int) public static signals IllegalArgumentException

 return caesar(input, int(26) - idx, isFalse)

method caesar_encipher(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException

 return caesar(input, idx, opt)

method caesar_decipher(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException

 return caesar(input, int(26) - idx, opt)

method caesar(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException

 if opt.upper.abbrev('U') >= 1 then caps = isTrue
 else                               caps = isFalse
 return caesar(input, idx, caps)

method caesar(input = Rexx, idx = int) public static signals IllegalArgumentException

 return caesar(input, idx, isFalse)

method isTrue public static returns boolean

 return (1 == 1)

method isFalse public static returns boolean

 return \isTrue</lang>
Caesar cipher, key:   1
The five boxing wizards jump quickly
Uif gjwf cpyjoh xjabset kvnq rvjdlmz
The five boxing wizards jump quickly

Caesar cipher, key:   2
The five boxing wizards jump quickly
Vjg hkxg dqzkpi ykbctfu lwor swkemna
The five boxing wizards jump quickly

Caesar cipher, key:  20
The five boxing wizards jump quickly
Nby zcpy vircha qctulxm dogj kocwefs
The five boxing wizards jump quickly

Caesar cipher, key:  25
The five boxing wizards jump quickly
Sgd ehud anwhmf vhyzqcr itlo pthbjkx
The five boxing wizards jump quickly

Caesar cipher, key:  13
The five boxing wizards jump quickly
Gur svir obkvat jvmneqf whzc dhvpxyl
The five boxing wizards jump quickly

Rot-13:
The five boxing wizards jump quickly
Gur svir obkvat jvmneqf whzc dhvpxyl
The five boxing wizards jump quickly

Caesar cipher, key:   1
Attack at dawn!
Buubdl bu ebxo!
Attack at dawn!

Caesar cipher, key:   2
Attack at dawn!
Cvvcem cv fcyp!
Attack at dawn!

Caesar cipher, key:  20
Attack at dawn!
Unnuwe un xuqh!
Attack at dawn!

Caesar cipher, key:  25
Attack at dawn!
Zsszbj zs czvm!
Attack at dawn!

Caesar cipher, key:  13
Attack at dawn!
Nggnpx ng qnja!
Attack at dawn!

Rot-13:
Attack at dawn!
Nggnpx ng qnja!
Attack at dawn!

Caesar cipher, key:   1
HI
IJ
HI

Caesar cipher, key:   2
HI
JK
HI

Caesar cipher, key:  20
HI
BC
HI

Caesar cipher, key:  25
HI
GH
HI

Caesar cipher, key:  13
HI
UV
HI

Rot-13:
HI
UV
HI

Nim

Translation of: Python

<lang nim>import strutils

proc caesar(s: string, k: int, decode = false): string =

 var k = if decode: 26 - k else: k
 result = ""
 for i in toUpper(s):
   if ord(i) >= 65 and ord(i) <= 90:
     result.add(chr((ord(i) - 65 + k) mod 26 + 65))

let msg = "The quick brown fox jumped over the lazy dogs" echo msg let enc = caesar(msg, 11) echo enc echo caesar(enc, 11, decode = true)</lang>

Oberon-2

Works with oo2c version2 <lang oberon2> MODULE Caesar; IMPORT

 Out;

CONST

 encode* = 1;
 decode* = -1;

VAR

 text,cipher: POINTER TO ARRAY OF CHAR;
 PROCEDURE Cipher*(txt: ARRAY OF CHAR; key: INTEGER; op: INTEGER; VAR cipher: ARRAY OF CHAR);
 VAR
   i: LONGINT;
 BEGIN
   i := 0;
   WHILE i < LEN(txt) - 1 DO
     IF (txt[i] >= 'A') & (txt[i] <= 'Z') THEN
       cipher[i] := CHR(ORD('A') + ((ORD(txt[i]) - ORD('A') + (key * op))) MOD 26)
     ELSIF (txt[i] >= 'a') & (txt[i] <= 'z') THEN
       cipher[i] := CHR(ORD('a') + ((ORD(txt[i]) - ORD('a') + (key * op))) MOD 26)
     ELSE
       cipher[i] := txt[i]
     END;
     INC(i)
   END;
   cipher[i] := 0X
 END Cipher;
 

BEGIN

 NEW(text,3);NEW(cipher,3);
 COPY("HI",text^);
 Out.String(text^);Out.String(" =e=> ");
 Cipher(text^,2,encode,cipher^);
 Out.String(cipher^);
 COPY(cipher^,text^);
 Cipher(text^,2,decode,cipher^);
 Out.String(" =d=> ");Out.String(cipher^);Out.Ln;  
 COPY("ZA",text^);
 Out.String(text^);Out.String(" =e=> ");
 Cipher(text^,2,encode,cipher^);
 Out.String(cipher^);
 COPY(cipher^,text^);
 Cipher(text^,2,decode,cipher^);
 Out.String(" =d=> ");Out.String(cipher^);Out.Ln; 
 NEW(text,37);NEW(cipher,37);
 COPY("The five boxing wizards jump quickly",text^);
 Out.String(text^);Out.String(" =e=> ");
 Cipher(text^,3,encode,cipher^);
 Out.String(cipher^);
 COPY(cipher^,text^);
 Cipher(text^,3,decode,cipher^);
 Out.String(" =d=> ");Out.String(cipher^);Out.Ln; 

END Caesar. </lang> Output:

HI =e=> JK =d=> HI
ZA =e=> BC =d=> ZA
The five boxing wizards jump quickly =e=> Wkh ilyh eralqj zlcdugv mxps txlfnob =d=> The five boxing wizards jump quickly

Objeck

<lang objeck> class Caesar {

 function : native : Encode(enc : String, offset : Int) ~ String {
   offset := offset % 26 + 26;
   encoded := "";
   enc := enc->ToLower();
   each(i : enc) {
     c := enc->Get(i);
     if(c->IsChar()) {
       j := (c - 'a' + offset) % 26;
       encoded->Append(j + 'a');
     }
     else {
       encoded->Append(c);
     };
   };
   
   return encoded;
 }
 
 function : Decode(enc : String, offset : Int) ~ String {
   return Encode(enc, offset * -1);
 }
 
 function : Main(args : String[]) ~ Nil {
   enc := Encode("The quick brown fox Jumped over the lazy Dog", 12);
   enc->PrintLine();
   Decode(enc, 12)->PrintLine();
 }

} </lang>

Output:
ftq cguow ndaiz raj vgybqp ahqd ftq xmlk pas
the quick brown fox jumped over the lazy dog

OCaml

<lang ocaml>let islower c =

 c >= 'a' && c <= 'z'

let isupper c =

 c >= 'A' && c <= 'Z'

let rot x str =

 let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 and lowchars = "abcdefghijklmnopqrstuvwxyz" in
 let rec decal x =
   if x < 0 then decal (x + 26) else x
 in
 let x = (decal x) mod 26 in
 let decal_up = x - (int_of_char 'A')
 and decal_low = x - (int_of_char 'a') in
 let len = String.length str in
 let res = String.create len in
 for i = 0 to pred len do
   let c = str.[i] in
   if islower c then
     let j = ((int_of_char c) + decal_low) mod 26 in
     res.[i] <- lowchars.[j]
   else if isupper c then
     let j = ((int_of_char c) + decal_up) mod 26 in
     res.[i] <- upchars.[j]
   else
     res.[i] <- c
 done;
 (res)

(* or in OCaml 4.00+: let rot x =

 let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 and lowchars = "abcdefghijklmnopqrstuvwxyz" in
 let rec decal x =
   if x < 0 then decal (x + 26) else x
 in
 let x = (decal x) mod 26 in
 let decal_up = x - (int_of_char 'A')
 and decal_low = x - (int_of_char 'a') in
 String.map (fun c ->
   if islower c then
     let j = ((int_of_char c) + decal_low) mod 26 in
     lowchars.[j]
   else if isupper c then
     let j = ((int_of_char c) + decal_up) mod 26 in
     upchars.[j]
   else
     c
 )
  • )</lang>

<lang ocaml>let () =

 let key = 3 in
 let orig = "The five boxing wizards jump quickly" in
 let enciphered = rot key orig in
 print_endline enciphered;
 let deciphered = rot (- key) enciphered in
 print_endline deciphered;
 Printf.printf "equal: %b\n" (orig = deciphered)
</lang>
Output:
$ ocaml caesar.ml 
Wkh ilyh eralqj zlcdugv mxps txlfnob
The five boxing wizards jump quickly
equal: true

Oforth

<lang Oforth>: ceasar(key, c) {

  c dup isLetter ifFalse: [ return ]
  isUpper ifTrue: [ 'A' ] else: [ 'a' ] c key + over - 26 mod + 

}

cipherE(s, key) { s map(#[ key ceasar ]) charsAsString }
cipherD(s, key) { cipherE(s, 26 key - ) }</lang>
Output:
>cipherE("Pack my box with five dozen liquor jugs.", 5) println
Ufhp rd gtc bnym knaj itejs qnvztw ozlx.
ok
>cipherD("Ufhp rd gtc bnym knaj itejs qnvztw ozlx.", 5) println
Pack my box with five dozen liquor jugs.

OOC

<lang ooc>main: func (args: String[]) {

       shift := args[1] toInt()
       if (args length != 3) {
               "Usage: #{args[0]} [number] [sentence]" println()
               "Incorrect number of arguments." println()
       } else if (!shift && args[1] != "0"){
               "Usage: #{args[0]} [number] [sentence]" println()
               "Number is not a valid number." println()
       } else {
               str := ""
               for (c in args[2]) {
                       if (c alpha?()) {
                               c = (c lower?() ? 'a' : 'A') + (26 + c toLower() - 'a' + shift) % 26
                       }
                       str += c
               }
               str println()
       }

}</lang>

Output:
$ ./caesar 8 "This should be a fairly original sentence."
Bpqa apwctl jm i niqztg wzqoqvit amvbmvkm.
$ ./caesar -9 "Yet another, fairly original sentence!"
Pvk refkyvi, wrzicp fizxzerc jvekvetv!

PARI/GP

<lang parigp>enc(s,n)={

 Strchr(Vecsmall(apply(k->if(k>96&&k<123,(k+n-97)%26+97, if(k>64&&k<91, (k+n-65)%26+65, k)),
 Vec(Vecsmall(s)))))

}; dec(s,n)=enc(s,-n);</lang>

Pascal

<lang pascal>Program CaesarCipher(output);

procedure encrypt(var message: string; key: integer);

 var
   i: integer;
 begin
   for i := 1 to length(message) do
     case message[i] of
       'A'..'Z': message[i] := chr(ord('A') + (ord(message[i]) - ord('A') + key) mod 26);
       'a'..'z': message[i] := chr(ord('a') + (ord(message[i]) - ord('a') + key) mod 26);
     end;
 end;

procedure decrypt(var message: string; key: integer);

 var
   i: integer;
 begin
   for i := 1 to length(message) do
     case message[i] of
      'A'..'Z': message[i] := chr(ord('A') + (ord(message[i]) - ord('A') - key + 26) mod 26);
      'a'..'z': message[i] := chr(ord('a') + (ord(message[i]) - ord('a') - key + 26) mod 26);
     end;
 end;

var

 key: integer;
 message: string;

begin

 key := 3;
 message := 'The five boxing wizards jump quickly';
 writeln ('Original message: ', message);
 encrypt(message, key);
 writeln ('Encrypted message: ', message);
 decrypt(message, key);
 writeln ('Decrypted message: ', message);

end.</lang>

Output:
>: ./CaesarCipher
Original message: The five boxing wizards jump quickly
Encrypted message: Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted message: The five boxing wizards jump quickly
>: 

Perl

<lang Perl>sub caesar {

       my ($message, $key, $decode) = @_;
       $key = 26 - $key if $decode;
       $message =~ s/([A-Z])/chr(((ord(uc $1) - 65 + $key) % 26) + 65)/geir;

}

my $msg = 'THE FIVE BOXING WIZARDS JUMP QUICKLY'; my $enc = caesar($msg, 10); my $dec = caesar($enc, 10, 'decode');

print "msg: $msg\nenc: $enc\ndec: $dec\n"; </lang>

Output:
msg: THE FIVE BOXING WIZARDS JUMP QUICKLY
enc: DRO PSFO LYHSXQ GSJKBNC TEWZ AESMUVI
dec: THE FIVE BOXING WIZARDS JUMP QUICKLY

Perl 6

<lang perl6>my @alpha = 'A' .. 'Z'; sub encrypt ( $key where 1..25, $plaintext ) {

   $plaintext.trans( @alpha Z=> @alpha.rotate($key) );

} sub decrypt ( $key where 1..25, $cyphertext ) {

   $cyphertext.trans( @alpha.rotate($key) Z=> @alpha );

}

my $original = 'THE FIVE BOXING WIZARDS JUMP QUICKLY'; my $en = encrypt( 13, $original ); my $de = decrypt( 13, $en );

.say for $original, $en, $de;

say 'OK' if $original eq all( map { .&decrypt(.&encrypt($original)) }, 1..25 );</lang>

Output:
THE FIVE BOXING WIZARDS JUMP QUICKLY
GUR SVIR OBKVAT JVMNEQF WHZC DHVPXYL
THE FIVE BOXING WIZARDS JUMP QUICKLY
OK

Phix

<lang Phix>sequence alpha_b = repeat(0,255)

        alpha_b['A'..'Z'] = 'A'
        alpha_b['a'..'z'] = 'a'

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 s

end function string 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</lang>
Output:
Tsj knsj ifd ns ymj rniiqj tk ymj snlmy, ybt ijfi rjs lty zu yt knlmy.
Gfhp yt gfhp ymjd kfhji jfhm tymjw, iwjb ymjnw xbtwix fsi xmty jfhm tymjw. %^&*()[
One fine day in the middle of the night, two dead men got up to fight.
Back to back they faced each other, drew their swords and shot each other. %^&*()[

PHP

<lang php><?php function caesarEncode( $message, $key ){

   $plaintext = strtolower( $message );
   $ciphertext = "";
   $ascii_a = ord( 'a' );
   $ascii_z = ord( 'z' );
   while( strlen( $plaintext ) ){
       $char = ord( $plaintext );
       if( $char >= $ascii_a && $char <= $ascii_z ){
           $char = ( ( $key + $char - $ascii_a ) % 26 ) + $ascii_a;
       }
       $plaintext = substr( $plaintext, 1 );
       $ciphertext .= chr( $char );
   }
   return $ciphertext;

}

echo caesarEncode( "The quick brown fox Jumped over the lazy Dog", 12 ), "\n"; ?></lang>

Output:
ftq cguow ndaiz raj vgybqp ahqd ftq xmlk pas

PicoLisp

<lang PicoLisp>(setq *Letters (apply circ (mapcar char (range 65 90))))

(de caesar (Str Key)

  (pack
     (mapcar '((C) (cadr (nth (member C *Letters) Key)))
        (chop (uppc Str)) ) ) )</lang>

Test:

: (caesar "IBM" 25)
-> "HAL"
: (caesar @ 1)
-> "IBM"

: (caesar "The quick brown fox jumped over the lazy dog's back" 7)
-> "AOLXBPJRIYVDUMVEQBTWLKVCLYAOLSHGFKVNZIHJR"
: (caesar @ (- 26 7))
-> "THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGSBACK"

PL/I

<lang pli>caesar: procedure options (main);

  declare cypher_string character (52) static initial
     ((2)'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  declare (text, encyphered_text) character (100) varying,
     offset fixed binary;
  
  get edit (text) (L); /* Read in one line of text */
  get list (offset);
  if offset < 1 | offset > 25 then signal error;
  put skip list ('Plain text=', text);
  encyphered_text = translate(text, substr(cypher_string, offset+1, 26),
     'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
  put skip list ('Encyphered text=', encyphered_text);
  text = translate(encyphered_text, substr(cypher_string, 27-offset, 26),
     'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
  put skip list ('Decyphered text=', text);

end caesar;</lang>

Output:

with offset of 5

Plain text=             THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG 
Encyphered text=        YMJVZNHPGWTBSKTCOZRUXTAJWYMJQFEDITL 
Decyphered text=        THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG

PowerShell

<lang Powershell># Author: M. McNabb function Get-CaesarCipher { Param ( [Parameter( Mandatory=$true,ValueFromPipeline=$true)] [string] $Text,

[ValidateRange(1,25)] [int] $Key = 1,

[switch] $Decode )

begin {

   $LowerAlpha = [char]'a'..[char]'z'
   $UpperAlpha = [char]'A'..[char]'Z'

}

process {

   $Chars = $Text.ToCharArray()
   
   function encode
   {
       param
       (
       $Char,
       $Alpha = [char]'a'..[char]'z'
       )
       $Index = $Alpha.IndexOf([int]$Char)
       $NewIndex = ($Index + $Key) - $Alpha.Length
       $Alpha[$NewIndex]
   }
   
   function decode
   {
       param
       (
       $Char,
       $Alpha = [char]'a'..[char]'z'
       )
       $Index = $Alpha.IndexOf([int]$Char)
       $int = $Index - $Key
       if ($int -lt 0) {$NewIndex = $int + $Alpha.Length}
       else {$NewIndex = $int}
       $Alpha[$NewIndex]
   }
   foreach ($Char in $Chars)
   {
       if ([int]$Char -in $LowerAlpha)
       {
           if ($Decode) {$Char = decode $Char}
           else {$Char = encode $Char}
       }
       elseif ([int]$Char -in $UpperAlpha)
       {
           if ($Decode) {$Char = decode $Char $UpperAlpha}
           else {$Char = encode $Char $UpperAlpha}
       }
       
       $Char = [char]$Char
       [string]$OutText += $Char
   }
   $OutText
   $OutText = $null

} }</lang> Usage examples:

Encode:
PS C:\> 'Pack my box with five dozen liquor jugs.' | Get-CaesarCipher -key 3
Sdfn pb era zlwk ilyh grchq oltxru mxjv.

Decode:
PS C:\> 'Sdfn pb era zlwk ilyh grchq oltxru mxjv.' | Get-CaesarCipher -key 3 -Decode
Pack my box with five dozen liquor jugs.

Encode lines of text from a file:
PS C:\> Get-Content C:\Text.txt | Get-CaesarCipher -key 10
Vsxo yxo.
Vsxo dgy!
Vsxo drboo;

Prolog

Works with: SWI-Prolog
Library: clpfd

<lang Prolog>:- use_module(library(clpfd)).

caesar :- L1 = "The five boxing wizards jump quickly", writef("Original : %s\n", [L1]),

% encryption of the sentence encoding(3, L1, L2) , writef("Encoding : %s\n", [L2]),

% deciphering on the encoded sentence encoding(3, L3, L2), writef("Decoding : %s\n", [L3]).

% encoding/decoding of a sentence encoding(Key, L1, L2) :- maplist(caesar_cipher(Key), L1, L2).

caesar_cipher(_, 32, 32) :- !.

caesar_cipher(Key, V1, V2) :- V #= Key + V1,

% we verify that we are in the limits of A-Z and a-z. ((V1 #=< 0'Z #/\ V #> 0'Z) #\/ (V1 #=< 0'z #/\ V #> 0'z) #\/ (V1 #< 0'A #/\ V2 #>= 0'A)#\/ (V1 #< 0'a #/\ V2 #>= 0'a)) #==> A,

% if we are not in these limits A is 1, otherwise 0. V2 #= V - A * 26,

% compute values of V1 and V2 label([A, V1, V2]).</lang>

Output:
 ?- caesar.
Original : The five boxing wizards jump quickly
Encoding : Wkh ilyh eralqj zlcdugv mxps txlfnob
Decoding : The five boxing wizards jump quickly
true .

PureBasic

The case is maintained for alphabetic characters (uppercase/lowercase input = uppercase/lowercase output) while non-alphabetic characters, if present are included and left unchanged in the result. <lang PureBasic>Procedure.s CC_encrypt(plainText.s, key, reverse = 0)

 ;if reverse <> 0 then reverse the encryption (decrypt)
 If reverse: reverse = 26: key = 26 - key: EndIf
 
 Static alphabet$ = "ABCEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
 
 Protected result.s, i, length = Len(plainText), letter.s, legal
 If key < 1 Or key > 25: ProcedureReturn: EndIf  ;keep key in range
  
 For i = 1 To length
   letter = Mid(plainText, i, 1)
   legal = FindString(alphabet$, letter, 1 + reverse)
   If legal
     result + Mid(alphabet$, legal + key, 1)
   Else
     result + letter
   EndIf 
 Next 
 ProcedureReturn result

EndProcedure

Procedure.s CC_decrypt(cypherText.s, key)

 ProcedureReturn CC_encrypt(cypherText, key, 1)

EndProcedure

If OpenConsole()

 Define key, plainText.s, encryptedText.s, decryptedText.s
 
 key = Random(24) + 1 ;get a random key in the range 1 -> 25
 
 plainText = "The quick brown fox jumped over the lazy dogs.": PrintN(RSet("Plain text = ", 17) + #DQUOTE$ + plainText + #DQUOTE$)
 encryptedText = CC_encrypt(plainText, key): PrintN(RSet("Encrypted text = ", 17) + #DQUOTE$ + encryptedText + #DQUOTE$)
 decryptedText = CC_decrypt(encryptedText, key): PrintN(RSet("Decrypted text = ", 17) + #DQUOTE$ + decryptedText + #DQUOTE$)
 
 Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
 CloseConsole()

EndIf</lang>

Output:
    Plain text = "The quick brown fox jumped over the lazy dogs."
Encrypted text = "Znk waoiq hxuct lud pasvkj ubkx znk rgfe jumy."
Decrypted text = "the quick brown fox jumped over the lazy dogs."

Alternate solution

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. <lang PureBasic>Procedure.s CC_encrypt(text.s, key, reverse = 0)

 ;if reverse <> 0 then reverse the encryption (decrypt)
 Protected i, *letter.Character, *resultLetter.Character, result.s = Space(Len(text))
 
 If reverse: key = 26 - key: EndIf
 If key < 1 Or key > 25: ProcedureReturn: EndIf  ;exit if key out of range
  
 *letter = @text: *resultLetter = @result
 While *letter\c
   Select *letter\c
     Case 'A' To 'Z'
       *resultLetter\c = ((*letter\c - 65 + key) % 26) + 65
     Case 'a' To 'z'
       *resultLetter\c = ((*letter\c - 97 + key) % 26) + 97
     Default
       *resultLetter\c = *letter\c
   EndSelect
   *letter + SizeOf(Character): *resultLetter + SizeOf(Character)
 Wend
 ProcedureReturn result

EndProcedure</lang>

Python

<lang Python>def caesar(s, k, decode = False): if decode: k = 26 - k return "".join([chr((ord(i) - 65 + k) % 26 + 65) for i in s.upper() if ord(i) >= 65 and ord(i) <= 90 ])

msg = "The quick brown fox jumped over the lazy dogs" print msg enc = caesar(msg, 11) print enc print caesar(enc, 11, decode = True)</lang>

Output:
The quick brown fox jumped over the lazy dogs
ESPBFTNVMCZHYQZIUFXAPOZGPCESPWLKJOZRD
THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGS

Alternate solution

Works with: Python version 2.x

(for 3.x change string.maketrans to str.maketrans)

<lang python>import string def caesar(s, k, decode = False):

  if decode: k = 26 - k
  return s.translate(
      string.maketrans(
          string.ascii_uppercase + string.ascii_lowercase,
          string.ascii_uppercase[k:] + string.ascii_uppercase[:k] +
          string.ascii_lowercase[k:] + string.ascii_lowercase[:k]
          )
      )

msg = "The quick brown fox jumped over the lazy dogs" print msg enc = caesar(msg, 11) print enc print caesar(enc, 11, decode = True)</lang>

Output:
The quick brown fox jumped over the lazy dogs
Esp bftnv mczhy qzi ufxapo zgpc esp wlkj ozrd
The quick brown fox jumped over the lazy dogs

A compact alternative solution <lang python> from string import ascii_uppercase as abc

def caesar(s, k, decode = False):

   trans = dict(zip(abc, abc[(k,26-k)[decode]:] + abc[:(k,26-k)[decode]]))
   return .join(trans[L] for L in s.upper() if L in abc)

msg = "The quick brown fox jumped over the lazy dogs" print(caesar(msg, 11)) print(caesar(caesar(msg, 11), 11, True)) </lang>

Output:
ESPBFTNVMCZHYQZIUFXAPOZGPCESPWLKJOZRD
THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOGS


R

This is a generalization of the Rot-13 solution for R at: http://rosettacode.org/wiki/Rot-13#R . <lang R>

  1. based on Rot-13 solution: http://rosettacode.org/wiki/Rot-13#R

ceasar <- function(x, key) {

 # if key is negative, wrap to be positive
 if (key < 0) {
   key <- 26 + key 
 }
 
 old <- paste(letters, LETTERS, collapse="", sep="")
 new <- paste(substr(old, key * 2 + 1, 52), substr(old, 1, key * 2), sep="")
 chartr(old, new, x)

}

  1. simple examples from description

print(ceasar("hi",2)) print(ceasar("hi",20))

  1. more advanced example

key <- 3 plaintext <- "The five boxing wizards jump quickly." cyphertext <- ceasar(plaintext, key) decrypted <- ceasar(cyphertext, -key)

print(paste(" Plain Text: ", plaintext, sep="")) print(paste(" Cypher Text: ", cyphertext, sep="")) print(paste("Decrypted Text: ", decrypted, sep=""))


</lang>

Output:
> print(ceasar("hi",2))
[1] "jk"
> print(ceasar("hi",20))
[1] "bc"
> print(paste("Plain Text: ", plaintext, sep=""))
[1] "Plain Text: The five boxing wizards jump quickly."
> print(paste("Cypher Text: ", cyphertext, sep=""))
[1] "Cypher Text: Wkh ilyh eralqj zlcdugv mxps txlfnob."
> print(paste("Decrypted Text: ", decrypted, sep=""))
[1] "Decrypted Text: The five boxing wizards jump quickly."

Racket

<lang racket>

  1. lang racket

(define A (char->integer #\A)) (define Z (char->integer #\Z)) (define a (char->integer #\a)) (define z (char->integer #\z))

(define (rotate c n)

 (define cnum (char->integer c))
 (define (shift base) (integer->char (+ base (modulo (+ n (- cnum base)) 26))))
 (cond [(<= A cnum Z) (shift A)]
       [(<= a cnum z) (shift a)]
       [else c]))

(define (caesar s n)

 (list->string (for/list ([c (in-string s)]) (rotate c n))))

(define (encrypt s) (caesar s 1)) (define (decrypt s) (caesar s -1)) </lang> Example:

> (define s (encrypt "The five boxing wizards jump quickly."))
> s
"Uif gjwf cpyjoh xjabset kvnq rvjdlmz."
> (decrypt s)
"The five boxing wizards jump quickly."

Retro

Retro provides a number of classical cyphers in the crypto' library. This implementation is from the library. <lang Retro>{{

 variable offset
 : rotate  ( cb-c ) tuck - @offset + 26 mod + ;
 : rotate? (  c-c )
   dup 'a 'z within [ 'a rotate ] ifTrue
   dup 'A 'Z within [ 'A rotate ] ifTrue ;

---reveal---

 : ceaser  (  $n-$ )
   !offset dup [ [ @ rotate? ] sip ! ] ^types'STRING each@ ;

}}

( Example ) "THEYBROKEOURCIPHEREVERYONECANREADTHIS" 3 ceaser ( returns encrypted string ) 23 ceaser ( returns decrypted string )</lang>

REXX

only Latin letters

This version conforms to the task's restrictions. <lang rexx>/*REXX pgm: Caesar cypher: Latin alphabet only, no punctuation or blanks*/ /* allowed, all lowercase Latin letters are treated as uppercase. */ arg key p /*get key and text to be cyphered*/ p=space(p,0) /*remove all blanks from text. */

                       say 'Caesar cypher key:' key
                       say '       plain text:' p

y=caesar(p, key)  ; say ' cyphered:' y z=caesar(y,-key)  ; say ' uncyphered:' z if z\==p then say "plain text doesn't match uncyphered cyphered text." exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────CAESAR subroutine───────────────────*/ caesar: procedure; arg s,k; @='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; L=length(@) ak=abs(k) if ak>length(@)-1 | k==0 | k== then call err k 'key is invalid' _=verify(s,@) /*any illegal char specified ? */ if _\==0 then call err 'unsupported character:' substr(s,_,1)

                                      /*now that error checks are done:*/

if k>0 then ky=k+1 /*either cypher it, or ··· */

         else ky=27-ak                /*     decypher it.              */

return translate(s,substr(@||@,ky,L),@) /*──────────────────────────────────ERR subroutine──────────────────────*/ err: say; say '***error!***'; say; say arg(1); say; exit 13</lang>

Output:

when using the input of



22 The definition of a trivial program is one that has no bugs

Caesar cypher key: 22
       plain text: THEDEFINITIONOFATRIVIALPROGRAMISONETHATHASNOBUGS
         cyphered: PDAZABEJEPEKJKBWPNEREWHLNKCNWIEOKJAPDWPDWOJKXQCO
       uncyphered: THEDEFINITIONOFATRIVIALPROGRAMISONETHATHASNOBUGS

almost all characters

This version allows upper and lowercase Latin alphabet as well as all the characters on the standard (computer) keyboard including blanks. <lang rexx>/*REXX pgm: Caesar cypher for almost all keyboard chars including blanks*/ parse arg key p /*get key and text to be cyphered*/

                       say 'Caesar cypher key:' key
                       say '       plain text:' p

y=caesar(p, key)  ; say ' cyphered:' y z=caesar(y,-key)  ; say ' uncyphered:' z if z\==p then say "plain text doesn't match uncyphered cyphered text." exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────CAESAR subroutine───────────────────*/ caesar: procedure; parse arg s,k; @='abcdefghijklmnopqrstuvwxyz' @=translate(@)@'0123456789(){}[]<>' /*add uppercase, digs, group symb*/ @=@'~!@#$%^&*_+:";?,./`-= /*add other characters here. */

                      /*last char is doubled, REXX quoted syntax rules.*/

L=length(@) ak=abs(k) if ak>length(@)-1 | k==0 then call err k 'key is invalid' _=verify(s,@) /*any illegal char specified ? */ if _\==0 then call err 'unsupported character:' substr(s,_,1) if k>0 then ky=k+1

         else ky=L+1-ak

return translate(s,substr(@||@,ky,L),@) /*──────────────────────────────────ERR subroutine──────────────────────*/</lang>

Output:

when using the input of



31 Batman's hood is called a "cowl" (old meaning).

Caesar cypher key: 31
       plain text: Batman's hood is called a "cowl" (old meaning).
         cyphered: g5^>5~e%d{!!8d}%d75<<98d5dU7!_<UdA!<8d>95~}~)BY
       uncyphered: Batman's hood is called a "cowl" (old meaning).

Ruby

Translation of: Perl 6
Works with: Ruby version 1.9.2+

(use of rotate)

<lang ruby>module CaesarCipher

 AtoZ = (0..25).each_with_object({}) do |key,h|
   str = [*"A".."Z"].rotate(key).join
   h[key] = str + str.downcase
 end
 
 def encrypt(key, plaintext)
   (1..25) === key or raise ArgumentError, "key not in 1..25"
   plaintext.tr(AtoZ[0], AtoZ[key])
 end
 
 def decrypt(key, ciphertext)
   (1..25) === key or raise ArgumentError, "key not in 1..25"
   ciphertext.tr(AtoZ[key], AtoZ[0])
 end

end

include CaesarCipher

original = "THEYBROKEOURCIPHEREVERYONECANREADTHIS" en = encrypt(3, original) de = decrypt(3, en)

[original, en, de].each {|e| puts e}

puts 'OK' if

 (1..25).all? {|k| original == decrypt(k, encrypt(k, original))}</lang>

The Ruby translation saves the rotations in an AtoZ hash. (The Perl 6 code called rotate() during each encrypt or decrypt.)

  • AtoZ[0] becomes "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  • AtoZ[3] becomes "DEFGHIJKLMNOPQRSTUVWXYZABCdefghijklmnopqrstuvwxyzabc",
  • and so on.

When the key is 3, encryption uses String#tr(AtoZ[0], AtoZ[3]) to substitute characters. Decryption uses String#tr(AtoZ[3], AtoZ[0]).

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.

Run BASIC

<lang runbasic>input "Gimme a ofset:";ofst ' set any offset you like

a$ = "Pack my box with five dozen liquor jugs" print " Original: ";a$ a$ = cipher$(a$,ofst) print "Encrypted: ";a$ print "Decrypted: ";cipher$(a$,ofst+6)

FUNCTION cipher$(a$,ofst) for i = 1 to len(a$)

 aa$   = mid$(a$,i,1)
 code$ = " "
 if aa$ <> " " then
   ua$   = upper$(aa$)
   a     = asc(ua$) - 64
   code$ = chr$((((a mod 26) + ofst) mod 26) + 65)
   if ua$ <> aa$ then code$ = lower$(code$)
 end if
 cipher$ = cipher$;code$

next i END FUNCTION</lang>

Output:
Gimme a ofset:?9
 Original: Pack my box with five dozen liquor jugs
Encrypted: Zkmu wi lyh gsdr psfo nyjox vsaeyb teqc
Decrypted: Pack my box with five dozen liquor jugs

Rust

This example shows proper error handling. It skips non-ASCII characters. <lang rust>use std::io::{self, Write}; use std::fmt::Display; use std::{env, process};

fn main() {

   let shift: u8 = env::args().nth(1)
       .unwrap_or_else(|| exit_err("No shift provided", 2))
       .parse()
       .unwrap_or_else(|e| exit_err(e, 3));
   let plain = get_input()
       .unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)));
   let cipher = plain.chars()
       .map(|c| {
           let case = if c.is_uppercase() {'A'} else {'a'} as u8;
           if c.is_alphabetic() { (((c as u8 - case + shift) % 26) + case) as char } else { c }
       }).collect::<String>();
   println!("Cipher text: {}", cipher.trim());

}


fn get_input() -> io::Result<String> {

   print!("Plain text:  ");
   try!(io::stdout().flush());
   let mut buf = String::new();
   try!(io::stdin().read_line(&mut buf));
   Ok(buf)

}

fn exit_err<T: Display>(msg: T, code: i32) -> ! {

   let _ = writeln!(&mut io::stderr(), "ERROR: {}", msg);
   process::exit(code);

}</lang>

Scala

<lang scala>object Caesar {

 private val alphaU='A' to 'Z'
 private val alphaL='a' to 'z'
 def encode(text:String, key:Int)=text.map{
   case c if alphaU.contains(c) => rot(alphaU, c, key)
   case c if alphaL.contains(c) => rot(alphaL, c, key)
   case c => c
 }
 def decode(text:String, key:Int)=encode(text,-key)
 private def rot(a:IndexedSeq[Char], c:Char, key:Int)=a((c-a.head+key+a.size)%a.size)

}</lang> <lang scala>val text="The five boxing wizards jump quickly" println("Plaintext => " + text) val encoded=Caesar.encode(text, 3) println("Ciphertext => " + encoded) println("Decrypted => " + Caesar.decode(encoded, 3))</lang>

Output:
Plaintext  => The five boxing wizards jump quickly
Ciphertext => Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted  => The five boxing wizards jump quickly

Alternate version

This version first creates non shifted and shifted character sequences and then encodes and decodes by indexing between those sequences. <lang scala>class Caeser(val key: Int) {

 @annotation.tailrec
 private def rotate(p: Int, s: IndexedSeq[Char]): IndexedSeq[Char] = if (p < 0) rotate(s.length + p, s) else s.drop(p) ++ s.take(p)
 val uc = 'A' to 'Z'
 val lc = 'a' to 'z'
 val as = uc ++ lc
 val bs = rotate(key, uc) ++ rotate(key, lc)
 
 def encode(c: Char) = if (as.contains(c)) bs(as.indexOf(c)) else c
 def decode(c: Char) = if (bs.contains(c)) as(bs.indexOf(c)) else c

}</lang>

<lang scala>val text = "The five boxing wizards jump quickly" val myCaeser = new Caeser(3) val encoded = text.map(c => myCaeser.encode(c)) println("Plaintext => " + text) println("Ciphertext => " + encoded) println("Decrypted => " + encoded.map(c => myCaeser.decode(c)))</lang>

Output:
Plaintext => The five boxing wizards jump quickly
Ciphertext => Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted => The five boxing wizards jump quickly

Scheme

<lang scheme>;

Works with R7RS-compatible Schemes (e.g. Chibi).
Also current versions of Chicken, Gauche and Kawa.

(cond-expand

 (chicken (use srfi-13))
 (gauche  (use srfi-13))
 (kawa    (import (srfi :13)))
 (else    (import (scheme base) (scheme write)))) ; R7RS


(define msg "The quick brown fox jumps over the lazy dog.") (define key 13)

(define (caesar char)

 (define A (char->integer #\A))
 (define Z (char->integer #\Z))
 (define a (char->integer #\a))
 (define z (char->integer #\z))
 (define c (char->integer char))
 (integer->char
   (cond ((<= A c Z) (+ A (modulo (+ key (- c A)) 26)))
         ((<= a c z) (+ a (modulo (+ key (- c a)) 26)))
         (else c)))) ; Return other characters verbatim.

(display (string-map caesar msg)) (newline) </lang>

Output:
Gur dhvpx oebja sbk whzcf bire gur ynml qbt.

sed

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. <lang sed>#!/bin/sed -rf

  1. Input: <number 0..25>\ntext to encode

/^[0-9]+$/ { # validate a number and translate it to analog form s/$/;9876543210dddddddddd/ s/([0-9]);.*\1.{10}(.?)/\2/ s/2/11/ s/1/dddddddddd/g /[3-9]|d{25}d+/ { s/.*/Error: Key must be <= 25/ q } # append from-table s/$/\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/ # .. and to-table s/$/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/ # rotate to-table, lower and uppercase independently, removing one `d' at a time : rotate s/^d(.*\n[^Z]+Z)(.)(.{25})(.)(.{25})/\1\3\2\5\4/ t rotate s/\n// h d }

  1. use \n to mark character to convert

s/^/\n/

  1. append conversion table to pattern space

G

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 loop s/\n\n.*//</lang>

Output:
$ ./caesar.sed 
3
The five boxing wizards jump quickly
Wkh ilyh eralqj zlcdugv mxps txlfnob
23
Wkh ilyh eralqj zlcdugv mxps txlfnob
The five boxing wizards jump quickly
26
Error: Key must be <= 25

Seed7

<lang seed7>$ include "seed7_05.s7i";

const func string: rot (in string: stri, in integer: encodingKey) is func

 result
   var string: encodedStri is "";
 local
   var char: ch is ' ';
   var integer: index is 0;
 begin
   encodedStri := stri;
   for ch key index range stri do
     if ch >= 'a' and ch <= 'z' then
       ch := chr((ord(ch) - ord('a') + encodingKey) rem 26 + ord('a'));
     elsif ch >= 'A' and ch <= 'Z' then
       ch := chr((ord(ch) - ord('A') + encodingKey) rem 26 + ord('A'));
     end if;
     encodedStri @:= [index] ch;
   end for;
 end func;

const proc: main is func

 local
   const integer: exampleKey is 3;
   const string: testText is "The five boxing wizards jump quickly";
 begin
   writeln("Original:  " <& testText);
   writeln("Encrypted: " <& rot(testText, exampleKey));
   writeln("Decrypted: " <& rot(rot(testText, exampleKey), 26 - exampleKey));
 end func;</lang>
Output:
Original:  The five boxing wizards jump quickly
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted: The five boxing wizards jump quickly

SequenceL

You only have to write an encrypt and decrypt function for characters. The semantics of Normalize Transpose allow those functions to be applied to strings. <lang sequencel>import <Utilities/Sequence.sl>; import <Utilities/Conversion.sl>;

lowerAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; upperAlphabet := "abcdefghijklmnopqrstuvwxyz";

caesarEncrypt(ch, key) := let correctAlphabet := lowerAlphabet when some(ch = lowerAlphabet) else upperAlphabet;

index := Sequence::firstIndexOf(correctAlphabet, ch);

newIndex := (index + key - 1) mod 26 + 1; in ch when not(some(ch = lowerAlphabet) or some(ch = upperAlphabet)) else correctAlphabet[newIndex];

caesarDecrypt(ch, key) := caesarEncrypt(ch, 26 - key);

main(args(2)) := let key := Conversion::stringToInt(args[2]); encrypted := caesarEncrypt(args[1], key); decrypted := caesarDecrypt(encrypted, key); in "Input: \t" ++ args[1] ++ "\n" ++ "Encrypted:\t" ++ encrypted ++ "\n" ++ "Decrypted:\t" ++ decrypted;</lang>

Output:
cmd:> main.exe "Pack my box with five dozen liquor jugs." 5
"Original: Pack my box with five dozen liquor jugs.
Encrypted: Ufhp rd gtc bnym knaj itejs qnvztw ozlx.
Decrypted: Pack my box with five dozen liquor jugs."

Sidef

Translation of: Perl

<lang ruby>func caesar(msg, key, decode=false) {

   decode && (key = (26 - key));
   msg.gsub(/([A-Z])/i, {|c| ((c.uc.ord - 65 + key) % 26) + 65 -> chr});

};

var msg = 'THE FIVE BOXING WIZARDS JUMP QUICKLY';

var enc = caesar(msg, 10); var dec = caesar(enc, 10, true);

say "msg: #{msg}"; say "enc: #{enc}"; say "dec: #{dec}";</lang>

Output:
msg: THE FIVE BOXING WIZARDS JUMP QUICKLY
enc: DRO PSFO LYHSXQ GSJKBNC TEWZ AESMUVI
dec: THE FIVE BOXING WIZARDS JUMP QUICKLY

Smalltalk

Works with: Smalltalk/X

well, I'm lucky: the standard library already contains a rot:n method! <lang Smalltalk>'THE QUICK BROWN FOX' rot:3 -> 'WKH TXLFN EURZQ IRA' </lang> but if it wasn't, here is an implementation for other smalltalks: <lang smalltalk> !CharacterArray methodsFor:'encoding'! rot:n

   ^ self class
       streamContents:[:aStream |
           self do:[:char |
               aStream nextPut:(char rot:n) ]]


!Character methodsFor:'encoding'! rot:n

    (self isLetter) ifTrue:[
       self isLowercase ifTrue:[
           ^ 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' at:(self-$a+1+n)
       ] ifFalse:[
           ^ 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ' at:(self-$A+1+n)
       ]
   ].
   ^ self

</lang>

Tcl

<lang tcl>package require Tcl 8.6; # Or TclOO package for 8.5

oo::class create Caesar {

   variable encryptMap decryptMap
   constructor shift {

for {set i 0} {$i < 26} {incr i} { # Play fast and loose with string/list duality for shorter code append encryptMap [format "%c %c %c %c " \ [expr {$i+65}] [expr {($i+$shift)%26+65}] \ [expr {$i+97}] [expr {($i+$shift)%26+97}]] append decryptMap [format "%c %c %c %c " \ [expr {$i+65}] [expr {($i-$shift)%26+65}] \ [expr {$i+97}] [expr {($i-$shift)%26+97}]] }

   }
   method encrypt text {

string map $encryptMap $text

   }
   method decrypt text {

string map $decryptMap $text

   }

}</lang> Demonstrating: <lang tcl>set caesar [Caesar new 3] set txt "The five boxing wizards jump quickly." set enc [$caesar encrypt $txt] set dec [$caesar decrypt $enc] puts "Original message = $txt" puts "Encrypted message = $enc" puts "Decrypted message = $dec"</lang>

Output:
Original message  = The five boxing wizards jump quickly.
Encrypted message = Wkh ilyh eralqj zlcdugv mxps txlfnob.
Decrypted message = The five boxing wizards jump quickly.

TUSCRIPT

<lang tuscript>$$ MODE TUSCRIPT text="THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" PRINT "text orginal ",text

abc="ABCDEFGHIJKLMNOPQRSTUVWXYZ",key=3,caesarskey=key+1 secretbeg=EXTRACT (abc,#caesarskey,0) secretend=EXTRACT (abc,0,#caesarskey) secretabc=CONCAT (secretbeg,secretend)

abc=STRINGS (abc,":</:"),secretabc=STRINGS (secretabc,":</:") abc=SPLIT (abc), secretabc=SPLIT (secretabc) abc2secret=JOIN(abc," ",secretabc),secret2abc=JOIN(secretabc," ",abc)

BUILD X_TABLE abc2secret=* DATA {abc2secret}

BUILD X_TABLE secret2abc=* DATA {secret2abc}

ENCODED = EXCHANGE (text,abc2secret) PRINT "text encoded ",encoded

DECODED = EXCHANGE (encoded,secret2abc) PRINT "encoded decoded ",decoded</lang>

Output:
text orginal    THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
text encoded    WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ
encoded decoded THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG 

TXR

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. <lang txr>@(next :args) @(cases) @{key /[0-9]+/} @text @(or) @ (throw error "specify <key-num> <text>") @(end) @(do

  (defvar k (int-str key 10)))

@(bind enc-dec

      @(collect-each ((i (range 0 25)))
         (let* ((p (tostringp (+ #\a i)))
                (e (tostringp (+ #\a (mod (+ i k) 26))))
                (P (upcase-str p))
                (E (upcase-str e)))
           ^(((,p ,e) (,P ,E))
             ((,e ,p) (,E ,P))))))

@(deffilter enc . @(mappend (fun first) enc-dec)) @(deffilter dec . @(mappend (fun second) enc-dec)) @(output) encoded: @{text :filter enc} decoded: @{text :filter dec} @(end)</lang>

Output:
$ ./txr caesar.txr 12 'Hello, world!'
encoded: Tqxxa, iadxp!
decoded: Vszzc, kcfzr!
$ ./txr caesar.txr 12 'Vszzc, kcfzr!'
encoded: Hello, world!
decoded: Jgnnq, yqtnf!

TypeScript

<lang javascript>function replace(input: string, key: number) : string { return input.replace(/([a-z])/g, ($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 97) % 26 + 97) ).replace(/([A-Z])/g, ($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 65) % 26 + 65)); }

// test var str = 'The five boxing wizards jump quickly'; var encoded = replace(str, 3); var decoded = replace(encoded, -3);

console.log('Enciphered: ' + encoded); console.log('Deciphered: ' + decoded);</lang>

UNIX Shell

Works with: bash

I added a tr function to make this "pure" bash. In practice, you'd remove that function and use the external tr utility. <lang bash>caesar() {

   local OPTIND
   local encrypt n=0
   while getopts :edn: option; do
       case $option in
           e) encrypt=true ;;
           d) encrypt=false ;;
           n) n=$OPTARG ;;
           :) echo "error: missing argument for -$OPTARG" >&2
              return 1 ;;
           ?) echo "error: unknown option -$OPTARG" >&2
              return 1 ;;
       esac
   done
   shift $((OPTIND-1))
   if -z $encrypt ; then
       echo "error: specify one of -e or -d" >&2
       return 1
   fi
   local upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
   local lower=abcdefghijklmnopqrstuvwxyz
   if $encrypt; then
       tr "$upper$lower" "${upper:n}${upper:0:n}${lower:n}${lower:0:n}" <<< "$1"
   else
       tr "${upper:n}${upper:0:n}${lower:n}${lower:0:n}" "$upper$lower" <<< "$1"
   fi

}

tr() {

   local -A charmap
   local i trans line char
   for ((i=0; i<${#1}; i++)); do
       charmap[${1:i:1}]=${2:i:1}
   done
   while IFS= read -r line; do
       trans=""
       for ((i=0; i<${#line}; i++)); do
           char=${line:i:1}
           if [[ -n ${charmap[$char]} ]]; then
               trans+=${charmap[$char]}
           else
               trans+=$char
           fi
       done
       echo "$trans"
   done

}

txt="The five boxing wizards jump quickly." enc=$(caesar -e -n 5 "$txt") dec=$(caesar -d -n 5 "$enc")

echo "original: $txt" echo "encrypted: $enc" echo "decrypted: $dec"</lang>

Output:
original:  The five boxing wizards jump quickly.
encrypted: Ymj knaj gtcnsl bnefwix ozru vznhpqd.
decrypted: The five boxing wizards jump quickly.

Ursala

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. <lang Ursala>#import std

  1. import nat

enc "n" = * -:~&@T ^p(rep"n" ~&zyC,~&)~~K30K31X letters # encryption function dec "n" = * -:~&@T ^p(~&,rep"n" ~&zyC)~~K30K31X letters # decryption function

plaintext = 'the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY'

  1. show+ # exhaustive test

test = ("n". <.enc"n",dec"n"+ enc"n"> plaintext)*= nrange/1 25</lang>

Output:
uif gjwf cpyjoh xjabset kvnq rvjdlmz UIF GJWF CPYJOH XJABSET KVNQ RVJDLMZ
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
vjg hkxg dqzkpi ykbctfu lwor swkemna VJG HKXG DQZKPI YKBCTFU LWOR SWKEMNA
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
wkh ilyh eralqj zlcdugv mxps txlfnob WKH ILYH ERALQJ ZLCDUGV MXPS TXLFNOB
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
xli jmzi fsbmrk amdevhw nyqt uymgopc XLI JMZI FSBMRK AMDEVHW NYQT UYMGOPC
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
ymj knaj gtcnsl bnefwix ozru vznhpqd YMJ KNAJ GTCNSL BNEFWIX OZRU VZNHPQD
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
znk lobk hudotm cofgxjy pasv waoiqre ZNK LOBK HUDOTM COFGXJY PASV WAOIQRE
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
aol mpcl ivepun dpghykz qbtw xbpjrsf AOL MPCL IVEPUN DPGHYKZ QBTW XBPJRSF
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
bpm nqdm jwfqvo eqhizla rcux ycqkstg BPM NQDM JWFQVO EQHIZLA RCUX YCQKSTG
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
cqn oren kxgrwp frijamb sdvy zdrltuh CQN OREN KXGRWP FRIJAMB SDVY ZDRLTUH
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
dro psfo lyhsxq gsjkbnc tewz aesmuvi DRO PSFO LYHSXQ GSJKBNC TEWZ AESMUVI
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
esp qtgp mzityr htklcod ufxa bftnvwj ESP QTGP MZITYR HTKLCOD UFXA BFTNVWJ
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
ftq ruhq najuzs iulmdpe vgyb cguowxk FTQ RUHQ NAJUZS IULMDPE VGYB CGUOWXK
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
gur svir obkvat jvmneqf whzc dhvpxyl GUR SVIR OBKVAT JVMNEQF WHZC DHVPXYL
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
hvs twjs pclwbu kwnofrg xiad eiwqyzm HVS TWJS PCLWBU KWNOFRG XIAD EIWQYZM
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
iwt uxkt qdmxcv lxopgsh yjbe fjxrzan IWT UXKT QDMXCV LXOPGSH YJBE FJXRZAN
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
jxu vylu renydw mypqhti zkcf gkysabo JXU VYLU RENYDW MYPQHTI ZKCF GKYSABO
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
kyv wzmv sfozex nzqriuj aldg hlztbcp KYV WZMV SFOZEX NZQRIUJ ALDG HLZTBCP
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
lzw xanw tgpafy oarsjvk bmeh imaucdq LZW XANW TGPAFY OARSJVK BMEH IMAUCDQ
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
max ybox uhqbgz pbstkwl cnfi jnbvder MAX YBOX UHQBGZ PBSTKWL CNFI JNBVDER
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
nby zcpy vircha qctulxm dogj kocwefs NBY ZCPY VIRCHA QCTULXM DOGJ KOCWEFS
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
ocz adqz wjsdib rduvmyn ephk lpdxfgt OCZ ADQZ WJSDIB RDUVMYN EPHK LPDXFGT
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
pda bera xktejc sevwnzo fqil mqeyghu PDA BERA XKTEJC SEVWNZO FQIL MQEYGHU
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
qeb cfsb ylufkd tfwxoap grjm nrfzhiv QEB CFSB YLUFKD TFWXOAP GRJM NRFZHIV
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
rfc dgtc zmvgle ugxypbq hskn osgaijw RFC DGTC ZMVGLE UGXYPBQ HSKN OSGAIJW
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY
sgd ehud anwhmf vhyzqcr itlo pthbjkx SGD EHUD ANWHMF VHYZQCR ITLO PTHBJKX
the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY

Vedit macro language

This implementation ciphers/deciphers a highlighted block of text in-place in current edit buffer. <lang vedit>#10 = Get_Num("Enter the key: positive to cipher, negative to de-cipher: ", STATLINE)

Goto_Pos(Block_Begin) while(Cur_Pos < Block_End) {

   #11 = Cur_Char & 0x60 + 1
   if (Cur_Char >= 'A') {
       Ins_Char((Cur_Char - #11 + 26 + #10) % 26 + #11, OVERWRITE)
   } else {

Char(1)

   }

}</lang>

Output:

with key 13

Original text:    Quick brown Fox jumps over the lazy Dog.
Encrypted text:   Dhvpx oebja Sbk whzcf bire gur ynml Qbt.
Decrypted text:   Quick brown Fox jumps over the lazy Dog.

Wortel

<lang wortel>@let {

 ; this function only replaces letters and keeps case
 ceasar &[s n] !!s.replace &"[a-z]"gi &[x] [
   @vars {
     t x.charCodeAt.
     l ?{
       && > t 96 < t 123 97
       && > t 64 < t 91 65
       0
     }
   }
   !String.fromCharCode ?{
     l +l ~% 26 -+ n t l
     t
   }
 ]
 !!ceasar "abc $%^ ABC" 10

}</lang> Returns:

"klm $%^ KLM"

XPL0

To decrypt a message use the negative value of the encrypting key. Usage: caesar key <infile.txt >outfile.xxx

<lang XPL0>code ChIn=7, ChOut=8, IntIn=10; int Key, C; [Key:= IntIn(8); repeat C:= ChIn(1);

       if C>=^a & C<=^z then C:= C-$20;
       if C>=^A & C<=^Z then
           [C:= C+Key;
           if C>^Z then C:= C-26
           else if C<^A then C:= C+26;
           ];
       ChOut(0, C);

until C=$1A; \EOF ]</lang>

Example outfile.xxx:

SDFN PB ERA ZLWK ILYH GRCHQ OLTXRU MXJV.

zkl

<lang zkl>var [const] letters= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

fcn caesarCodec(str,n,encode=True){

  if(not encode) n=26-n;
  m:=n+26; sz:=26-n;
  ltrs:=String(letters[n,sz],letters[0,n],letters[m,sz],letters[26,n]);
  str.apply('wrap(c){ try{ ltrs[letters.index(c)] } catch{ c } });

}</lang> <lang zkl>text:="The five boxing wizards jump quickly"; N:=3; code:=caesarCodec(text,N); println("text = ",text); println("encoded(%d) = %s".fmt(N,code)); println("decoded = ",caesarCodec(code,N,False));</lang>

Output:
text = The five boxing wizards jump quickly
encoded(3) = Wkh ilyh eralqj zlcdugv mxps txlfnob
decoded = The five boxing wizards jump quickly