patterncModerate
XOR encryption program in C
Viewed 0 times
xorprogramencryption
Problem
I've written a program that encrypts files (or
To compile:
stdin) with a user-specified key from 1 to 255. This "encryption" is very insecure, but still a bit better than ROT13 (at least for people trying to crack it using pencils and paper). How can I improve this program?#include
#include
#include
#include
/*
* A simple program for encrypting files (or stdin) using the XOR operation.
* Such encryption is very insecure as there are only 254 valid key values,
* but is still much more secure than ROT13, where there is only 1 valid key.
*
* Using a key value of 0 or 256 is equivalent to no encryption, and is
* therefore not allowed. Other values are too large to fit into one byte
* and therefore cannot be properly XORed with a byte.
*/
static void usage(const char *const);
int main(int argc, char *argv[])
{
FILE *in = NULL;
FILE *out = NULL;
int k = 0;
int c;
while ((c = getopt(argc, argv, "i:o:k:h")) != -1) {
switch (c) {
case 'i':
in = fopen(optarg, "r");
break;
case 'o':
out = fopen(optarg, "w");
break;
case 'k':
k = atoi(optarg);
break;
case 'h':
usage(argv[0]);
return EXIT_FAILURE;
}
}
in = in ? in : stdin;
out = out ? out : stdout;
if (k 255) {
fprintf(stderr, "%s: must specify a valid key via -k\n",
argv[0]);
fprintf(stderr, "%s: valid key values are from 1 to 255\n",
argv[0]);
return EXIT_FAILURE;
}
while ((c = fgetc(in)) != EOF) {
fputc(c ^ k, out);
}
return EXIT_SUCCESS;
}
void usage(const char *const s)
{
fprintf(stderr, "usage: %s -k key [-i input] [-o output]\n", s);
}To compile:
$ clang -O2 -Weverything -Werror -o xor xor.cSolution
Use binary mode
Currently, you are opening your files like this:
On a UNIX system that would be fine, but on a Windows system, that could result in CRLF pairs being converted into a single LF when reading, and single LF characters converted to CRLF pairs when writing. This is particularly important because once you do the xor operation, your output may contain random LF characters.
You should open your files in binary mode instead:
Currently, you are opening your files like this:
in = fopen(optarg, "r");
out = fopen(optarg, "w");On a UNIX system that would be fine, but on a Windows system, that could result in CRLF pairs being converted into a single LF when reading, and single LF characters converted to CRLF pairs when writing. This is particularly important because once you do the xor operation, your output may contain random LF characters.
You should open your files in binary mode instead:
in = fopen(optarg, "rb");
out = fopen(optarg, "wb");Code Snippets
in = fopen(optarg, "r");
out = fopen(optarg, "w");in = fopen(optarg, "rb");
out = fopen(optarg, "wb");Context
StackExchange Code Review Q#113721, answer score: 11
Revisions (0)
No revisions yet.