scugn1zz0
scugn1zz0

Reputation: 337

Very simple cryptography function in C

I'm trying to create a very simple cryptography function that takes a character array and returns another character array with its value increased by one.

Precise explanation: The input is a line of text inserted by the user. I expect that the array line for example "abcdef" becomes "bcdefg". What really happen is that I can't increase the value of each character, I don't know how to do it. I don't get any error from the compiler, just the result is wrong and I think that the problem is related to strcat, but I can't imagine how to solve it.

Since I'm a C student, in the answer I would like something just to fix my program, if it is possible, not a whole new one.

#include <stdio.h>
#include <string.h>

char line[20];
char duplicated[20];
char hashed[];

/********************************************************
 * hashf -- Simple hash function that takes a character *
 *          array and returns that takes a character    *
 *          array and returns another character array   *
 *          with its value increased by one.            *
 *                                                      *
 * Parameters -- string duplicated from the text        *
 *               inserted by the user                   *
 *                                                      *
 * Returns -- the hashed array (supposed to...)         *
 ********************************************************/
char hashf(char duplicated[]) {
    hashed[0] = '\0';
    for (int i = 0; duplicated[i] != '\0' ; ++i) {
        duplicated[i] += 1;
        strcat(hashed, duplicated[i]);
    }
    return (hashed);
}

int main() {
    printf("Put a text to hash: ");
    fgets(line, sizeof(line), stdin);

    strcpy(duplicated, line);        // strcpy(string1, string2) -- copy string 2 into string 1

    /* This two line are used to remove the '\n' character from the end of the array */
    duplicated[strlen(duplicated)-1] = '\0';  
    line[strlen(line)-1] = '\0';

    printf("The text %s become %s", line, hashf(duplicated));
}

Upvotes: 0

Views: 142

Answers (3)

Justin Sanders
Justin Sanders

Reputation: 343

Do you have all compiler error messages on? When I compile, I get three messages, which put me in the direction of fixing it.A big problem is the way you handle global variables. You don't need to pass them all over the place. Second, the strcpy takes two pointers, you gave it a pointer and an int, I think this caused the segmentation fault. Anyways, I fixed it up for you:

#include <stdio.h>
#include <string.h>

char line[20];
char duplicated[20];
char hashed[40];

void hashf() {
    for (int i = 0; duplicated[i] != '\0' ; ++i) {
        duplicated[i] += 1;
        hashed[i] = duplicated[i];
    }
}

int main() {
    printf("Put a text to hash: ");
    fgets(line, sizeof(line), stdin);

    strcpy(duplicated, line);      
    duplicated[strlen(duplicated)-1] = '\0';  
    line[strlen(line)-1] = '\0';

    hashf();
    printf("The text %s become %s", line, hashed);
} 

Just something to think about, what will z go to ;) ? Will it be what you want? Good luck?

Upvotes: 0

r-sniper
r-sniper

Reputation: 1493

Many mistakes.

Don't ignore compiler warnings.

First Warning

warning: array ‘hashed’ assumed to have one element char hashed[];

You haven't allocated memory so its been assumed as 1 byte;

Change it to char hashed[40];

2nd Warning

warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion] strcat(hashed, duplicated[i]); ^~~~~~~~~~ Blockquote

/usr/include/string.h:133:14: note: expected ‘const char * restrict’ but argument is of type ‘char’ extern char *strcat (char *__restrict __dest, const char *__restrict __src)

strcat has prototype char * strcat ( char * destination, const char * source );

Notice the second parameter const char * , which means you pass an address and it will concatenate string from that address till it finds \0 but you are passing a char(will not work)

3rd Warning

warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=] printf("The text %s become %s", line, hashf(duplicated));

beacuse you function return type is char it should be char *

So to conclude Change your function as follows

char* hashf(char duplicated[]) {
    hashed[0] = '\0';
    for (int i = 0; duplicated[i] != '\0' ; ++i) {
        duplicated[i] += 1;
        }
       // strcat(hashed, duplicated); as others suggested no need for this
    //}
    return (duplicated);
}

Upvotes: 2

Hash should be an one-way-function. (read: https://en.wikipedia.org/wiki/Hash_function on invertible properties)

Anyway, there's some mistakes. You want to return an array, but you use char.

You must change char hashf(char duplicated[]) to char* hashf(char duplicated[]). (may be this is useful: https://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm)

I think this is the cleanest way to implement it:

char* hashf(char duplicated[]) {
    for (int i = 0; duplicated[i] != '\0' ; ++i) {
        duplicated[i] += 1;
    }
    return duplicated;
}

Upvotes: 0

Related Questions