C代写:COMP2401B AES

实现一个简易版的AES加密算法。

AES

Goal

For this assignment, you will write a program in C, in the Ubuntu Linux environment, to provide the end user with the ability to encrypt and decrypt messages, using a secret key. In order to prevent your enemies from easily cracking your encrypted messages, the algorithm that you implement will use an incrementing counter to encrypt each character in a slightly dierent way, similar to (but much simpler than) the Counter (CTR) mode of the Advanced Encryption Standard (AES) symmetric encryption algorithm. Your program will output the result (either the ciphertext or the plaintext) to the screen.

Learning Outcomes

With this assignment, you will:

  • get familiar with the Linux programming environment
  • write a small program in C that is modular, and correctly designed and documented
  • use bit masks and bitwise operators to manipulate values at the bit level

Instructions

Your program will prompt the user to indicate whether they wish to encrypt a readable message (the plaintext), or decrypt an already encrypted sequence of numbers (the ciphertext) back to its readable form. You will implement the encryption and decryption algorithm described below.

Your program must follow the programming conventions that we saw in the course lectures, including but not restricted to the correct separation of code into modular functions, basic error checking, and the documentation of each function. A more comprehensive list of constraints can be found in the Constraints section below.

Understand the encryption/decryption algorithm

The encryption and decryption algorithm that you implement will process each source byte, one at a time. Because the exact same algorithm will be used for both encryption and decryption, we use the term source byte to indicate each byte of input to the algorithm. For encryption, each source byte will be plaintext, and for decryption, each source byte will be ciphertext.

The algorithm will process each source byte with a key and a counter that are initialized at the beginning of the program. It’s important that the initial key and counter values be identical for both the encryption and the decryption processes. If we encrypt a message with specific key and counter values, we must use the same key and counter values for decryption. If we use dierent key and counter values for decryption, we will get garbage instead of plaintext when we decrypt the ciphertext.
The initial key value will be 0b10110010, and the initial counter value will be 0b00101000. Both values will be updated as the algorithm processes each byte.

The Algorithm:
For every source byte (plaintext or ciphertext):

  • (1) process the counter value, using the key value, as follows:
    • (a) make a copy of the counter value into a temp counter
    • (b) for every bit position, starting at bit position 7:
    • (i) compute two bit positions (position 1 and position 2) that you will use to perform an xor between two of the temp counter bits: position 1 is set to the current bit position, and position
      2 is computed as follows:
      • (1) if the key bit at the current bit position is 1, then position 2 is set to one bit position to the left of the current bit position, assuming we circle back to the least significant bits (for example, we consider bit 0 to be to the left of bit 7)
      • (2) if the key bit at the current bit position is 0, then position 2 is set to two bit positions to the left of the current bit position, assuming we circle back
    • (ii) xor the two temp counter bits found at positions 1 and 2
    • (iii) set the temp counter bit at the current bit position to the result of the xor operation
    • (iv) return the temp counter value as the updated counter value
  • (2) increment the updated counter value by 1
  • (3) increment the key value by 1
  • (4) process the source byte, using the counter value, as follows:
    • (a) make a copy of the source byte into a temp value
    • (b) compute the counter value modulo 9
    • (c) if the result is less than 3:
      • (i) loop over the bits, starting at bit position 0
      • (ii) at every other bit position, compute the xor of the source bit and the counter bit, and set the temp value bit to the result of the xor operation
    • (d) if the result is between 3 and 5 inclusively:
      • (i) loop over the bits, starting at bit position 1
      • (ii) at every other bit position, compute the xor of the source bit and the counter bit, and set the temp value bit to the result of the xor operation
    • (e) if the result is greater than 5:
      • (i) loop over the bits, starting at bit position 0
      • (ii) at every bit position, compute the xor of the source bit and the counter bit, and set the temp value bit to the result of the xor operation
    • (f) return the resulting temp value as the result of the processing; if the source byte was plaintext, then this temp value will be the corresponding ciphertext; if it was ciphertext, the temp value will be the plaintext
  • (5) the result of processing the source byte is the output (ciphertext or plaintext) for that source byte

Design your program

You will begin by designing the modular functions that you will use for implementing the algorithm described above. Your functions must be single-purpose and reusable, and they must have a clear interface with parameters that are identified as either input, output, or input-output parameters.
In addition to the getBit(), setBit(), and clearBit() functions that we covered in class, you must have a minimum of three (3) additional functions, excluding main().

Write the code

  1. You will begin with the a1-posted.c file that is posted in cuLearn. You must rename this file to an appropriate name for Assignment #1.
  2. You will implement the getBit(), setBit(), and clearBit() functions, exactly as we saw in class.
    Your program must use these functions to access and make all changes to the bits inside each value.
  3. You will implement the functions that you designed in the Design step above.
  4. You will implement the main() function to first prompt the user for whether they want to encrypt a string, or decrypt a sequence of numbers.
  5. If the user chooses to encrypt a string, your program will do the following:
    • (a) read a plaintext string from the command line; because this string will contain multiple spacedelimited words, you cannot use the scanf() library function for this; use fgets() instead, as shown in the provided code
    • (b) encrypt the plaintext entered by the user, as described in the algorithm
    • (c) print out to the screen the space-separated numeric sequence of ciphertext bytes
  6. If the user chooses to decrypt an encrypted sequence, your program will do the following:
    • (a) read a sequence of numbers from the command line, up to a sentinel value of -1
    • (b) decrypt the sequence entered by the user, as described in the algorithm
    • (c) print out to the screen the resulting plaintext as characters
      NOTE: The user should be able to decrypt any ciphertext that was produced by the algorithm, and produce the original string as plaintext.

Packaging

Every assignment in this course must to follow the conventional packaging rules for Unix-based systems:

  1. Your code must be archived into a tar or zip file. No other formats will be accepted.
  2. You must provide a README file that contains a preamble (program author, purpose, list of source, header, and data files if applicable), as well as compiling and launching instructions.
  3. Do not submit any additional files, including object files, executables, or any supplementary files or directories (macOS users must remove their additional hidden directories).

Test the program

You must test your program thoroughly. If your algorithm is correctly implemented, you will be able to decrypt the following sequence to its correct plaintext, using the initial key and counter values described in the algorithm (bragging rights go to the first student who emails me the correct decrypted message!):

71 73 227 33 2 32 150 11 175 158 79 77 162 101 197 247 116 219 37 18 76 0 254 120 210
32 146 13 58 32 73 226 195 226 102 109 115 129 170 192 19 50 30 162 124 46 33 73 76
187 245 46 62 122 138 36 80 109 176 91 113 224 109 222 219 11 175 49 97 43 61 4 53 113
99 69 77 231 43 123 121 125 143 79 224 40 105 79 84 117 248 55 197 62 42 159 225 204
14 162 213 24 164 -1

NOTE: You must try dierent initial values for the key and the counter. As long as you use the same values for both encryption and decryption, the algorithm should work.

Constraints

Your program must comply with all the rules of correct software engineering that we have learned during the lectures, including but not restricted to:

  1. The code must be written using the default C standard that is supported by the course VM, and it must compile and execute in that environment. It must not require the installation of libraries or packages or any software not already provided in the VM provided.
  2. Your program must be correctly designed, and it must be separated into modular, reusable functions.
  3. Your program must reuse the functions that you implemented, everywhere possible.
  4. Your program must perform all basic error checking.
  5. Do not use any global variables.
  6. All functions must be documented, as indicated in the course material, section 1.2.

Submission

  1. You will submit in cuLearn, before the due date and time, the following:
    • One tar or zip file that includes: (a) all source and header files (b) a README file that includes: (i) a preamble (program author, purpose, list of source and header files) (ii) compilation and launching instructions
      NOTE: Do not include object files, executables, hidden files, or duplicate files or directories in your submission.
  2. Late submissions will be subject to the late penalty described in the course outline. No exceptions will be made, including for technical and/or connectivity issues. Do not wait until the last day to submit.
  3. Only files uploaded into cuLearn will be graded. Submissions that contain incorrect, corrupt, or missing files will receive a grade of zero. Corrections to submissions will not be accepted after the due date and time, for any reason.