#--Name: Sai Sahith Akula-----
#--Student number: 2312324-----
#--Name of the module: Advanced Statistical Programming-----
#--Assignment Title: Data Definition-----


# Define the alphabet
alphabet <- c('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')

# Initialize the Vigenère matrix with 26 rows and columns
vigenere <- matrix(ncol = 26, nrow = 26, data=rep(alphabet, 26))

# Fill the matrix with shifted alphabets
for (i in 1:26) {
  # Calculate the shift for each row
  shifted_index <- (0:25) + (i - 1)
  # Ensure the index wraps around using the modulo operator
  shifted_index <- (shifted_index) %% 26 + 1
  vigenere[i, ] <- alphabet[shifted_index]
}

# Set the column and row names to the alphabet for easy reference
rownames(vigenere) <- alphabet
colnames(vigenere) <- alphabet

# Print the matrix
print(vigenere)

#Question 2
# The given keyphrase and message
keyphrase <- "UWTSDSWANSEA"
plaintext <- "CAPTURETHEFLAG"

# Extend the keyphrase to match the length of the plaintext
extended_keyphrase <- rep(strsplit(keyphrase, "")[[1]], length.out = nchar(plaintext))

# Convert plaintext to uppercase (if it's not already) to match the cipher table
plaintext <- toupper(plaintext)

# Create a vector to hold the ciphertext
ciphertext <- character(nchar(plaintext))

# Encrypt the message
for (i in 1:nchar(plaintext)) {
  # Find the row of the vigenere matrix that corresponds to the keyphrase letter
  row_index <- match(extended_keyphrase[i], alphabet)
  # Find the column of the vigenere matrix that corresponds to the plaintext letter
  column_index <- match(substr(plaintext, i, i), alphabet)
  # Look up the letter in the Vigenere matrix and add it to the ciphertext
  ciphertext[i] <- vigenere[row_index, column_index]
}

# Convert the ciphertext vector to a single string
ciphertext_string <- paste(ciphertext, collapse = "")

# Display the plaintext and the ciphertext
cat("Plaintext: ", plaintext, "\n")
cat("Ciphertext: ", ciphertext_string, "\n")



#Question 3
# Given keyphrase and ciphertext
keyphrase <- "RPROGRAMMING"
ciphertext <- "ETMSXWISTBNHRIKZKKHMFGBAUDECZEEQPBBCZC"

# Extend the keyphrase to match the length of the ciphertext
extended_keyphrase <- rep(strsplit(keyphrase, "")[[1]], length.out = nchar(ciphertext))

# Create a vector to hold the plaintext
plaintext <- character(nchar(ciphertext))

# Decrypt the message
for (i in 1:nchar(ciphertext)) {
  # Find the row of the vigenere matrix that corresponds to the keyphrase letter
  row_index <- match(extended_keyphrase[i], alphabet)
  # Get the whole row that corresponds to the current letter in the keyphrase
  cipher_row <- vigenere[row_index, ]
  # Find the position of the ciphertext letter in this row
  column_index <- match(substr(ciphertext, i, i), cipher_row)
  # The position index gives us the plaintext letter
  plaintext[i] <- alphabet[column_index]
}

# Convert the plaintext vector to a single string
plaintext_string <- paste(plaintext, collapse = "")

# Display the decrypted plaintext
cat("Decrypted Plaintext: ", plaintext_string, "\n")
