So i am making a small program to receive and decrypt a string, and then return the decrypted string and print it. I am quite new to C, so the whole pointer and string array thing still confuses me. My function: char **decrypt2
takes a string
(char* pw
) and a int
(encryptionInt
) to use for decryption. I manage to copy the string into another string, and change the value of it with the encryptionInt
. But when i print the final array it contains some extra characters, and when i print the returned string in another function then the values look random and wrong.
Edit: Sorry, i realise that not all the arguments to the different functions are used.
char **decrypt2(char* pw, int encryptInt) {
int pwLength = strlen(pw);
char arr1[pwLength];
char *arr2[1];
printf("pw length: %i\n", pwLength);
for (int i = 0; i < pwLength; i++)
{
arr1[i] = pw[i];
printf("standard: %c\n", arr1[i]);
}
for (int i = 0; i < pwLength; i++)
{
arr1[i] = arr1[i] - encryptInt; //minus in ascii
printf("decrypted: %c\n", arr1[i]);
}
arr2[0] = arr1;
printf("decrypted final: %s\n", arr2[0]);
char **ptr = arr2;
return ptr;
}
int login(char* userName, char* loginPw, int encryptInt) {
char **decryptedPWArray = decrypt2("test", encryptInt);
printf("received string: %s\n", decryptedPWArray[0]);
return 0;
}
int main(int argc, char const *argv[])
{
int true = login("anders", "test", 3);
}
So i expected the "decrypted final" and the "received string" output to be: qbpq, but this is what i get:
pw length: 4
standard: t
standard: e
standard: s
standard: t
decrypted: q
decrypted: b
decrypted: p
decrypted: q
decrypted final: qbpq@@
received string: F¿vìP@