# Kush Patel (2409544)

# Task 1:
# Create a Polybius cipher matrix
Polybius <- matrix(c(
  ' ', 'Z', 'M', 'D', ' ', 'K', 'W', 'Q', ' ', 'G',  # Row 0
  'V', ' ', 'A', ' ', 'R', ' ', 'E', ' ', 'T', 'O',  # Row 1
  ' ', 'J', 'I', 'S', 'B', 'P', 'Y', ' ', 'F', ' ',  # Row 2
  'L', ' ', ' ', 'C', ' ', 'U', 'H', 'N', ' ', 'X'   # Row 3
), nrow = 4, byrow = TRUE)  

# Assign row names (0 to 3) and column names (0 to 9) 
rownames(Polybius) <- 0:3
colnames(Polybius) <- 0:9

# Print the Polybius cipher matrix to verify
print("Polybius Matrix:")
print(Polybius)

# Task 2: Encrypt the Message "CAPTURETHEFORT"

# Step 1: Define the message to be encrypted
message <- "CAPTURETHEFORT"

# Step 2: Convert the message into individual characters
# `substring()` extracts each character from the message string as a separate element in a character vector
message_chars <- substring(message, 1:nchar(message), 1:nchar(message))

# Step 3: Initialize an empty vector to store the encrypted ciphertext
# `character(0)` creates an empty character vector to store the numeric codes
ciphertext <- character(0)

# Step 4: Loop through each character in the message to find its position in the Polybius matrix
for (char in message_chars) {
  # Find the row and column indices of the character in the Polybius matrix
  pos <- which(Polybius == char, arr.ind = TRUE)  # Returns a matrix with row and column indices
  
  if (length(pos) > 0) {  # If the character exists in the Polybius matrix
    # Extract row and column indices and adjust for 0-based indexing
    row_num <- pos[1] - 1  # Subtract 1 from the row index to make it 0-based
    col_num <- pos[2] - 1  # Subtract 1 from the column index to make it 0-based
    # Combine the row and column into a single string in "rowcolumn" format
    ciphertext <- c(ciphertext, sprintf("%d%d", row_num, col_num))
  }
}

# Print the encrypted message as a series of two-digit strings
print("Ciphertext:")
print(ciphertext)

# Task 3: Convert the Ciphertext into Groups of 5 Digits

# Step 1: Check if the ciphertext length is a multiple of 5
# Use the modulus operator (%%) to find the remainder when the length is divided by 5
remainder <- length(ciphertext) %% 5

if (remainder > 0) {
  # If the remainder is greater than 0, calculate the number of digits needed to pad the ciphertext
  padding_needed <- 5 - remainder
  # Generate random digits (0-9) to fill the padding
  # `sample()` generates the required number of random digits, allowing repeats
  padding <- sample(0:9, padding_needed, replace = TRUE)
  # Add the random digits to the ciphertext as strings
  ciphertext <- c(ciphertext, sprintf("%d", padding))
}

# Step 2: Format the ciphertext into groups of 5 digits
# Combine all the ciphertext elements into a single string
ciphertext_string <- paste(ciphertext, collapse = "")

# Split the ciphertext string into groups of 5 characters using `substring()`
# The start and end points of each group are specified using sequences
groups <- substring(ciphertext_string, seq(1, nchar(ciphertext_string), 5), seq(5, nchar(ciphertext_string) + 4, 5))

# Combine the groups of 5 into a single string separated by spaces
finaltext <- paste(groups, collapse = " ")

# Display the final formatted ciphertext
print("Final formatted ciphertext:")
print(finaltext)
