1

I am trying to create an encryptor in C with the following algorithm I thought of. But, for some reasons it is not working.

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

void 
encrypt(
    char word[],
    char password[]
)
{
    char res[strlen(word)];
    int temp;
    for (int i = 0; i < strlen(word); i++){
        temp = (int) word[i];
        temp = temp + (int) password[i % strlen(password)];
        temp = (temp - 30)/2;
        res[i] = (char) temp;
    }
    printf("%s\n", res);
}

void decrypt(
    char word[],
    char password[]
)
{
    char res[strlen(word)];
    int temp;
    for (int i = 0; i < strlen(word); i++){
        temp = (int) word[i];
        temp = temp * 2;
        temp = temp + 30;
        temp = temp - (int) password[i % strlen(password)];
        res[i] = (char) temp;
    }
    printf("%s\n", res);
}

int main(void) {
    encrypt("Hello World", "main");
    decrypt("KT[^_1Q_`WW", "main");
  return 0;
}

I got the following output

Output : 
KT[^_1Q_`WW
GekloWnqkc

I noticed that some of the text are matching while some got + 1 somehow but i am unable to figure out how. While The decrypted text should be Hello World. I am new in programming and started with C. How can I fix this ?

yawad
  • 123
  • 1
  • 6
  • 2
    You haven't allocated room for the null terminator in the local arrays so the program will crash if you treat them like strings. You need to do `strlen(...) + 1`. – Lundin Jun 21 '21 at 13:05
  • @Lundin The problem here is not only that. – MikeCAT Jun 21 '21 at 13:08
  • I close this as a duplicate to the "how to use strings" FAQ. If you still have bugs after fixing those basic problems, then please post a new question with the corrected code. – Lundin Jun 21 '21 at 13:08
  • Thank you for your help in that. But that did not quite solve my problem. Do you have any more idea why that is happening ? – yawad Jun 21 '21 at 13:10
  • 1
    @MikeCAT Probably not but there's no point in posting yet another "here's how you do utterly fundamental string handling in C" answer. While the answer is essentially "read the string chapter of any beginner-level book". So generally the OP has to fix those bugs, then get back with the fixed code as a separate question. – Lundin Jun 21 '21 at 13:11
  • Instead of simply posting the code of your algorithm's implementation, you should also describe your algorithm. That way, people can advise you if they determine your code doesn't actually implement the algorithm correctly, or if the algorithm itself will not achieve what you want. – jarmod Jun 21 '21 at 13:14
  • `GekloWnqkc` is effectively `Geklo<31>Wnqkc` with the `<31>` ASCII unprintable character replacing the space. – pmg Jun 21 '21 at 13:24

2 Answers2

2

Your encrypt/decrypt error (for string error read comments) is that division by 2 is not reversible, ie 16/2 == 17/2 == 8.

When you take 8 and want to decrypt it... you have no way to know if it was originally a 16 or a 17.

pmg
  • 106,608
  • 13
  • 126
  • 198
-1

When you encrypt, you need to use the multiplication, because, anything times two, will result in an even number. So, the division by two, will result in the same number ...

This, might help you Obs: This will not work with non ascii chars...

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


void decrypt (
    char word[],
    char password[]
) {
    int temp;
    int size = strlen(word);
    int pass_size = strlen(password);
    for (int i = 0; i < size; i++){
        temp = (int) word[i];
        temp = temp / 2 - 30 ;
        temp = temp + (int) password[i % pass_size];
        word[i] = (char) temp;
    }
}

void encrypt (
    char word[],
    char password[]
) {
    int temp;
    int size = strlen(word);
    int pass_size = strlen(password);
    for (int i = 0; i < size; i++){
        temp = (int) word[i];
        temp = temp - (int) password[i % pass_size];
        temp = (temp + 30) * 2;
        word[i] = (char) temp;
    }
}

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

    char *dup = strdup(args[1]);
    
    encrypt(dup, "sgtcortez");

    printf("Encrypted: %s\n", dup);

    decrypt(dup, "sgtcortez");

    printf("Decrypted: %s\n", dup);
    
    return 0;
}
sgtcortez
  • 407
  • 4
  • 15