I am working on an encryption/decryption code for C. Whilst I have the code encrypting and decrypting well enough there are still some errors. I want the users input to be in between '!' (ASCII value 32) and '~' (ASCII value 126), However, when the user puts in a character such as '~' with a fairly highly offset/key-value the output is in the extended ASCII characters range. Is there any way that I can force the output to not be in the extended character range and still decrypt/encrypt properly?
I have messed around with changing the values for "cipher" but it hasn't helped too much.
Below is my code
void Encryption(char* OriginalText, int offset) {
int i = 0;
int cipher;
char encrypt;
printf("Encrypted string: \n");
while (OriginalText[i] != '\0') {
if (OriginalText[i] == ' ') {
encrypt = ' ';
printf("%c", encrypt);
i = i + 1;
}
cipher = ((int)OriginalText[i] + (offset % 26) % -26);
encrypt = (char)(cipher);
printf("%c", encrypt);
i = i + 1;
}
printf("\n");
}
elseto yourif?) You could also rotate over a wider range; shifting from'!'to'~'is what rot47 does, for example. In that case, some letters will turn into symbols and vice versa. If the range is[min, min + n)and if0 ≤ offset < n, thennew = min + (old - min + offset) % n. - M Oehmcipherdoes not match Caesar cipher - David C. Rankin"hello ". - Paul Hankin