# Function to create a Vigenère matrix
create_vigenere_matrix <- function() {
  letters <- 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')
  # Matrix size is 26x26, representing 26 letters
  matrix_size <- 26  
  
  # Creating a 26x26 matrix and assigning row and column names as letters of the alphabet
  vigenere_matrix <- matrix(NA, nrow = matrix_size, ncol = matrix_size,
                            dimnames = list(letters, letters))
  
  # Filling each element of the Vigenère matrix using two nested loops
  for (i in 1:matrix_size) {
    for (j in 1:matrix_size) {
      # Filling each position in the matrix according to the Vigenère cipher algorithm rules
      vigenere_matrix[i, j] <- letters[(i + j - 2) %% matrix_size + 1]
    }
  }
  
  # Returning the filled Vigenère matrix
  return(vigenere_matrix)  
}

# Calling the function to create a Vigenère matrix
vigenere_matrix <- create_vigenere_matrix()
# Printing the Vigenère matrix
print(vigenere_matrix)  





# Prepare the letters
letters <- 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')

# Function to perform Vigenère encryption
vigenere_encrypt <- function(plaintext, keyphrase) {
  # Length of the keyphrase
  key_length <- nchar(keyphrase) 
  # Length of the plaintext
  plain_length <- nchar(plaintext)  
  
  # Convert plaintext and keyphrase to uppercase
  plaintext <- toupper(plaintext)
  keyphrase <- toupper(keyphrase)
  
  # Initialize ciphertext vector
  ciphertext <- character(plain_length)
  
  # Loop through each character in the plaintext for encryption
  for (i in 1:plain_length) {
    # Get the i-th character of the plaintext
    plain_char <- substr(plaintext, i, i)
    # Get corresponding key character
    key_char <- substr(keyphrase, ((i - 1) %% key_length) + 1, ((i - 1) %% key_length) + 1)  
    
    # Check if the character is in the letters
    if (plain_char %in% letters) { 
      # Find the index of the plaintext character in the letters
      plain_index <- match(plain_char, letters) 
      # Find the index of the key character in the letters
      key_index <- match(key_char, letters)  
      
      # Calculate the encrypted index using Vigenère cipher
      encrypted_index <- ((plain_index - 1) + (key_index - 1)) %% 26 + 1
      # Assign the encrypted character to the ciphertext
      ciphertext[i] <- letters[encrypted_index]  
    } else {
      # If the character is not in the letters, directly add it to the ciphertext
      ciphertext[i] <- plain_char
    }
  }
  
  # Return the concatenated ciphertext
  return(paste(ciphertext, collapse = "")) 
}

# Define the keyphrase and plaintext
keyphrase <- "UWTSDSWANSEA"
plaintext <- "CAPTURETHEFLAG"

# Encrypt the plaintext using the Vigenère encryption function
ciphertext <- vigenere_encrypt(plaintext, keyphrase)

# Display the plaintext and ciphertext
cat("Plaintext:", plaintext, "\n")
cat("Ciphertext:", ciphertext, "\n")




# Prepare the letters
letters <- 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')

# Function to perform Vigenère decryption
vigenere_decrypt <- function(ciphertext, keyphrase) {
  # Length of the keyphrase
  key_length <- nchar(keyphrase) 
  # Length of the ciphertext
  cipher_length <- nchar(ciphertext)  
  
  # Convert ciphertext and keyphrase to uppercase
  ciphertext <- toupper(ciphertext)
  keyphrase <- toupper(keyphrase)
  
  # Initialize plaintext vector
  plaintext <- character(cipher_length)
  
  # Loop through each character in the ciphertext for decryption
  for (i in 1:cipher_length) {
    # Get the i-th character of the ciphertext
    cipher_char <- substr(ciphertext, i, i) 
    # Get corresponding key character
    key_char <- substr(keyphrase, ((i - 1) %% key_length) + 1, ((i - 1) %% key_length) + 1)  
    
    # Check if the character is in the letters
    if (cipher_char %in% letters) {  
      # Find the index of the cipher character in the letters
      cipher_index <- match(cipher_char, letters)  
      # Find the index of the key character in the letters
      key_index <- match(key_char, letters)  
      
      # Calculate the decrypted index using Vigenère cipher
      decrypted_index <- ((cipher_index - 1) - (key_index - 1) + 26) %% 26 + 1
      # Assign the decrypted character to the plaintext
      plaintext[i] <- alphabet[decrypted_index] 
    } else {
      # If the character is not in the alphabet, directly add it to the plaintext
      plaintext[i] <- cipher_char
    }
  }
  
  # Return the concatenated plaintext
  return(paste(plaintext, collapse = ""))
}

# Define the keyphrase and ciphertext
keyphrase <- "RPROGRAMMING"
ciphertext <- "ETMSXWISTBNHRIKZKKHMFGBAUDECZEEQPBBCZC"

# Decrypt the ciphertext using the Vigenère decryption function
decrypted_text <- vigenere_decrypt(ciphertext, keyphrase)

# Display the ciphertext and the resulting decrypted text
cat("Ciphertext:", ciphertext, "\n")
cat("Decrypted text:", decrypted_text, "\n")

