Bugs in algorithm that encrypts text

1

I am writing an algorithm that encrypts text using a password word, Vigenère cipher. Upper and lower case characters should be encrypted, special characters, and numbers should be ignored. My questions:

  • When you run the program, enter the password and then the text, the error Segmentation fault (recorded core image) occurs. Per What?
  • The code is not working, how can I improve it?

code:

#include<cc50.h> // BIBLIOTECA DO CURSO QUE ESTOU FAZENDO.
#include<string.h>
#include<stdio.h>

int
main(int argc, char argv[])
{

    if (argc != 2)
    {
        printf("Erro 1. Digite uma palavra na linha de comando.\n");
        return 1;
    }

    printf("Texto a ser criptografado:\n");
    string texto = GetString();

    int k = 0;
    int l = strlen(texto);
    int m = strlen(argv);

    for (int i = 0, j = 0; i < l; i++)
    {
        k = atoi(argv[j]);
        if (j > m) // SE O CONTADOR J FOR MAIOR QUE A QUANTIDADE DE CARACTERES DA SENHA, REDEFINE J e K PARA 0.
            {
            j = 0;
            k = 0;
            }
        else if (texto[i] >= 65 && texto[i] <= 90)
            {
            texto[i] = (((texto[i] - 65) + k) % 26) + 65;
            j++;
            }
        else if (texto[i] >= 97 && texto[i] <= 122)
            {
            texto[i] = (((texto[i] - 97) + k) % 26) + 97;
            j++;
            }
        else;
        printf("%c", texto[i]);
    }

    printf("\n");
    return 0;
}

It does not have to be a big answer, little tips that guide me the solution is good, since I'm already a few weeks stuck with this problem.

    
asked by anonymous 19.09.2017 / 01:02

1 answer

2

You are not scrolling through each letter of the password, but trying to convert the entire password to a number. The line:

    k = atoi(argv[j]);

should be:

    k = (int)argv[1][j]; // Caractere j do argumento 1
                         // Não use atoi, pois o que você quer é o valor do caractere em si

The error is because since the size of argv is 2 , the third iteration of the loop tries to read a memory location that goes beyond the bounds of the array, then it gives the segmentation fault.

Similarly, the line:

int m = strlen(argv);

should be:

int m = strlen(argv[1]); // O tamanho da primeira palavra

In fact, your code seems correct. Note however that its k is not a number of 0 a 26 (as in the original Vigenère cipher), but rather the value of the character itself. This brings the side effect of the cipher to be case sensitive (the original does not differentiate capitalization). If you want to do the standardized way, you need to normalize the value of k before using it. Example:

    k = (int)argv[1][j];
    if ( 65 <= k && k <= 90 )
        k -= 65;
    else if ( 97 <= k && k <= 122 )
        k -= 97;
    else
        k = 0; // Caractere inválido na senha, ignore

P.S. As pointed out by Isac in the comments, the way you are declaring argv is also incorrect - is to be an array of strings, not a single string.

    
19.09.2017 / 01:27