Caesar cipher: Difference between revisions

 
(19 intermediate revisions by 15 users not shown)
Line 383:
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with-- Caesar Cipher Implementation in Ada.Text_IO;
 
with Ada.Text_IO; use Ada.Text_IO;
procedure Caesar is
 
procedure Caesar is
type modulo26 is modulo 26;
 
-- Base function to encrypt a character
function modulo26 (Character: Character; Output: Character) return modulo26 is
function Cipher(Char_To_Encrypt: Character; Shift: Integer; Base: Character) return Character is
Base_Pos : constant Natural := Character'Pos(Base);
begin
return modulo26 (Character'PosVal((Character)+Character'Pos(OutputChar_To_Encrypt) + Shift - Base_Pos) mod 26 + Base_Pos);
end modulo26Cipher;
 
-- Function to encrypt a character
function Character(Val: in modulo26; Output: Character)
function Encrypt_Char(Char_To_Encrypt: Character; Shift: Integer) return Character is
begin
case Char_To_Encrypt is
return Character'Val(Integer(Val)+Character'Pos(Output));
when 'A'..'Z' =>
end Character;
-- Encrypt uppercase letters
 
return Cipher (Char_To_Encrypt, Shift, 'A');
function crypt (Playn: String; Key: modulo26) return String is
Ciph: String(Playn when 'Range);a'..'z' =>
-- Encrypt lowercase letters
return Cipher (Char_To_Encrypt, Shift, 'a');
when others =>
-- Leave other characters unchanged
return Char_To_Encrypt;
end case;
end Encrypt_Char;
 
-- Function to decrypt a character
function Decrypt_Char(Char_To_Decrypt: Character; Shift: Integer) return Character is
begin
return Encrypt_Char(Char_To_Decrypt, -Shift);
for I in Playn'Range loop
end Decrypt_Char;
case Playn(I) is
when 'A' .. 'Z' =>
Ciph(I) := Character(modulo26(Playn(I)+Key), 'A');
when 'a' .. 'z' =>
Ciph(I) := Character(modulo26(Playn(I)+Key), 'a');
when others =>
Ciph(I) := Playn(I);
end case;
end loop;
return Ciph;
end crypt;
 
TextMessage: constant String := Ada.Text_IO.Get_Line;
KeyShift: modulo26Positive := 3; -- Default key from "Commentarii de Bello Gallico" shift cipher
-- Shift value (can be any positive integer)
 
Encrypted_Message: String(Message'Range);
begin -- encryption main program
Decrypted_Message: String(Message'Range);
begin
-- Encrypt the message
for I in Message'Range loop
Encrypted_Message(I) := Encrypt_Char(Message(I), Shift);
end loop;
 
-- Decrypt the encrypted message
Ada.Text_IO.Put_Line("Playn ------------>" & Text);
for I in Message'Range loop
Text := crypt(Text, Key);
Decrypted_Message(I) := Decrypt_Char(Encrypted_Message(I), Shift);
Ada.Text_IO.Put_Line("Ciphertext ----------->" & Text);
end loop;
Ada.Text_IO.Put_Line("Decrypted Ciphertext ->" & crypt(Text, -Key));
 
-- Display results
end Caesar;</syntaxhighlight>
Put_Line("Plaintext: " & Message);
Put_Line("Ciphertext: " & Encrypted_Message);
Put_Line("Decrypted Ciphertext: " & Decrypted_Message);
 
end Caesar;
</syntaxhighlight>
{{out}}
<pre>> ./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</pre>
</pre>
 
=={{header|ALGOL 68}}==
{{trans|Ada|Note: This specimen retains the original [[#Ada|Ada]] coding style.}}
Line 1,260 ⟶ 1,276:
Enter ciphertext > ? HT CJQZMXMVAO DN APGG JA ZZGN.
MY HOVERCRAFT IS FULL OF EELS.</pre>
 
==={{header|GW-BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
{{trans|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">10 REM Caesar cipher
20 CLS
30 DEC$ = ""
40 TYPE$ = "cleartext "
50 PRINT "If decrypting enter "+"<d> "+" -- else press enter "; : INPUT DEC$
60 INPUT "Enter offset > "; IOFFSET
70 IF DEC$ = "d" THEN IOFFSET = 26-IOFFSET: TYPE$ = "ciphertext "
110 PRINT "Enter "+TYPE$+"> ";
120 INPUT CAD$
140 LONGITUD = LEN(CAD$)
150 FOR I = 1 TO LONGITUD
160 ITEMP = ASC(MID$(CAD$,I,1))
170 IF ITEMP > 64 AND ITEMP < 91 THEN ITEMP = ((ITEMP-65)+IOFFSET) MOD 26 : PRINT CHR$(ITEMP+65); : ELSE PRINT CHR$(ITEMP);
230 NEXT I
240 PRINT
250 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
=={{header|Beads}}==
Line 1,836 ⟶ 1,879:
=> "The Quick Brown Fox jumped over the lazy dog"
</pre>
 
Fast version, using str/escape:
<syntaxhighlight lang="clojure">(defn fast-caesar [n s]
(let [m (mod n 26)
upper (map char (range 65 91))
upper->new (zipmap upper (drop m (cycle upper)))
lower (map char (range 97 123))
lower->new (zipmap lower (drop m (cycle lower)))]
(clojure.string/escape s (merge upper->new lower->new))))</syntaxhighlight>
output:<pre>
(fast-caesar 12 "The Quick Brown Fox jumped over the lazy dog")
=> "Ftq Cguow Ndaiz Raj vgybqp ahqd ftq xmlk pas"
</pre>
 
=={{header|COBOL}}==
[[COBOL-85]] ASCII or EBCIDIC
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
identificationPROGRAM-ID. divisionCAESAR.
program-id. caesar.
data division.
1 msg pic x(50)
value "The quick brown fox jumped over the lazy dog.".
1 offset binary pic 9(4) value 7.
1 from-chars pic x(52).
1 to-chars pic x(52).
1 tabl.
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
2 pic x(26) value "abcdefghijklmnopqrstuvwxyz".
2 pic x(26) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
procedure division.
begin.
display msg
perform encrypt
display msg
perform decrypt
display msg
stop run
.
 
encryptDATA DIVISION.
WORKING-STORAGE SECTION.
move tabl (1:52) to from-chars
01 MSG move tabl (1 + offset:52) to to-charsPIC X(50)
VALUE "The quick brown fox jumped over the lazy dog.".
inspect msg converting from-chars
01 OFFSET PIC to9(4) to-charsVALUE 7 USAGE BINARY.
01 FROM-CHARS PIC X(52).
01 TO-CHARS PIC X(52).
01 TABL.
02 PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
02 PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
02 PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
02 PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
 
decryptPROCEDURE DIVISION.
BEGIN.
move tabl (1 + offset:52) to from-chars
moveDISPLAY tabl (1:52) to to-charsMSG
inspectPERFORM msg converting from-charsENCRYPT
DISPLAY to to-charsMSG
.PERFORM DECRYPT
end program caesar. DISPLAY MSG
STOP RUN.
</syntaxhighlight>
 
ENCRYPT.
MOVE TABL (1:52) TO FROM-CHARS
MOVE TABL (1 + OFFSET:52) TO TO-CHARS
INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.
 
DECRYPT.
MOVE TABL (1 + OFFSET:52) TO FROM-CHARS
MOVE TABL (1:52) TO TO-CHARS
INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.
 
END PROGRAM CAESAR.</syntaxhighlight>
{{out}}
<pre>
Line 1,884 ⟶ 1,938:
</pre>
 
{{works with|OpenCOBOL|2.0COBOL 2002}}
<syntaxhighlight lang="cobolcobolfree"> >>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. caesar-cipher.
 
Line 1,892 ⟶ 1,947:
REPOSITORY.
FUNCTION encrypt
FUNCTION decrypt.
 
.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 plaintext PIC X(50).
01 offset PICUSAGE 99BINARY-CHAR.
 
01 encrypted-str PIC X(50).
 
PROCEDURE DIVISION.
DISPLAY "Enter a message to encrypt: " WITH NO ADVANCING
ACCEPT plaintext
DISPLAY "Enter the amount to shift by: " WITH NO ADVANCING
ACCEPT offset
MOVE encrypt(offset, plaintext) TO encrypted-str
DISPLAY "Encrypted: " encrypted-str
DISPLAY "Decrypted: " decrypt(offset, encrypted-str).
 
MOVE FUNCTION encrypt(offset, plaintext) TO encrypted-str
DISPLAY "Encrypted: " encrypted-str
DISPLAY "Decrypted: " FUNCTION decrypt(offset, encrypted-str)
.
END PROGRAM caesar-cipher.
 
IDENTIFICATION DIVISION.
 
FUNCTION-ID. encrypt.
 
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
 
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 i PICUSAGE 9(3)INDEX.
01 a USAGE BINARY-CHAR.
 
01 a PIC 9(3).
 
LINKAGE SECTION.
01 offset PICUSAGE 99BINARY-CHAR.
01 str PIC X(50).
 
01 encrypted-str PIC X(50).
 
PROCEDURE DIVISION USING offset, str RETURNING encrypted-str.
PERFORM VARYING i FROM 1 BY 1 UNTIL i > LENGTH(str)
MOVE str TO encrypted-str
PERFORM VARYING i FROM 1 BYIF str(i:1) UNTILIS iNOT >ALPHABETIC FUNCTIONOR LENGTH(str(i:1) = SPACE
IF encrypted- MOVE str (i:1) IS NOT ALPHABETIC ORTO encrypted-str (i:1) = SPACE
EXIT PERFORM CYCLE
END-IF
IF str(i:1) IS ALPHABETIC-UPPER
 
IF encrypted-str MOVE ORD(i:1"A") ISTO ALPHABETIC-UPPERa
MOVE FUNCTION ORD("A") TO a
ELSE
MOVE FUNCTION ORD("a") TO a
END-IF
MOVE CHAR(MOD(ORD(str(i:1)) - a + offset, 26) + a)
 
MOVE FUNCTION CHAR(FUNCTION MOD(FUNCTION ORD(TO encrypted-str (i:1))
- a + offset, 26) + a)
TO encrypted-str (i:1)
END-PERFORM
EXIT FUNCTION.
END FUNCTION encrypt.
 
END FUNCTION encrypt.
 
IDENTIFICATION DIVISION.
FUNCTION-ID. decrypt.
 
Line 1,954 ⟶ 2,007:
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION encrypt.
 
.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 decrypt-offset PICUSAGE 99BINARY-CHAR.
 
LINKAGE SECTION.
01 offset PICUSAGE 99BINARY-CHAR.
01 str PIC X(50).
 
01 decrypted-str PIC X(50).
 
PROCEDURE DIVISION USING offset, str RETURNING decrypted-str.
SUBTRACT 26offset FROM offset26 GIVING decrypt-offset
MOVE FUNCTION encrypt(decrypt-offset, str) TO decrypted-str
EXIT FUNCTION.
 
END FUNCTION decrypt.</syntaxhighlight>
 
Line 1,979 ⟶ 2,031:
Decrypted: The quick brown fox jumps over the lazy dog.
</pre>
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">cipher = (msg, rot) ->
Line 2,404 ⟶ 2,457:
Encrypted: Ufhp rd gtc bnym knaj itejs qnvztw ozlx.
Decrypted: Pack my box with five dozen liquor jugs.</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func$ crypt str$ key .
for c$ in strchars str$
c = strcode c$
if c >= 65 and c <= 90
c = (c + key - 65) mod 26 + 65
elif c >= 97 and c <= 122
c = (c + key - 97) mod 26 + 97
.
enc$ &= strchar c
.
return enc$
.
enc$ = crypt "Rosetta Code" 4
print enc$
print crypt enc$ -4
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
The EDSAC had only upper-case letters, which were represented by 5-bit codes.
Line 2,646 ⟶ 2,718:
ENTER MESSAGE
</pre>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
Line 2,782 ⟶ 2,855:
if (-1 < index)
{
^ Letters[(_key+index).mod:(26)]
}
else
Line 2,789 ⟶ 2,862:
if (-1 < index)
{
^ BigLetters[(_key+index).mod:(26)]
}
else
Line 2,812 ⟶ 2,885:
console.printLine("Original text :",TestText);
var encryptedText := TestText.encrypt:(Key);
 
console.printLine("Encrypted text:",encryptedText);
 
var decryptedText := encryptedText.decrypt:(Key);
 
console.printLine("Decrypted text:",decryptedText);
Line 2,855 ⟶ 2,928:
Decrypted: The five boxing wizards jump quickly
</pre>
=={{header|Elm}}==
<syntaxhighlight lang="elm">
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
module Main exposing (main)
 
import Browser
import Html exposing (Html, h2, div, p, input, button, text, textarea)
import Html.Attributes exposing (style, class, placeholder, value)
import Html.Events exposing (onInput, onClick)
 
 
-- MAIN
 
main =
Browser.sandbox { init = init, update = update, view = view }
 
 
-- MODEL
 
type alias Model =
{ codeKey : Int
, normtext : String
}
 
 
init : Model
init =
{ codeKey = 3
, normtext = "" }
 
 
-- UPDATE
 
type Msg
= Change String
| DecKey
| IncKey
 
 
update : Msg -> Model -> Model
update msg model =
case msg of
DecKey ->
{ model | codeKey = model.codeKey - 1}
IncKey ->
{ model | codeKey = model.codeKey + 1}
Change newContent ->
{ model | normtext = String.toUpper newContent }
 
 
 
encodeChar : Int -> Char -> Char
encodeChar codeKey character =
if Char.isUpper character then
Char.fromCode (modBy 26 (Char.toCode character - 65 + codeKey) + 65)
 
else if Char.isLower character then
Char.fromCode (modBy 26 (Char.toCode (Char.toUpper character) - 65 + codeKey) + 65)
 
else
character
 
 
encodeText : Int -> String -> String
encodeText codeKey normtext =
String.map (encodeChar codeKey) normtext
 
 
subheads aKey =
if aKey > 0 then "Original"
else if aKey < 0 then "Encrypted text"
else "?"
 
-- VIEW
 
view : Model -> Html Msg
view model =
div []
[ div []
[h2 [class "h2style"] [text (subheads model.codeKey)]
, textarea [ class "textframe", placeholder "Write here your text!"
, value model.normtext, onInput Change ] []
]
, div []
[h2 [class "h2style"] [text (subheads (negate model.codeKey))]
, textarea [ class "textframe", value (encodeText model.codeKey model.normtext) ] []]
, div [class "keystyle"] [ button [ class "butstyle", onClick DecKey ] [ text "-1" ]
, text (" Key " ++ String.fromInt model.codeKey)
, button [ class "butstyle", onClick IncKey ] [ text "+1" ]
]
]
 
</syntaxhighlight>
 
{{out}}
<pre>
Original: THE QUICK ORANGE FOX JUMPED OVER THE SLOW DOG
Encrypted: WKH TXLFN RUDQJH IRA MXPSHG RYHU WKH VORZ GRJ (used key value 3)
Encrypted: XLI UYMGO SVERKI JSB NYQTIH SZIV XLI WPSA HSK (used key value 4)
Decrypted: THE QUICK ORANGE FOX JUMPED OVER THE SLOW DOG (used key value -3)
Decrypted: THE QUICK ORANGE FOX JUMPED OVER THE SLOW DOG (used key value -4)
 
Live version with html file calling the elm module as compiled to JavaScript
at https://ellie-app.com/qVvVDKdK6PQa1
</pre>
%% Ceasar cypher in Erlang for the rosetta code wiki.
%% Implemented by J.W. Luiten
Line 3,174 ⟶ 3,351:
</syntaxhighlight>
=={{header|Forth}}==
<syntaxhighlight lang="forth">: ceasarcaesar ( c n -- c )
over 32 or [char] a -
dup 0 26 within if
Line 3,180 ⟶ 3,357:
else 2drop then ;
 
: ceasarcaesar-string ( n str len -- )
over + swap do i c@ over ceasarcaesar i c! loop drop ;
: ceasarcaesar-inverse ( n -- 'n ) 26 swap - 26 mod ;
 
2variable test
s" The five boxing wizards jump quickly!" test 2!
 
3 test 2@ ceasarcaesar-string
test 2@ cr type
 
3 ceasarcaesar-inverse test 2@ ceasarcaesar-string
test 2@ cr type</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortan 90 and later}}
Line 3,701 ⟶ 3,879:
Encphered text = "wkh txlfn eurzq ira mxpshg ryhu wkh odcb grj"
Decphered text = "the quick brown fox jumped over the lazy dog"</pre>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(function caeser by of
(let alphabets (proj (map char-code) (range 65 91) (range 97 123))
shifted (.. vec (map (rotate by) alphabets))
table (kv-dict (flatten alphabets) (flatten shifted)))
(... str (map #(or (table %) %) of)))
 
(let original "The Quick Brown Fox Jumps Over The Lazy Dog."
encrypted (caeser -1 original)
decrypted (caeser 1 encrypted))
(str "Original: " original "
Encrypted: " encrypted "
Decrypted: " decrypted)
</syntaxhighlight>
 
{{out}}
 
<pre>
Original: The Quick Brown Fox Jumps Over The Lazy Dog.
Encrypted: Sgd Pthbj Aqnvm Enw Itlor Nudq Sgd Kzyx Cnf.
Decrypted: The Quick Brown Fox Jumps Over The Lazy Dog.
</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "CaesarCi.bas"
Line 3,835 ⟶ 4,039:
<syntaxhighlight lang="javascript">function caesar (text, shift) {
return text.toUpperCase().replace(/[^A-Z]/g,'').replace(/./g, function(a) {
return String.fromCharCode(65+((a.charCodeAt(0) - 65 + shift) % 26 + 26) % 26 + 65);
});
}
Line 3,860 ⟶ 4,064:
.replace(/[^A-Z]/g, '')
.replace(/./g, a =>
String.fromCharCode(65 + ((a.charCodeAt(0) - 65 + shift) % 26 + 26) % 26 + 65));</syntaxhighlight>
 
 
Line 4,042 ⟶ 4,246:
"uifsf jt b ujef jo uif bggbjst pg nfo"
</syntaxhighlight>
=={{header|Koka}}==
<syntaxhighlight lang="koka">
fun encode(s : string, shift : int)
fun encode-char(c)
if c < 'A' || (c > 'Z' && c < 'a') || c > 'z' return c
val base = if c < 'Z' then (c - 'A').int else (c - 'a').int
val rot = (base + shift) % 26
(rot.char + (if c < 'Z' then 'A' else 'a'))
s.map(encode-char)
 
fun decode(s: string, shift: int)
s.encode(0 - shift)
 
fun trip(s: string, shift: int)
s.encode(shift).decode(shift)
 
fun main()
"HI".encode(2).println
"HI".encode(20).println
"HI".trip(10).println
val enc = "The quick brown fox jumped over the lazy dog".encode(11)
enc.println
enc.decode(11).println
</syntaxhighlight>
{{out}}
<pre>
JK
BC
HI
Esp bftnv mczhy qzi ufxapo zgpc esp wlkj ozr
The quick brown fox jumped over the lazy dog
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.5-2
Line 4,084 ⟶ 4,321:
Bright vixens jump; dozy fowl quack.
</pre>
 
=={{header|LabVIEW}}==
For readability, input is in all caps.<br/>{{VI snippet}}<br/>[[File:LabVIEW_Caesar_cipher.png]]
Line 4,162 ⟶ 4,400:
Using the built-in rotate() function on a number over a range, a number outside of the range will pass through unaltered.
 
<syntaxhighlight lang="langur">val .rot = ffn(.s, .key) {
cp2s map(ffn(.c) { rotate(rotate(.c, .key, 'a'..'z'), .key, 'A'..'Z') }, s2cp .s)
}
 
 
val .s = "A quick brown fox jumped over something."
Line 4,177 ⟶ 4,416:
encrypted: X nrfzh yoltk clu grjmba lsbo pljbqefkd.
decrypted: A quick brown fox jumped over something.</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">key = 7
Line 5,456 ⟶ 5,696:
=={{header|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.
<syntaxhighlight lang="purebasicbasic">Procedure.s CC_encrypt(plainText.s, key, reverse = 0)
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
Line 5,519 ⟶ 5,760:
Wend
ProcedureReturn result
EndProcedure</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Python}}==
<syntaxhighlight lang="python">def caesar(s, k, decode = False):
Line 6,611 ⟶ 6,854:
Decrypted message = The five boxing wizards jump quickly.
</pre>
 
=={{header|True BASIC}}==
{{works with|QBasic}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">LET dec$ = ""
LET tipo$ = "cleartext "
 
PRINT "If decrypting enter 'd' -- else press enter ";
INPUT dec$
PRINT "Enter offset ";
INPUT llave
 
IF dec$ = "d" THEN
LET llave = 26 - llave
LET tipo$ = "ciphertext "
END IF
 
PRINT "Enter "; tipo$;
INPUT cadena$
 
LET cadena$ = UCASE$(cadena$)
LET longitud = LEN(cadena$)
 
FOR i = 1 TO longitud
!LET iTemp = ASC(MID$(cadena$,i,1)) 'QBasic
LET itemp = ORD((cadena$)[i:i+1-1][1:1]) !'True BASIC
IF iTemp > 64 AND iTemp < 91 THEN
!LET iTemp = ((iTemp - 65) + llave) MOD 26 'QBasic
LET iTemp = MOD(((iTemp - 65) + llave), 26) !'True BASIC
PRINT CHR$(iTemp + 65);
ELSE
PRINT CHR$(iTemp);
END IF
NEXT i
END</syntaxhighlight>
 
=={{header|TUSCRIPT}}==
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
Line 7,040 ⟶ 7,320:
import rand
 
const (
(
lo_abc = 'abcdefghijklmnopqrstuvwxyz'
up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
Line 7,067 ⟶ 7,346:
if nchr > u8(122) {nchr -= 26}
}
else {nchr = chr}
nchr = chr
}
chr_arr << nchr
}
Line 7,109 ⟶ 7,386:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class Caesar {
static encrypt(s, key) {
var offset = key % 26
Line 7,141 ⟶ 7,418:
Bright vixens jump; dozy fowl quack.
</pre>
 
=={{header|X86 Assembly}}==
{{trans|C custom implementation}}
Line 7,454 ⟶ 7,732:
 
You are encouraged to solve this task according to the task description, using any language you may know.</pre>
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = @import("std").io.getStdOut().writer();
 
pub fn rot(txt: []u8, key: u8) void {
for (txt, 0..txt.len) |c, i| {
if (std.ascii.isLower(c)) {
txt[i] = (c - 'a' + key) % 26 + 'a';
} else if (std.ascii.isUpper(c)) {
txt[i] = (c - 'A' + key) % 26 + 'A';
}
}
}
 
pub fn main() !void {
const key = 3;
var txt = "The five boxing wizards jump quickly".*;
 
try stdout.print("Original: {s}\n", .{txt});
rot(&txt, key);
try stdout.print("Encrypted: {s}\n", .{txt});
rot(&txt, 26 - key);
try stdout.print("Decrypted: {s}\n", .{txt});
}
</syntaxhighlight>
{{out}}
<pre>
Original: The five boxing wizards jump quickly
Encrypted: Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted: The five boxing wizards jump quickly
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn caesarCodec(str,n,encode=True){
Line 7,474 ⟶ 7,785:
decoded = The five boxing wizards jump quickly
</pre>
 
=={{header|zonnon}}==
<syntaxhighlight lang="zonnon">
885

edits