I have deleted duplicate questions and this is the final revision. I have a question about how to decrypt a decrypted string with a string key in C. My basic information about encryption and decryption with using int key but don't know what to do with a string key.
I am required to write the definition of the function below with these only given data and a comment acts as a hint to help write the function.
One of the bitwise operators has been used to encrypt a string one character at a time using all characters in the encryption key, from left to right. Given the encryption key and ent[] the encrypted text, the decrypt function retrieves the original text and places it in det[].
void decrypt(const char key[], const char ent[], char det[]);//fun prototype
char key[] = "Advanced C";
char ent[100] = ""├8023.)/<)423}2;}↑%>1(.4 8}↕☼};2/}├<)<}↑3>/$-)423}<39}├8>/$-)423s";
char det[100];
Which algorithm to follow to deal with a string key. In this case, I am required to use all char in key[] from left to right but don't know what algorithm to do that.
my first trial and didn't work
void decrypt(const char key[], const char ent[], char det[])
{
int i = 0;
while (ent[i] != '\0')
{
int j = 0;
while (key[j] != '\0')
{
det[i] = ent[i] ^ key[j];
j ;
i ;
if(!(*ent))
break;
}
}
}
CodePudding user response:
If XOR is the algorithm then encrypt & decrypt can be simplified to :
#include <stdio.h>
#include <string.h>
void decrypt (const char key[], const char cipher[], char text[]) {
char XOR = 0;
while (*key) XOR ^= *key ;
strcpy (text, cipher); // buffer overflow chance
while (*text) *text ^= XOR;
}
void encrypt (const char key[], const char text[], char cipher[]) {
char XOR = 0;
while (*key) XOR ^= *key ;
strcpy (cipher, text); // buffer overflow chance
while (*cipher) *cipher ^= XOR;
}
int main () {
char key[] = "Advanced C";
char ent[100] = "";//├8023.)/<)423}2;}↑%>1(.4 8}↕☼};2/}├<)<}↑3>/$-)423}<39}├8>/$-)423s";
char det[100] = "Demonstration of Exclusive OR for Data Encryption and Decryption.";
encrypt(key, det, ent);
printf ("Encrypted Text: [%s]\n", ent);
det[0] = '\0';
decrypt(key, ent, det);
printf ("Decrypted Text: [%s]\n", det);
return 0;
}
CodePudding user response:
(I answer with How do I ask and answer homework questions? in mind. Starting off with some feedback on the thoughts OP explains.)
"should I accumulate ASCII code of each character in key"
Yes, that was tried by Eric Postpischl and resulted in slightly weird but practically readable output.
(Here is my thought, which, trusting Eric, I now consider improbable:
Probably not. I recommend to instead use each of the characters separatly.
I.e.
- use the first key character on the first encrypted text character, the result going into the first decrypted text characer
- then the second key character on the second encrypted text character, into second decrypted text character
)
"then XORing"
Not necessarily. Your assignment states "One of the bitwise operators", so it is not necessarily XOR. But yes, XOR makes most sense for a reversable encryption, so definitly try that first.
"or using >> or << or what exactly"
I would not consider those operators to be among the "bitwise" ones. So no, unlikely.
"start over when getting the end of key[]"
Yes, that seems the only probable way.
"but didn't work too"
It would help if you would have elaborated on what went wrong.
But I hope that not accumulating the key solves your problem.
Please show the results you get and a MRE ( https://stackoverflow.com/help/minimal-reproducible-example) of what exactly you tried.
Seeing your function, have a look at
det[i] = ent[i] ^ key[j];
Within the inner loop, with changing j and constant i, it will write repeatedly to the same place in det
. But only the last one has an effect, only the last key character is effectively used.
You intended to accumulate but did not.
Consider using that line (or somethign very similar) outside of the inner loop, with j
being 0, and then do something different inside the loop, which lets all the key characters have an influence.
(See Erics comment, it is more detailed.)
Seeing the modified function, I doubt
j ;
i ;
Incrementing i
within the inner loop seems fishy. It easily gets passed the terminator of ent
and the size of det
.
Please use a debugger to observe your program working.
Looking at another update of your code, I wonder what you are trying to program there. I understand that what you want to do is "accumulate all characters of the key to one key, apply that one key to all input characters". There is a way to do that in nested loops, but I recommend to do the accumulating in one non-nested loop and in a second non-nested loop apply the accumulated key to each input character. I recommd to throw your current version of the inner loop away, indeed drop the whole concept of an innner loop an try again. Reading Erics comment it seems to be more than sufficiently detailed.