#Sneha babariya (2409818)


# Task 1.1: Define the Polybius matrix
# The Polybius cipher matrix maps letters to unique numeric codes based on their position.
# Each letter is identified by its row and column indices.
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)  # Create a 4x10 matrix, filling row by row.

# Add row and column labels to make it easier to interpret the matrix and access elements.
rownames(polybius) <- 0:3  # Label rows as 0 to 3 (zero-based indexing).
colnames(polybius) <- 0:9  # Label columns as 0 to 9.

# Display the Polybius matrix to verify its structure and ensure it's correctly defined.
print("Polybius Matrix:")
print(polybius)

#________________________________________________________________________________________________

# Task 1.2: Define a function to encrypt a message using the Polybius cipher
# This function takes a plaintext message and converts it into a numeric ciphertext
# based on the Polybius matrix.
message <- "CAPTURETHEFORT"

# Split the message into individual characters to process them one by one.
chars <- substring(message, 1:nchar(message), 1:nchar(message))

# Initialize an empty character vector to store the ciphertext.
ciphertext <- character(0)

# Loop through each character in the message.
for (char in chars) {
  # Locate the character in the Polybius matrix.
  # 'which()' with 'arr.ind = TRUE' returns the row and column indices.
  pos <- which(polybius == char, arr.ind = TRUE)  # Returns indices of the match
  
  # Check if the character exists in the Polybius matrix.
  if (length(pos) > 0) {  # Check if the character exists in the matrix
    row_num <- pos[1] - 1  # Convert to zero-based row index
    col_num <- pos[2] - 1  # Convert to zero-based column index
    
    # Combine the row and column indices into a single string (e.g., "12")
    ciphertext <- c(ciphertext, paste0(row_num, col_num))  # Append to ciphertext
  }
}

#________________________________________________________________________________________________


# Task 1.3: Define a function to format the ciphertext into groups of 5 digits
# This function ensures the ciphertext is readable by splitting it into blocks of 5 digits.

# Combine all parts of the ciphertext into a single string.
combined <- paste(ciphertext, collapse = "")

# Split the combined string into segments of 5 characters.
# Initialize an empty vector to store formatted segments
formatted_text <- c()

# Loop through the ciphertext in chunks of 5 characters
for (i in seq(1, nchar(combined), by = 5)) {
  # Extract a 5-character segment
  segment <- substr(combined, i, i + 4)
  
  # If the segment has fewer than 5 characters, pad it with random digits
  if (nchar(segment) < 5) {
    # Generate random digits to fill up to 5 characters
    segment <- paste0(segment, paste0(sample(0:9, 5 - nchar(segment), replace = TRUE), collapse = ""))
  }
  
  # Add the segment to the formatted text
  formatted_text <- c(formatted_text, segment)
}
# Combine all segments into a single string separated by spaces.
final_text <- paste(formatted_text, collapse = " ")



# Display the original plaintext message for reference.
print("Original message:")
print(message)  # Print the original plaintext message

# Display the raw ciphertext as a character vector (before formatting into groups).
print("Ciphertext as vector:")
print(ciphertext)  # Print the raw ciphertext as a vector

# Format the ciphertext into groups of 5 digits using the formatting function.
# The 'format_ciphertext' function ensures readability by adding padding and grouping digits.
print("Final formatted text:")
print(final_text)  # Print the formatted ciphertext
