Skip to content

Caesar Cipher in Code

Write it yourself in the language you are learning, or plug the free API or JavaScript library into your own project.

Step-by-step tutorials

Ready to use

  • REST API

    Caesar, ROT13 and Vigenère over HTTP, with no sign-up.

  • Caesar Cipher JS Library

    A JavaScript and TypeScript package with no dependencies, for Node.js and browsers.

Check your output

  • Caesar Cipher Tool

    Run the same text and shift here and compare the result with your program.

advertisement

The same idea in every language

Each tutorial comes down to one calculation. Turn the letter into a number from 0 to 25, add the shift, wrap around with modulo, then turn the number back into a letter:

E(x) = (x + k) mod 26

Decrypting subtracts the shift instead, and that is where most bugs appear. In C, Java, JavaScript and PHP the % operator can return a negative number, while Python’s cannot, so outside Python you write (x - k + 26) % 26.

Tutorial, API or library?

Follow a tutorial when you want to understand the code or have an assignment to hand in. The REST API suits a project in any language: it takes a plain HTTP request, needs no sign-up, and allows 60 requests per minute per IP address. For JavaScript and TypeScript projects, the caesar-cipher-js library saves you the request.

Whichever you pick, this is code for learning and puzzles. Passwords and user data need modern encryption from a maintained library.

Other sections

Cipher Tools

Encrypt, decrypt and crack text in your browser.

Cryptography Basics

How ciphers work and where they came from.

Cryptanalysis

How to read a message without the key.