# Define the alphabet
alpha <- 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")

# Created the vigenermatrix with the use of Loops 
n <- length(alpha)
vigenere <- matrix(NA, nrow = n, ncol = n, dimnames = list(alpha, alpha))

for (a in 1:n) {
  for (b in 1:n) {
    vigenere[a, b] <- alpha[(a + b - 2) %% n + 1]
  }
}

# Print the Vigenère cipher matrix  
print(vigenere)

#QUESTION 2

keyphrase <- "UWTSDSWANSEA"
plaintext <- "CAPTURETHEFLAG"

# Initialize vectors to hold keyphrase, plaintext, and ciphertext
keyphrase_vector <- character(0)
plaintext_vector <- character(0)
ciphertext_vector <- character(0)

# Repeat keyphrase to match the length of the plaintext
keyphrase_length <- nchar(keyphrase)
plaintext_length <- nchar(plaintext)

for (i in 1:plaintext_length) {
  keyphrase_vector <- c(keyphrase_vector, substr(keyphrase, (i - 1) %% keyphrase_length + 1, (i - 1) %% keyphrase_length + 1))
}

# Convert letters to numbers (A=0, B=1, ..., Z=25)
for (i in 1:plaintext_length) {
  plaintext_vector <- c(plaintext_vector, substr(plaintext, i, i))
}

# Encrypt each letter in the plaintext
for (i in 1:plaintext_length) {
  # Convert letters to numbers (A=0, B=1, ..., Z=25)
  plaintext_num <- as.numeric(charToRaw(plaintext_vector[i])) - as.numeric(charToRaw("A"))
  key_num <- as.numeric(charToRaw(keyphrase_vector[i])) - as.numeric(charToRaw("A"))
  
  # Encrypt letter
  ciphertext_num <- (plaintext_num + key_num) %% 26
  
  # Convert back to letters
  ciphertext_vector <- c(ciphertext_vector, rawToChar(as.raw(ciphertext_num + as.numeric(charToRaw("A")))))
}

# Display the results
cat("Plaintext:", paste(plaintext_vector, collapse = ""), "\n")
cat("Keyphrase:", paste(keyphrase_vector, collapse = ""), "\n")
cat("Ciphertext:", paste(ciphertext_vector, collapse = ""), "\n")


#QUESTION 3
keyphrase <- "RPROGRAMMING"
ciphertext <- "ETMSXWISTBNHRIKZKKHMFGBAUDECZEEQPBBCZC"

# Initialize vectors to hold keyphrase, ciphertext, and plaintext
keyphrase_vector <- character(0)
ciphertext_vector <- character(0)
plaintext_vector <- character(0)

# Repeat keyphrase to match the length of the ciphertext
keyphrase_length <- nchar(keyphrase)
ciphertext_length <- nchar(ciphertext)

for (i in 1:ciphertext_length) {
  keyphrase_vector <- c(keyphrase_vector, substr(keyphrase, (i - 1) %% keyphrase_length + 1, (i - 1) %% keyphrase_length + 1))
}

# Convert letters to numbers (A=0, B=1, ..., Z=25)
for (i in 1:ciphertext_length) {
  ciphertext_vector <- c(ciphertext_vector, substr(ciphertext, i, i))
}

# Decrypt each letter in the ciphertext
for (i in 1:ciphertext_length) {
  # Convert letters to numbers (A=0, B=1, ..., Z=25)
  ciphertext_num <- as.numeric(charToRaw(ciphertext_vector[i])) - as.numeric(charToRaw("A"))
  key_num <- as.numeric(charToRaw(keyphrase_vector[i])) - as.numeric(charToRaw("A"))
  
  # Decrypt letter
  plaintext_num <- (ciphertext_num - key_num + 26) %% 26
  
  # Converting back to letters
  plaintext_vector <- c(plaintext_vector, rawToChar(as.raw(plaintext_num + as.numeric(charToRaw("A")))))
}

# Display the results
cat("Ciphertext:", paste(ciphertext_vector, collapse = ""), "\n")
cat("Keyphrase:", paste(keyphrase, collapse = ""), "\n")
cat("Plaintext:", paste(plaintext_vector, collapse = ""), "\n")

