Caesar cipher

From TheAlmightyGuru
Jump to: navigation, search

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 turn 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 used in English, 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 in 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. You can also rotate backward along the alphabet, but, either way, the direction should be included in the key. 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 they were shifted in the encryption process. If the letters were encrypted with a shift of 1 forwards, then, to decrypt the ciphertext, 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, rely on complex mathematics, or use random values.
  • The ciphertext is well enough obfuscated that it is unreadable to most people at a glance. This makes it useful for display text in public while preventing a passerby from casually understanding it. It is frequently used on forums to hide content spoilers.

Deficiencies

  • Unfortunately, the simplicity of the Caesar cipher is its downfall. The encryption is trivial to decrypt without the key. 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.
  • Even if the encryption were strong, and the key was necessary to decrypt the message, the key must be known by the recipient of the ciphertext, so the key could be intercepted beforehand, 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 the basic alphabet, it can be expanded to include both upper and lower letter, numbers, punctuation, various other symbols, etc. 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. See one-time pad for a more complicated version.

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

Links

Link-Wikipedia.png