Algorithm bugs that encrypt text

Asked

Viewed 90 times

1

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

  • When running the program, enter the password and later the text, error occurs Segmentation failure (recorded core image). For what?
  • The code is not working, how to 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 doesn’t have to be a great answer, small tips that guide me the solution serves, because I’m already a few weeks stuck to this problem.

  • Can it be more specific which error ? And in which line the same happens

  • 1

    What is the purpose of this line k = atoi(argv[j]); ? argv supposedly only has a string in addition to the executable name, so it quickly fails there if the Indice passes the 1. Not to mention that argv should be declared as char **argv or char *argv[]

1 answer

2


You are not going through every letter of the password, but trying to convert the entire password into 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 mistake is because, like the size of argv is 2, the third iteration of the loop attempts to read a memory position that goes beyond the limits of the array, so it gives the segmentation failure.

In the same way, the line:

int m = strlen(argv);

should be:

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

Furthermore, your code seems correct. Note however that your k is not a number of 0 to 26 (as in the original Vigenère cipher), but rather the value of the character itself. This brings the collateral effect of the cipher being case sensitive (the original does not differentiate capitalization). If you want to do it in the standard way, you will 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.

  • Helped me a lot! To close, just one more question: argv[] need to be a two-dimensional list always? In the code I wrote char argv[] because in my head it would be easier to work with a simple list of characters instead of a list of strings.

  • C is not my specialty (I only used it in college about 15 years ago), so I can’t say for sure, but I believe so. When arguments are passed to a program, they are separated by the operating system itself, and presented to C as a list of strings. The first argument is always the name of the program, the second is the arguments separated by space. So, if you declared char *argv[] and called his show as meuprograma.exe teste etc, argv[0] will be the string "meuprograma.exe", argv[1] will be the string "teste", argv[2] will be "etc" and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.