AJDERNIZ

Ciberseguridad
Alumno: Axel López
Grupo: IC
Profesor: Johel Solano Quirós

Práctica: deszifrado en Vigenere

Trampa

Lo cierto es que tanta bailadera de columnas y filas me enreda bastante. Nuevamente, me dio pereza. Lo bueno es que el cifrado de Vigenere consiste en sumar el índice de cada letra del mensaje al índice de la letra correspondiente en la clave. El descifrado es exactamente lo contrario: la resta.

Agreggué un poco de código al programa que escribí para la práctica de cifrado anterior. El resultado es el siguiente:

#include 
#include  /* calloc */
#include  /* strlen, strcmp */

int main(int argc, char **argv){

  int alphabet_size;

  int index;
  int key_index;

  size_t message_length;
  size_t key_length;

  char *message = NULL;
  char *key = NULL;
  char *option = NULL;
  char *encrypted_message = NULL;

  if (argc != 4) {
    puts("Invalid amount of arguments");
    puts("Proper syntax: ./vigenere \"MESSAGE\" \"KEY\" [-encrypt | -decrypt]");
    return EXIT_FAILURE;
  }

  alphabet_size = ('Z' - 'A') + 1;

  message = argv[1];
  message_length = strlen(message);
  for (index = 0; index < message_length; index++) {
    if (message[index] < 'A' || 'Z' < message[index]) {
      if (message[index] < 'a' || 'z' < message[index]) {
        if (message[index] != ' ') {
          puts("Invalid message");
          return EXIT_FAILURE;
        }
      }
      message[index] = toupper(message[index]);
    }
  }

  key = argv[2];
  key_length = strlen(key);
  for (index = 0; index < key_length; index++) {
    if (key[index] < 'A' || 'Z' < key[index]) {
      if (key[index] < 'a' || 'z' < key[index]) {
        puts("Invalid key");
        return EXIT_FAILURE;
      }
      key[index] = toupper(key[index]);
    }
  }

  option = argv[3];

  encrypted_message = calloc(sizeof(char), message_length + 1);
  if (NULL == encrypted_message) {
    puts("Could not allocate encrypted_message");
    return EXIT_FAILURE;
  }

  if (0 == strcmp(option, "-encrypt")) {

    puts("M K E");
    puts("=====");
    for(index = 0, key_index = 0; index < message_length;
        index++, key_index = (key_index + 1) % key_length) {
      if (' ' == message[index]) {
        encrypted_message[index] = message[index];
        puts("");
        index++;
      }
      encrypted_message[index] =
        (message[index] + key[key_index]) % alphabet_size + 'A';
      printf("%c %c %c\n", message[index], key[key_index],
          encrypted_message[index]);
    }
    encrypted_message[message_length] = '\0';

    printf("Message: %s\n", message);
    printf("Key: %s\n", key);
    printf("Encrypted: %s\n", encrypted_message);

  } else if (0 == strcmp(option, "-decrypt")) {

      puts("M K D");
      puts("=====");
      for(index = 0, key_index = 0; index < message_length;
          index++, key_index = (key_index + 1) % key_length) {
        if (' ' == message[index]) {
          encrypted_message[index] = message[index];
          puts("");
          index++;
        }
        encrypted_message[index] =
          (message[index] - key[key_index] + alphabet_size) % alphabet_size + 'A';
        printf("%c %c %c\n", message[index], key[key_index],
            encrypted_message[index]);
      }
      encrypted_message[message_length] = '\0';

      printf("Message: %s\n", message);
      printf("Key: %s\n", key);
      printf("Decrypted: %s\n", encrypted_message);

  } else {
    puts("Invalid option parameter passed");
    puts("The only options are: '-encrypt' and '-decrypt'");
  }


  free(encrypted_message);
  return EXIT_SUCCESS;
}

  

Lista de mensajes para descifrar

La clave para todos los mensajes es: TECNOLOGIA.

    • Mensaje: JWRHTST AD JHKRFBMRPOW
    • Cifrado: QSPUFHF UV JOGPSNBDJGW
    • Mensaje: UHWIPXXGJ FF XRTIIPYN
    • Cifrado: BDUVBMJAB FM TPGUXBSF
    • Mensaje: YSFFXBIH CVM IDHDE
    • Cifrado: FODSJQUB UVT EBUPT
    • Mensaje: WTQTXSJYJ MU WHR
    • Cifrado: DPOGJHVSB MB SFE
    • Mensaje: USDZXLP SXT LNHGVAh
    • Cifrado: BOBMJAB MPT SJFTHPT