Swift practice: Ceasar’s Cipher

Caesar Cipher is a type of encryption algorithm that works by shifting each letter in the plaintext by a certain number of positions down the alphabet. This shift value is known as the “key”. For example, with a key of 3, the letter A would be replaced by D, B would become E, C would become F and so on.

The Caesar cipher is named after Julius Caesar, who is said to have used this cipher to communicate with his officials. It is very easy to implement and understand, but it is not very secure since there are only 25 possible keys to try. In fact, anyone can crack a Caesar Cipher with just a bit of trial and error.

Ceasar’s Cipher in Swift

In the following Swift code, I will show you how to implement the Caesar Cipher algorithm for the encryption of a String of text. If you want to check the code, use your Playground in Xcode.

let alphabet: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

var secretMessage = "codewithpassionbyclaude".lowercased()
var message = Array(secretMessage)

for i in 0 ..< message.count {
  for j in 0 ..< alphabet.count {
    if message[i] == alphabet[j] {
      message[i] = alphabet[(j+19) % 26]
      break
    }
  }
}

let shortMessage = String(message)

print("The secret message was '\(secretMessage)'")
print("The coded result is '\(shortMessage)'")

// Output: The secret message was 'codewithpassionbyclaude'
// Output: The coded result is 'vhwxpbmaitllbhgurvetnwx'

Quick notes

The data

let alphabet: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
  • This line defines an array of characters called “alphabet”, which contains all the letters of the alphabet in lowercase.
var secretMessage = "codewithpassionbyclaude".lowercased()
  • This line defines a string called “secretMessage” and sets it equal to the string “codewithpassionbyclaude”, converted to lowercase using the “lowercased()” method.
var message = Array(secretMessage)
  • This line defines an array of characters called “message” and sets it equal to the characters of “secretMessage” as an array using the “Array()” initializer.

The loop

for i in 0 ..< message.count {
  for j in 0 ..< alphabet.count {
    if message[i] == alphabet[j] {
      message[i] = alphabet[(j+19) % 26]
      break
    }
  }
}
  • This block of code implements the Caesar Cipher encryption algorithm.
  • It iterates over each character in the “message” array and compares it to each character in the “alphabet” array.
  • If the character in “message” matches the character in “alphabet”, it is replaced with the character 19 positions ahead in “alphabet” using the formula “(j+19) % 26”.
  • The “break” statement is used to exit the inner loop once a match is found and the character has been replaced.

The outcome

let shortMessage = String(message)
  • This line defines a new string called “shortMessage” and sets it equal to the characters in the “message” array concatenated together using the “String()” initializer.
print("The secret message was '\(secretMessage)'")
print("The coded result is '\(shortMessage)'")
  • These lines print the original “secretMessage” and the resulting “shortMessage” to the console.

Leave a Reply

Your email address will not be published. Required fields are marked *