Write an assembly language program to implement the following encryption/decrypt
ID: 3866990 • Letter: W
Question
Write an assembly language program to implement the following encryption/decryption algorithm. Your program will read in a key file, a message file to encrypt/decrypt, a user-entered password, and as an option, the number of rounds as in the following command line: (order is irrelevant) To encrypt: eryptor.exe - e - i - k - p [- r -o ] To decrypt: eryptor.exe -d -i - k - p [- r - o ] If no password is specified, the password will be "password" for that run of the program. The number of rounds " - r" is optional and will default to one with a maximum of three. I will provide a keyfile called "key.dat" and that should be used for testing your program, however you can rename it. The default output filename is the input filename with a ".enc" extension if encrypted and a ". dec" extension if decrypted. I provide a C shell program for every group to handle some of the mundane tasks such as parsing the command line input and hashing the password. The key file is 65537 bytes in length (indexed 0 to 65536) and that length is an integral part for allowing this simple algorithm to work - do not alter the length. Your program should read that key file into a global byte array that is exactly 65537 bytes in length. The SHA-256 hash will process the password and output 32 bytes of hash data into an array. This data will be used to get both the starting point and the hop count for the encryption/decryption. The first two bytes of the hash will be the starting point (0 - 65535) and the next two bytes will get the hop count (1 to 65536, use a zero to mean 65536). The following C code functionality must be accomplished in assembly language for(round = 0: roundExplanation / Answer
Firstly, there are two kinds of swapping the nibbles:
1. a normal nibble swap, ie, 0xD2 -> 0x2D. A nibble is 4 bits. The hexadecimal number 0xD2 can be written as a binary number as follows. D = 1101 and 2 = 0010. So 0xD2 in hexadecimal is 1101 0010 in binary. Now we exchange the lower 4 bits with the higher 4 bits. So the new number is 0010 1101 which translates to 0x2D in hexadecimal. The logic in C is as follows:
int a = 0xD2;
int temp; \ this is a temporary variable which we will use in order to get the swap of nibbles.
temp = a; \ copying the value of a in temporary variable
a = a & 0x0F; \ now a contains the lower 4 bits of the number, ie a = 0x02
temp = temp & 0xF0; \ now temp contains the higher 4 bits of the number, ie temp = 0xD0
\ now we need to shift the positions of both. It can be done using the bit shift operator << and >>
a = a << 4; \ left shift the value in a, so that a = 0x20
temp = temp >> 4; \ right shift value in temp so that temp = 0x0D
\ Now we do bitwise or on them to combine the results.
a = a | temp;
\ now a = 0x2D;
2. Half nibble swap:
Here if the number is 0xB4, ie 1011 0100, then each 4 bits is swapped within itself. Ie, 1011 becomes 1110 and 0100 becomes 0001, So the new number is 1110 0001
This can be done as follows in C:
int a = 0xB4;
int temp = a; \use a temporary variable
a = a & 0x33; \ we preserve lower two bits in upper nibble and lower nibble of the number. The rest become zero
temp = temp & 0xCC; \ we preserve upper two bits in lower and uppper nibble of the number. The rest become zero
a = a << 2; \ left shift by two bits, so that the lower bits of each nibble now become the upper bits
temp = temp >> 2; \ right shift by two so that the upper two bits of each nibble become the lower bits
a = a | temp; \ combine the results.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.