Starting from:

$30

CS204-Homework 6 Templates and Bit Operations Solved

Cryptography, in which anything digital heavily depends on for (to-some-degree) secure data sharing, is an important part of the computer science, if not tedious. In this homework, you are going to perform a very simple cryptography operation. Any cryptologic algorithm performs its task by encrypting the message (making it unreadable) and decrypting it (converting back to human readable form). Most of the cryptography algorithms use a key to encrypt and decrypt messages.

 

In this homework, you are given a cpp file with main. You are going to write a template class which operates encryption and decryption operations on chars, short integers, long integers, long long integers. The encryption operation will consist of three stages: 

 

XOR operation
Circular shifting
Flipping the given bits
 

Your template class has 3 fields: key for XOR operation, nShift for number of shifts in circular shifting and listFlips for the list of bits to flip.

 

The class, named Encrypter, will have a constructor and 2 member functions: encrypt, decrypt. Encrypt function will do the afore-mentioned operation in the given order. And decrypted function will do the operations in the reverse order to decrypt the data successfully. 

 

 

 

 

 

1- XOR operation
 

It will take the input and operate bitwise xor operation on it with the key. The following image shows the result of sample XOR operation.

Then, the output of the xor operation will be given to Circular shifting operation as input.

 

2- Circular Shifting
 

It is the shifting operation with moving the final bit to the first bit while shifting. With this approach of shifting, we achieve to prevent loss while shifting. Let’s say we want to shift our 0001 0111 with circular shift operation to left and right. Images below shows the operation.

 

 

In the encryption function you are going to implement, it will shift the bits nShifts many times to left. And at decryption stage, it will shift the bits nShifts many times to right. If nShifts is greater than or equal to the bit size of data to be shifted, it will take the mod(%) of nShifts and then shift that many times.  

 

3- Flip the given bits
 

While creating Encrypter object, a vector of bits to flip will be given. Let’s say listFlips is given as follows: {2, 3, 4, 7}. Then as seen in the following image, 2nd, 3rd, 4th and 7th bits of the result of circular shift operation will be flipped. 

 

 

If there is a bit greater than the bit count of the data type, they will be neglected and won’t be in the listFlips vector. Let’s say our datatype to be encrypted and decrypted is char which is 1 byte(8 bits). If the list of bits to flip given to the constructor of the Encrypter for char is given as follows: {0, 5, 7, 12, 19}. Since 12 and 19 is greater than 7(the maximum index we can hav efor char datatype), they will be rejected and our listFlips vector will be as follows: {0, 5, 7}. Than only these bits will be flipped.

More products