To gain experience writing C programs including input, output, program arguments, and working with binary data.
The XOR bitwise operation can be used as a two-directional encryption scheme. To encrypt a message we need a string to encrypt and another string, called the key, to use in the encryption. We then treat each of these as a sequence of bytes, and XOR each byte in the message by each byte in the key. When the key runs out, we loop back to the beginning of it.
For example, if our message is "Hello" and the key is "meh", then we would do the encryption thusly:
Here we XOR the bits of each letter's ASCII value one-by-one with the bits in each letter of the key, with the key looping at the end if needed. The bytes that are the result of this process comprise the encrypted message.
The cool thing about this encryption scheme is that we do the exact same process to get the message back! If we try this for the example above, it looks like this:
Using the same key to apply the process again gets us back our original text.
Your program should take three command line parameters:
If we are doing encryption, your program should read in a
line of input from the user using scanf
. You can assume all strings for this
program will be less than 1,000 characters in length. After reading the input,
you should apply the XOR algorithm, then write the result to the file.
If we are doing decryption, your program should begin by
reading in the input in from the file. You should then apply the XOR
algorithm, an display the resulting string to the screen with printf
.
You should not have two sections in your code that apply the XOR algorithm, but instead place it in a function so that it can be called upon to do both encryption and decryption.
Your program should print an error and quit if any of the following occur:
The file being read and written by this program has a very simple format. First, there are 4 bytes giving the size of the encrypted text in the file. Following that are that many bytes of text.
When reading, you should use fread
to first read the integer, and
then call fread
a second time to read the message.
When writing to the file, first write the size of the encrypted message with fwrite
followed by a second fwrite
to write the message itself.
Because the files are storing binary values that may or may not match to ASCII values, you won't be able to sensibly open the files with Vim or any other text editor. There contents will look like gibberish if you try.
Although you should use the same function to both encrypt and decrypt the data, I would suggest testing decryption first. That way, you can test your program with an existing input file. The file secret contains text that has been encrypted with this scheme and the key "eagle". Your program should be able to decrypt this as:
$ ./a.out dec eagle secret Coding in C is fun!
When you are confident that your decryption is working, you can test the encryption and decryption together using your program:
$ ./a.out enc pizza my-file Enter a message: this is a test message $ ./a.out dec pizza my-file this is a test message
^
operator does the XOR operation.strcmp
for comparing strings and not ==
.Programs will be graded based on the following criteria:
Criteria | Grade |
---|---|
Decrypting the example file correctly | 50 |
Encrypting new files correctly | 80 |
Program broken down into multiple functions | 90 |
Code well-written and commented | 100 |
When you are done, please submit the C code under the assignment in Canvas.
Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.