Coding · ·

Caesar

Info: Those interested in ciphers should consider competing in the National Cipher Challenge that starts this month.

Supposedly, Caesar used to 'encrypt' confidential messages by shifting each letter by some number of places. For instance, he might write A as B, B as C, C as D, ... and, wrapping around alphabetically, Z as A. The message recipient would decrypt the message by shifting letters in the opposite direction. This is a very basic type of substitution cipher.

Unencrypted text is generally called plaintext, and encrypted text ciphertext. The secret (in this case, the number of letters to shift by) is called the key. Plaintext is usually written in lowercase and ciphertext in uppercase.

Encrypting hello with a key of 2 yields JGNNQ. Encrypting zero with a key of 3 yields CHUR.

In a file called caesar.js in a folder called caesar, write a program that enables you to encrypt messages using Caesar's cipher as follows:

Here is an example session with the program:

 
❯ bun caesar.js hi
Provide a key (integer) as an argument

❯ bun caesar.js 3.4
Provide a key (integer) as an argument

❯ bun caesar.js 2
plaintext:  hello
ciphertext: JGNNQ

❯ bun caesar.js 2049
plaintext:  excellent
ciphertext: ZSXZGGZIO

❯ bun caesar.js -1
plaintext:  excellent work! 123 :)
ciphertext: DWBDKKDMS VNQJ! 123 :)

Tips