Overview

You will be reading a binary file, decoding its contents, and printing the decoded text onto the screen.

Assignment

You will be creating a program that reads an encrypted file, decrypts it, and prints its contents to the screen.

The encrypted file will be a binary file, so you must open and read it into memory as a binary file.

The files: msg0.enc, msg0.plain, msg0.key demonstrate how this cryptography works. The dkey for msg0.enc is B and the nkey is 127.

COSC130 Cryptography Scheme

The binary files are encrypted by using two keys: a dkey (data key) and an nkey (index key). These two keys must match what was used to encrypt the files in order to decrypt the data.

The data key is exactly 8 bits and the index key is exactly 32 bits, and they will be supplied as the second command line argument and third command line argument, respectively.

The first command line argument is the name of the encrypted file.

The encryption scheme encrypts the data, byte-by-byte, then it shuffles where the bytes are located within the file.

This means to decrypt the file, you must decrypt the data using the dkey and then locate the next block to decrypt using the nkey.

Each block in the encrypted file is exactly 8 bytes. The first byte is the actual data and the last 4 bytes is the index that data belongs to.

The middle three bytes are called "padding" and are not used. You must ignore these bytes.

When you read the first block, you will get an encrypted data byte and an encrypted index (4 bytes).

To decrypt the data byte, you must XOR the encrypted data byte with the dkey.

To decrypt the index, you must XOR the encrypted index with the nkey.

Now, you should have a decrypted data byte and a decrypted index. For example, say the decrypted byte is 'B' and the decrypted index is 4. This means that all_data[4] = 'B'.

In other words, the index describes a 0-based index into a full size data array. For a properly encrypted file, all indices will be represented somewhere within the file. However, you must still error check to ensure decryption is successful. If something does not decrypt within sane bounds, you must let the user know.

You will continue to decrypt block-by-block until all blocks have been decrypted. Afterward, you will have an array of characters, which contains the data you need to print to the screen.

NOTE: Your error messages do not need to match mine, however it should properly relay all relevant information to the user.

Sample Interaction

./lab2b msg0.enc B 127
Hello

./lab2b msg0.enc C 127
Idmmn

./lab2b msg0.enc B 126
eHll
o

./lab2b msg0.enc Z 1893871254987
Error decoding chunk 0, decoded index -209322571, but max chunks is 6.

The last error message is a result of decoding the index with the nkey. In this case, it decoded the index -209322571, however all_data[-209322571] is an invalid index, so it prints an error and quits the program.

Restrictions

  • You must use C-style files (fopen, fread, fseek, etc.)
  • You must NOT use cin or cout. Instead, use scanf and printf.
  • You may not use vectors or any other container. Instead, you must use pointers and dynamic memory.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.