# -------------------------------------
# Task 1: Encryption using Polybius Square
# -------------------------------------
# References: The Art of R Programming, Chapter 6 (Matrices), pages 78-80
# -------------------------------------

# -------------------------------------
# Step 1: Define the Polybius matrix
# -------------------------------------

# Creating a 4x10 matrix to use as the Polybius square for encryption.
# Reference: Chapter 6: Matrices, pages 78-80, where matrix creation and indexing are described.

polybius <- matrix(c(" ", "Z", "M", "D", " ", "K", "W", "Q", " ", "G", 
                     "V", " ", "A", " ", "R", " ", "E", " ", "T", "O", 
                     " ", "J", "I", "S", "B", "P", "Y", " ", "F", " ", 
                     "L", " ", " ", "C", " ", "U", "H", "N", " ", "X"), 
                      nrow = 4, byrow = TRUE)

# Assign numerical labels to rows and columns (0-3 for rows, 0-9 for columns)

rownames(polybius) <- 0:3  # Assign row names (reference: Chapter 7, Indexing, pages 100-102)
colnames(polybius) <- 0:9  # Assign column names (reference: Chapter 7, Indexing, pages 100-102)

# Display the Polybius matrix for reference

polybius


# -------------------------------------
# Step 2: The message to encrypt
# -------------------------------------


# Define the message to encrypt, manually split into individual characters.
# The message here represents the plaintext we want to encrypt using the Polybius square.

message <- "CAPTURETHEFORT" # Define the message to be encrypted

# Convert the message into a vector of individual characters using substring()

message_vector <- sapply(1:nchar(message), function(i) substring(message, i, i))

# -------------------------------------
# Step 3: Prepare an empty vector to hold the ciphertext
# -------------------------------------

ciphertext <- c()  # Initialize an empty vector to store the ciphertext

# -------------------------------------
# Step 4: Loop through each character in the message
# -------------------------------------

# Loop through each character in the message to find its corresponding row and column in the Polybius matrix.
# Reference: Chapter 10: Character Manipulation, pages 140-143, where iteration and indexing are discussed.

for (char in message_vector) 
{
  # Find the row and column where the character is located
  for (i in 1:4) 
  {
    for (j in 1:10) 
    {
      if (polybius[i, j] == char) 
      {
        # Combine row and column into the cipher code
        code <- paste0(i - 1, j - 1)  # Subtract 1 to convert to 0-based indexing
        # Add the code to the ciphertext
        ciphertext <- c(ciphertext, code)
      }
    }
  }
}

# Display the generated ciphertext as a vector of row-column pairs.
ciphertext

# -------------------------------------
# Step 5: Format the ciphertext for display
# -------------------------------------

# Assume ciphertext is already generated from previous steps

ciphertext <- c("33", "12", "25", "18", "35", "14", "16", 
                "18", "36", "16", "28", "19", "14", "18")  

# Step 1: Combine all elements of ciphertext into a single string
# Reference: Chapter 10, Character Manipulation, pages 140-143, where character concatenation is discussed.

ciphertext_string <- paste(ciphertext, collapse = "")

# Step 2: Split the string into groups of 5 digits

finaltext <- c()

for (i in seq(1, nchar(ciphertext_string), by = 5)) 
{
  # Extract a substring of 5 characters starting from position i
  group <- substr(ciphertext_string, i, i + 4)
  if(nchar(group)<5)
  {
    group <-paste0(group,paste0(sample(0:9,5-nchar(group),replace=TRUE),collapse=""))
    
  }
  # Add this group to the finaltext vector
  finaltext <- c(finaltext, group)
}

# Step 3: Print the finaltext vector
# Display the final formatted ciphertext in groups of five digits.
finaltext

# -------------------------------------
# References
# -------------------------------------
# 1. Matrices: Chapter 6, "The Art of R Programming", pages 78-80
# 2. Indexing: Chapter 7, "The Art of R Programming", pages 100-102
# 3. Character Manipulation: Chapter 10, "The Art of R Programming", pages 140-143
