Difference between revisions of "Caesar cipher"

From TheAlmightyGuru
Jump to: navigation, search
(Program)
Line 25: Line 25:
  
 
==Program==
 
==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.
 
  ' This program will encode or decode text by using a Caesar (shift) cipher.
 
  ' Copyright 2019-06-18 - Dean Tersigni
 
  ' Copyright 2019-06-18 - Dean Tersigni
Line 110: Line 112:
 
   
 
   
 
  Sleep
 
  Sleep
 
  
 
==Links==
 
==Links==

Revision as of 13:39, 18 June 2019

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 forward 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 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. The example below uses a shift of 13.

 plaintext: ATTACK TONIGHT
       key: 13
ciphertext: NGGNPX GBAVTUG

Decryption

To decrypt ciphertext that has been encrypted with the Caesar cipher, you need only to rotate the letters in reverse the name number they were reversed forward. If the letters were encrypted with a shift of 1, then, to decrypt the ciphertext, C becomes B, B becomes A, and A becomes Z again.

ciphertext: TLLA HA AOL SHRL
       key: 7
 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.

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.

Variations

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