# Difference between revisions of "Caesar cipher"

A ring model depicting a Caesar cipher rotated forward 4 letters so that A will encipher to E, B to F, and so forth.

A Caesar cipher, or shift cipher is a primitive form of encryption named after Julius Caesar who used the algorithm to encrypt his letters. The algorithm turns plaintext into ciphertext by shifting the letters of the plaintext forward along the alphabet. The cipher can be adjusted to work with any alphabet for any language. When using the English alphabet, the cipher is commonly called "ROT13" or "rotate 13," which shifts each letter in the plaintext forward 13 values in the alphabet.

## Encryption

To encrypt text using the Caesar cipher, first, choose the number of letters in you will be shifting the alphabet. Next, simply rotate each letter in the plaintext along the alphabet that number of letters. If you reach the end of the alphabet, rotate back to the beginning. For example, using a Caesar cipher with a shift forward of 1, A becomes B, B becomes C, C becomes D, and so forth until you get to Z, which rotates back to the beginning of the alphabet and becomes A. Since you can also rotate either direction along the alphabet, the direction must be included as part of the key. Generally all non-letters (spaces, punctuation, etc.) are removed from the plain text before encryption, but I've left them in my examples for simplicity's sake. The example below uses a shift of 13 forward.

``` plaintext: ATTACK TONIGHT.
key: 13 forward
ciphertext: NGGNPX GBAVTUG.
```

## Decryption

To decrypt ciphertext that has been encrypted with the Caesar cipher, you need only to rotate the letters the same number of places in the opposite direction in which they were shifted in the encryption process. If the letters were encrypted with a shift of 1 place forward, then, to decrypt the ciphertext, you rotate 1 backward, so C becomes B, B becomes A, and A becomes Z again.

```ciphertext: TLLA HA AOL SHRL.
key: 7 forward
plaintext: MEET AT THE LAKE.
```

## Benefits

• The biggest benefit of the Caesar cipher is how easy it is to use. A short message can be encrypted and decrypted in your head, and even longer messages only need a paper and pencil. The cipher doesn't require a computer, it doesn't rely on complex mathematics, and it doesn't need sufficiently random values.
• The ciphertext is well enough obfuscated that it is unreadable to most people at a glance. This makes it useful for displaying text in public while preventing a passerby from casually understanding it. It is frequently used on forums and in hint books to hide content spoilers, and used on things like children's decoder rings to teach them basic cryptography.

## Deficiencies

• Unfortunately, the simplicity of the Caesar cipher is its downfall; the encryption is trivial to crack. Even without a computer, a brute force attack can be made of all possible shifts in a short period of time, and, since the shift is constant throughout the entire message, only the correct key will produce an intelligible plaintext. This pretty much guarantees that even a novice can crack the encryption in a short amount of time, and, a computer which can analyze the frequency of letters, can usually determine which of the possible outcomes is the correct one in under a second.
• Even if the encryption couldn't be easily cracked, this is a form of private key encryption. The key must be known by the recipient of the ciphertext, so it could be intercepted, or the recipient could be persuaded to reveal it. Such a deficiency is solved with public key encryption.

## Variations

The Caesar cipher doesn't have to use a basic alphabet. For English, it can be expanded to include both upper and lower letter, numbers, punctuation, various other symbols, etc., but both sender and receiver must know the order of the alphabet. Likewise, alphabets from foreign languages can be used.

A Caesar cipher uses the same shift for all letters in the message, but there are variations where the shift changes from letter to letter, either through incrementing, using a modulo, or based on a randomized dictionary like the uncrackable one-time pad.

## Program

This FreeBASIC program will encode plaintext and decode ciphertext to any specified shift amount.

```' This program will encode or decode text by using a Caesar (shift) cipher.
' Copyright 2019-06-18 - Dean Tersigni

Dim As Byte Rotate
Dim As String PlainText
Dim As String CipherText
Dim As String Choice
Dim As Integer Place
Dim As String Letter
Dim As UShort Code

Do
Input "(E)ncode or (D)ecode? ", Choice
Print

Choice = UCase(Choice)

Select Case Choice
Case "E", "D"
Exit Do
Case Else
Print "Please enter E or D."
Print
End Select
Loop

If Choice = "E" Then
Input "Type the plaintext to encode: ", PlainText
PlainText = UCase(PlainText)

Do
Input "How much to rotate forward (1-25)? ", Rotate
Print

If Rotate > 0 And Rotate < 26 Then
Exit Do
Else
Print "Please enter a number from 1 to 25."
End If
Loop

For Place = 1 To Len(PlainText)
Letter = Mid(PlainText, Place, 1)
Code = ASC(Letter)
If Code > 64 And Code < 91 Then
Code = Code + Rotate
If Code > 90 Then
Code = Code - 26
End If
End If
CipherText = CipherText + Chr(Code)
Next Place

Print "Your ciphertext is: " + CipherText
Else
Input "Enter the ciphertext to decode: ", CipherText
CipherText = UCase(CipherText)

Do
Input "How much to rotate backward (1-25)? ", Rotate
Print

If Rotate > 0 And Rotate < 26 Then
Exit Do
Else
Print "Please enter a number from 1 to 25."
End If
Loop

For Place = 1 To Len(CipherText)
Letter = Mid(CipherText, Place, 1)
Code = ASC(Letter)
If Code > 64 And Code < 91 Then
Code = Code - Rotate
If Code < 65 Then
Code = Code + 26
End If
End If
PlainText = PlainText + Chr(Code)
Next Place

Print "Your plaintext is: " + PlainText
End If

Sleep
```