I used the code included below to decrypt some graphic files. I would like to modify it so it can encrypt them again. Unfortunately I'm a graphic designer and I only have very basic knowledge about programing (and not in C) so I don't understand this code on a level that would allow me to modify it by myself.
#include <stdio.h>
int main (int argc, char **argv)
{
FILE *inp, *outp;
int i;
char sig[] = "CF10", *ptr;
if (argc != 3)
{
printf ("usage: decode [input] [output]\n");
return -1;
}
inp = fopen (argv[1], "rb");
if (inp == NULL)
{
printf ("bad input file '%s'\n", argv[1]);
return -2;
}
ptr = sig;
while (*ptr)
{
i = fgetc (inp);
if (*ptr != i)
{
printf ("input file sig is not 'CF10'\n");
return -2;
}
ptr++;
}
outp = fopen (argv[2], "wb");
if (outp == NULL)
{
printf ("bad output file '%s'\n", argv[1]);
return -2;
}
do
{
i = fgetc(inp);
if (i != EOF)
fputc (i ^ 0x8d, outp);
} while (i != EOF);
fclose (inp);
fclose (outp);
printf ("all done. bye bye\n");
return 0;
}
This could, at best, be described as obscuring the content. It uses 'xor' encryption, and the one advantage of that is that is self-decrypting.
You can run the code on an unencrypted file and get an encrypted file, or on an encrypted file and get an unencrypted file.
I misread the code; sorry. Your code reads the CF10 magic number, and then writes the rest of the data after xor'ing it; the CF10 tells it that it is obscured. To obscure unobscured data, you should write the CF10 magic number to the output file, then read the input and xor it and write it. A refined version of the code would test the first four bytes of the file (for equality with CF10) to ensure that you are not re-obscuring and already obscured file.
You might need to handle an option argument that determines whether to encrypt or decrypt (obscure or unobscure). Alternatively, you can use the first four bytes of the file to tell you whether to encrypt or decrypt.
Note that the basic point of the original answer — that xor encryption and decryption are fundamentally the same operation — remains valid.