Caesar Cipher Implementation

Classic encryption algorithm with customizable shift value

By Cojocaru David29d ago (Sep 13, 2025)
tutorial
lua
algorithms

Caesar Cipher Implementation

lua
-- Caesar Cipher Implementation in Lua

local CaesarCipher = {}
CaesarCipher.__index = CaesarCipher

function CaesarCipher.new(shift)
    local self = setmetatable({}, CaesarCipher)
    self.shift = shift or 3
    return self
end

function CaesarCipher:encrypt(text)
    local result = {}
    
    for i = 1, #text do
        local char = text:sub(i, i)
        local encrypted_char = self:shift_character(char, self.shift)
        table.insert(result, encrypted_char)
    end
    
    return table.concat(result)
end

function CaesarCipher:decrypt(text)
    local result = {}
    
    for i = 1, #text do
        local char = text:sub(i, i)
        local decrypted_char = self:shift_character(char, -self.shift)
        table.insert(result, decrypted_char)
    end
    
    return table.concat(result)
end

function CaesarCipher:shift_character(char, shift)
    local byte_val = string.byte(char)
    
    -- Handle uppercase letters (A-Z)
    if byte_val >= 65 and byte_val <= 90 then
        local shifted = ((byte_val - 65 + shift) % 26 + 26) % 26
        return string.char(shifted + 65)
    end
    
    -- Handle lowercase letters (a-z)
    if byte_val >= 97 and byte_val <= 122 then
        local shifted = ((byte_val - 97 + shift) % 26 + 26) % 26
        return string.char(shifted + 97)
    end
    
    -- Return unchanged if not a letter
    return char
end

-- Brute force decryption (try all possible shifts)
function CaesarCipher.brute_force_decrypt(encrypted_text)
    print("Brute force decryption attempts:")
    print("=================================")
    
    for shift = 1, 25 do
        local cipher = CaesarCipher.new(shift)
        local decrypted = cipher:decrypt(encrypted_text)
        print(string.format("Shift %2d: %s", shift, decrypted))
    end
end

-- Example usage
local function main()
    local cipher = CaesarCipher.new(13) -- ROT13
    local original_message = "Hello, World! This is a secret message."
    
    print("Original message: " .. original_message)
    
    local encrypted = cipher:encrypt(original_message)
    print("Encrypted message: " .. encrypted)
    
    local decrypted = cipher:decrypt(encrypted)
    print("Decrypted message: " .. decrypted)
    
    print("\n" .. string.rep("-", 50))
    
    -- Demonstrate different shift values
    local test_message = "ATTACKATDAWN"
    print("Testing with message: " .. test_message)
    
    for shift_val = 1, 5 do
        local test_cipher = CaesarCipher.new(shift_val)
        local test_encrypted = test_cipher:encrypt(test_message)
        print(string.format("Shift %d: %s", shift_val, test_encrypted))
    end
    
    print("\n" .. string.rep("-", 50))
    
    -- Brute force example
    local mystery_cipher = CaesarCipher.new(7)
    local mystery_encrypted = mystery_cipher:encrypt("THE QUICK BROWN FOX")
    print("Mystery encrypted text: " .. mystery_encrypted)
    print()
    CaesarCipher.brute_force_decrypt(mystery_encrypted)
end

main()

Views

15

Lines

102

Characters

2,960

Likes

0

Details

Language
Lua
Created
Sep 13, 2025
Updated
29d ago
Size
2.9 KB

Build your snippet library

Join thousands of developers organizing and sharing code snippets.