This is a MASM Project Assembly Code for x86 Processors ! Encryption and Decryption Program

This is a MASM Project Assembly Code for x86 Processors !

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

we will be using all of the assembly language instruction we have learned, including:

  • Loops
  • Procedures
  • Boolean Operations
  • Conditional jumps
  • Bitwise operations (shifts and rotates)
  • Integer arithmetic
  • String primitive Instructions
  • Windows functions for I/O*

(We will be covering Windows I/O functions more in week 15)

The Program

For this project, you will be creating two assembly language programs. The first program will read input from a user, encrypt it, and store it in a text file. The second will read the text file, decrypt the text, and display it to the console. Both of these programs need to gracefully handle errors.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

The first program will follow these steps (details of each step are given further down the page):

  • Tell the user to type anything of their choice, or just ‘q’ to quit.
  • Read the user’s input
  • If the user entered just ‘q’, end the program
  • Else, count the number of lowercase e’s in the input
  • Find the number of e’s modulo 8 (the remainder when divided by 8)
  • If the modulo is 0, encrypt the data by flipping all of the bits of each character in the input
  • If the modulo is not 0, encrypt the data by rolling the bits of each character to the left a number of times equal to the modulo
  • Write the length of the input, the modulo number, and then the encrypted input to the text file
  • Repeat from step 2

The second program will follow these steps:

  • If the text file does not exist, output an error message and quit
  • If it does exist, read the first input length and modulo number from the text file
  • If there is nothing to read, end the program
  • Use the length of the input to read the first batch of input
  • If the modulo number is 0, decrypt the data by flipping all of the bits of each character of the input
  • If the modulo is not 0, decrypt the data by rolling each character to the right a number of times equal to the modulo
  • Write the decrypted input to the console
  • Repeat from step 2

You can use anything we have learned in this course up to this point to accomplish this task. It is up to you to decide what variables, procedures, and macros you need to create for this program. You can split each program into multiple procedures if you want.

The two programs will NOT be in the same Visual Studio project; since they each need their own main procedure, they will generate an error if they are both placed in the same project. Any data that is the same between the two files (such as the input buffer size and the text file name) can be hard-coded into each file.

Input and Output

The two programs will need to perform inputs and outputs from both the console and from files.

The first program will continue to read inputs from the user until they enter ‘q’. The second program will read from the text file until it reaches the end of the file. You will want to make sure that the input buffer for both programs is sufficiently large (100-300 bytes should be sufficient) and the same between both programs.

For console I/O may use the

IO_Procedures.asm

Download IO_Procedures.asm

file (which we used back in the week 12 assignment), or you can implement the Win32 ReadConsoleA and WriteConsoleA functions yourself.

We will be learning more about the file I/O functions in week 15. The minimum that you need to know to use them will be given here. You may want to put off writing the file I/O code until you know more about how Windows functions work, or you can use the code below and review/rewrite it once you are more comfortable with Windows functions in general.

Writing Text to a File

You will need to use 3 Windows procedures to write text to a file. The prototypes are:

CreateFileA PROTO, lpFilename:PTR BYTE, dwDesiredAccess:DWORD, dwShareMode:DWORD, lpSecurityAttributes:DWORD, dwCreationDisposition:DWORD, dwFlagsAndAttributes:DWORD, hTemplateFile:DWORD WriteFile PROTO, hFile:DWORD, lpBuffer:PTR BYTE, nNumberOfBytesToWrite:DWORD, lpNumberOfBytesWritten:PTR DWORD, lpOverlapped:PTR DWORD CloseHandle PROTO, hObject:DWORD

The CreateFileA procedure is used to create or open a file for reading or writing (the permissions are provided as parameters). WriteFile is used to write text to the opened file. CloseHandle is what closes the file after all writes are complete. (The prototypes for CreateFileA and WriteFile were split into multiple lines because they’re very long.)

To use these functions, perform the following steps:

  • Include all the prototypes at the top of your file.
  • At the beginning of your program, invoke CreateFileA with the following arguments in order:the offset of a string containing the name of the file to open the number 40000000h (this tells the procedure that you want to open the file for generic text writing)the number 0 (this is the share mode, which is going unused)the number 0 (this is the security attributes, which are going unused)the number 2 (this is the creation disposition). This parameter tells the computer to always create a new empty file with the specified name. If one already exists, it is erased and replaced with a new empty file.the number 128 (this is the flags for file creation – this is the “normal flags” option)the number 0 (this is the template file, which is going unused)
  • After calling CreateFileA the file’s handle will be placed in EAX. The handle is how your program can access the file that was just created. You should immediately save this handle in a variable since you will need it for the rest of the program.
  • When you want to write something to the file, invoke WriteFile with the following arguments in order:the file’s handle that you saved after invoking CreateFileAthe offset of the string you want to writethe number of bytes you want to writethe offset of a DWORD variable. This variable will store the number of bytes that were successfully written by the procedure. This is required for the procedure to be called, but is not necessary for our purposes.the number 0 (this is an “overlap” option, which is going unused)
  • Call WriteFile as many times as you want to keep writing more text into the file. All text will appear sequentially in the file.
  • When your program needs to end and you are done writing text, invoke CloseHandle and pass the file’s handle as the only parameter. This closes the file that you opened and saves all of the text you have written. If you don’t close the handle before your program ends, the file may not be written properly.

Reading Text from a File

You will need to use 4 Windows procedures to read text from a file. The prototypes are:

CreateFileA PROTO, lpFilename:PTR BYTE, dwDesiredAccess:DWORD, dwShareMode:DWORD, lpSecurityAttributes:DWORD, dwCreationDisposition:DWORD, dwFlagsAndAttributes:DWORD, hTemplateFile:DWORD ReadFile PROTO, hFile:DWORD, lpBuffer:PTR BYTE, nNumberOfBytesToRead:DWORD, lpNumberOfBytesRead:PTR DWORD, lpOverlapped:PTR DWORD CloseHandle PROTO, hObject:DWORD GetLastError PROTO

The CreateFileA and CloseHandle procedures are the exact same ones used when writing to a file. ReadFile is the counterpart to WriteFile and works in the same way, except that it reads text from a file. GetLastError is used to determine if the file you want to read from exists.

To use these functions, perform the following steps:

  • Include all the prototypes at the top of your file.
  • At the beginning of your program, invoke CreateFileA with the following arguments in order (the ones that are different from before are bolded):the offset of a string containing the name of the file to open the number 80000000h (this tells the procedure that you want to open the file for generic text reading)the number 0 (this is the share mode, which is going unused)the number 0 (this is the security attributes, which are going unused)the number 3 (this is the creation disposition). This parameter tells the computer to only open the file if it exists. If a file with the specified name does not exist, this procedure will do nothing.the number 128 (this is the flags for file creation – this is the “normal flags” option)the number 0 (this is the template file, which is going unused)
  • After calling CreateFileA the file’s handle will be placed in EAX. The handle is how your program can access the file that was just created. You should immediately save this handle in a variable since you will need it for the rest of the program.

  • Before running the rest of your program, invoke GetLastError with no parameters to determine if the file you attempted to open exists. This procedure will set EAX to a value based on the last generated error. The code for “file not found” is 2, so if EAX equals 2 after you call GetLastError then no file was opened and you can end the program prematurely. (You don’t have to close the file handle if it was not successfully opened.)
  • When you want to read something from the file, invoke ReadFile with the following arguments in order:the file’s handle that you saved after invoking CreateFileAthe offset of a buffer to read into (whatever’s read from the file will be placed in the buffer)the number of bytes you want to readthe offset of a DWORD variable. This variable will store the number of bytes that were successfully read by the procedure. You can check this number if you want to be sure that you successfully read everything that you wanted. If you read from the file and this variable says that you read 0 bytes, then you are at the end of the file and there is nothing left to read.the number 0 (this is an “overlap” option, which is going unused)
  • Call ReadFile as many times as you want to keep reading the file’s contents. Each read will move through the file sequentially – you won’t read the same text from the file twice.
  • When your program needs to end and you are done reading text, invoke CloseHandle and pass the file’s handle as the only parameter. This closes the file that you opened. If you don’t close the handle before your program ends, the file may be corrupted or your program may crash.

Encrypting the Data

The main goal of these program is to encrypt/decrypt the strings entered by the user. In the first program, after you have gotten input from the user you encrypt it by following these steps:

  • Start by counting the number of lowercase e’s in the user’s input. You can either read through the input string yourself or use string primitive instructions.
  • Find the number of e’s modulo 8. That means to find the remainder after dividing by 8. You can easily obtain this using the DIV instruction. Save this modulo number – it will be needed for several steps later.
  • If the modulo number is 0, then you will encrypt the string by flipping all of the bits of the input string. Use either a NOT or an XOR instruction to accomplish this.
  • If the modulo number is not 0, then you will encrypt the string by rolling each character to the left. The number of spaces to roll them will be equal to the modulo (so if the modulo is 3, you will roll each character 3 to the left). The roll instruction only accepts immediate values or the CL register the number operand. Since you can’t hard-code in the number of rolls, you will need to use CL (see section 7.1 in the textbook for more details). You should rotate each character in isolation – the bits from one character do not need to carry into the next one.

When you write the encrypted input string to your file, you will need to perform 3 separate writes:

  • A single byte counting the length of the input string
  • A single byte containing the modulo number calculated above
  • The entirety of the input string

Since the user can input as much text as they want, your program will need to repeat this process in a loop. Each input from the user will be encrypted slightly differently (because they will have different numbers of e’s) and each input will be written to the text file sequentially with its own length and modulo number.

Decrypting the Data

In the second program, you will need to read the contents of the file and decrypt it. If you wrote the length of the string and the modulo number to the file correctly in the other program, you can then read those values to make it easier to decrypt:

  • Read the first two bytes of the text file. The first byte will be the length of the input and the second will be the modulo number. Save these values.
  • Read the input. The number of bytes to read is the input length value you just obtained in step 1.
  • If the modulo number is 0, then you will decrypt the string by flipping all of the bits of the input string.
  • If the modulo number is not 0, then you will decrypt the string by rolling each character to the right. This requires the exact same process as the encryption, except you roll to the right instead of left.

Once the input string is decrypted you can output it to the console. It should appear exactly as it did when first entered, even with the line breaks in the same places. You can then repeat the process from step 1, since the next portion of input to decrypt will have its own length and modulo number.

Checking if the File Exists

The first program should always create and overwrite the text file, but the second program should only read the file. If the file does not exist, your program should write an error message and then quit. You can check if it doesn’t exist by calling CreateFileA and then GetLastError. The GetLastError function puts the program’s most recent error code in the EAX register. The error code for “file not found” is 2, so if EAX = 2 after you try to open the file and call GetLastError then there is no file to open.

Once you are finished, submit both of your complete .asm program files. Please submit ALL of the files that you used in this project!

This is a MASM Project Assembly Code for x86 Processors !
we will be using all of the assembly language instruction we have learned, including:








Loops
Procedures
Boolean Operations
Conditional jumps
Bitwise operations (shifts and rotates)
Integer arithmetic
String primitive Instructions
Windows functions for I/O*
(We will be covering Windows I/O functions more in week 15)
The Program
For this project, you will be creating two assembly language programs. The first
program will read input from a user, encrypt it, and store it in a text file. The second will
read the text file, decrypt the text, and display it to the console. Both of these programs
need to gracefully handle errors.
The first program will follow these steps (details of each step are given further down the
page):
1. Tell the user to type anything of their choice, or just ‘q’ to quit.
2. Read the user’s input
3. If the user entered just ‘q’, end the program
4. Else, count the number of lowercase e’s in the input
5. Find the number of e’s modulo 8 (the remainder when divided by 8)
6. If the modulo is 0, encrypt the data by flipping all of the bits of each character
in the input
7. If the modulo is not 0, encrypt the data by rolling the bits of each character to
the left a number of times equal to the modulo
8. Write the length of the input, the modulo number, and then the encrypted
input to the text file
9. Repeat from step 2
The second program will follow these steps:
1. If the text file does not exist, output an error message and quit
2. If it does exist, read the first input length and modulo number from the text
file
3. If there is nothing to read, end the program
4. Use the length of the input to read the first batch of input
5. If the modulo number is 0, decrypt the data by flipping all of the bits of each
character of the input
6. If the modulo is not 0, decrypt the data by rolling each character to the right a
number of times equal to the modulo
7. Write the decrypted input to the console
8. Repeat from step 2
You can use anything we have learned in this course up to this point to accomplish this
task. It is up to you to decide what variables, procedures, and macros you need to create
for this program. You can split each program into multiple procedures if you want.
The two programs will NOT be in the same Visual Studio project; since they each need
their own main procedure, they will generate an error if they are both placed in the same
project. Any data that is the same between the two files (such as the input buffer size
and the text file name) can be hard-coded into each file.
Input and Output
The two programs will need to perform inputs and outputs from both the console and
from files.
The first program will continue to read inputs from the user until they enter ‘q’. The
second program will read from the text file until it reaches the end of the file. You will
want to make sure that the input buffer for both programs is sufficiently large (100-300
bytes should be sufficient) and the same between both programs.
For console I/O may use the IO_Procedures.asm Download IO_Procedures.asmfile
(which we used back in the week 12 assignment), or you can implement the Win32
ReadConsoleA and WriteConsoleA functions yourself.
We will be learning more about the file I/O functions in week 15. The minimum that you
need to know to use them will be given here. You may want to put off writing the file
I/O code until you know more about how Windows functions work, or you can use the
code below and review/rewrite it once you are more comfortable with Windows
functions in general.
Writing Text to a File
You will need to use 3 Windows procedures to write text to a file. The prototypes are:
CreateFileA PROTO, lpFilename:PTR BYTE, dwDesiredAccess:DWORD, dwShareMode:DWORD, lpS
ecurityAttributes:DWORD,
dwCreationDisposition:DWORD, dwFlagsAndAttributes:DWORD, hTemplate
File:DWORD
WriteFile PROTO, hFile:DWORD, lpBuffer:PTR BYTE, nNumberOfBytesToWrite:DWORD, lpNumbe
rOfBytesWritten:PTR DWORD,
lpOverlapped:PTR DWORD
CloseHandle PROTO, hObject:DWORD
The CreateFileA procedure is used to create or open a file for reading or writing (the
permissions are provided as parameters). WriteFile is used to write text to the opened
file. CloseHandle is what closes the file after all writes are complete. (The prototypes for
CreateFileA and WriteFile were split into multiple lines because they’re very long.)
To use these functions, perform the following steps:
1. Include all the prototypes at the top of your file.
2. At the beginning of your program, invoke CreateFileA with the following
arguments in order:
• the offset of a string containing the name of the file to open
• the number 40000000h (this tells the procedure that you want to
open the file for generic text writing)
• the number 0 (this is the share mode, which is going unused)
• the number 0 (this is the security attributes, which are going
unused)
• the number 2 (this is the creation disposition). This parameter tells
the computer to always create a new empty file with the specified
name. If one already exists, it is erased and replaced with a new
empty file.
• the number 128 (this is the flags for file creation – this is the
“normal flags” option)
• the number 0 (this is the template file, which is going unused)
3. After calling CreateFileA the file’s handle will be placed in EAX. The handle is
how your program can access the file that was just created. You should
immediately save this handle in a variable since you will need it for the rest of
the program.
4. When you want to write something to the file, invoke WriteFile with the
following arguments in order:
• the file’s handle that you saved after invoking CreateFileA
• the offset of the string you want to write
• the number of bytes you want to write
the offset of a DWORD variable. This variable will store the number
of bytes that were successfully written by the procedure. This is
required for the procedure to be called, but is not necessary for our
purposes.
• the number 0 (this is an “overlap” option, which is going unused)
5. Call WriteFile as many times as you want to keep writing more text into the
file. All text will appear sequentially in the file.
6. When your program needs to end and you are done writing text, invoke
CloseHandle and pass the file’s handle as the only parameter. This closes the
file that you opened and saves all of the text you have written. If you don’t close
the handle before your program ends, the file may not be written properly.

Reading Text from a File
You will need to use 4 Windows procedures to read text from a file. The prototypes are:
CreateFileA PROTO, lpFilename:PTR BYTE, dwDesiredAccess:DWORD, dwShareMode:DWORD, lpS
ecurityAttributes:DWORD,
dwCreationDisposition:DWORD, dwFlagsAndAttributes:DWORD, hTemplate
File:DWORD
ReadFile PROTO, hFile:DWORD, lpBuffer:PTR BYTE, nNumberOfBytesToRead:DWORD, lpNumberO
fBytesRead:PTR DWORD,
lpOverlapped:PTR DWORD
CloseHandle PROTO, hObject:DWORD
GetLastError PROTO
The CreateFileA and CloseHandle procedures are the exact same ones used when
writing to a file. ReadFile is the counterpart to WriteFile and works in the same way,
except that it reads text from a file. GetLastError is used to determine if the file you
want to read from exists.
To use these functions, perform the following steps:
1. Include all the prototypes at the top of your file.
2. At the beginning of your program, invoke CreateFileA with the following
arguments in order (the ones that are different from before are bolded):
• the offset of a string containing the name of the file to open
• the number 80000000h (this tells the procedure that you want to
open the file for generic text reading)
• the number 0 (this is the share mode, which is going unused)
• the number 0 (this is the security attributes, which are going
unused)
• the number 3 (this is the creation disposition). This parameter tells
the computer to only open the file if it exists. If a file with the
specified name does not exist, this procedure will do nothing.
• the number 128 (this is the flags for file creation – this is the
“normal flags” option)
the number 0 (this is the template file, which is going unused)
3. After calling CreateFileA the file’s handle will be placed in EAX. The handle is
how your program can access the file that was just created. You should
immediately save this handle in a variable since you will need it for the rest of
the program.
4. Before running the rest of your program, invoke GetLastError with no
parameters to determine if the file you attempted to open exists. This
procedure will set EAX to a value based on the last generated error. The code
for “file not found” is 2, so if EAX equals 2 after you call GetLastError then no
file was opened and you can end the program prematurely. (You don’t have to
close the file handle if it was not successfully opened.)
5. When you want to read something from the file, invoke ReadFile with the
following arguments in order:
• the file’s handle that you saved after invoking CreateFileA
• the offset of a buffer to read into (whatever’s read from the file will
be placed in the buffer)
• the number of bytes you want to read
• the offset of a DWORD variable. This variable will store the number
of bytes that were successfully read by the procedure. You can
check this number if you want to be sure that you successfully read
everything that you wanted. If you read from the file and this
variable says that you read 0 bytes, then you are at the end of the
file and there is nothing left to read.
• the number 0 (this is an “overlap” option, which is going unused)
6. Call ReadFile as many times as you want to keep reading the file’s contents.
Each read will move through the file sequentially – you won’t read the same
text from the file twice.
7. When your program needs to end and you are done reading text, invoke
CloseHandle and pass the file’s handle as the only parameter. This closes the
file that you opened. If you don’t close the handle before your program ends,
the file may be corrupted or your program may crash.

Encrypting the Data
The main goal of these program is to encrypt/decrypt the strings entered by the user. In
the first program, after you have gotten input from the user you encrypt it by following
these steps:
1. Start by counting the number of lowercase e’s in the user’s input. You can
either read through the input string yourself or use string primitive
instructions.
2. Find the number of e’s modulo 8. That means to find the remainder after
dividing by 8. You can easily obtain this using the DIV instruction. Save this
modulo number – it will be needed for several steps later.
3. If the modulo number is 0, then you will encrypt the string by flipping all of
the bits of the input string. Use either a NOT or an XOR instruction to
accomplish this.
4. If the modulo number is not 0, then you will encrypt the string by rolling each
character to the left. The number of spaces to roll them will be equal to the
modulo (so if the modulo is 3, you will roll each character 3 to the left). The
roll instruction only accepts immediate values or the CL register the number
operand. Since you can’t hard-code in the number of rolls, you will need to use
CL (see section 7.1 in the textbook for more details). You should rotate each
character in isolation – the bits from one character do not need to carry into
the next one.
When you write the encrypted input string to your file, you will need to perform 3
separate writes:



A single byte counting the length of the input string
A single byte containing the modulo number calculated above
The entirety of the input string
Since the user can input as much text as they want, your program will need to repeat this
process in a loop. Each input from the user will be encrypted slightly differently (because
they will have different numbers of e’s) and each input will be written to the text file
sequentially with its own length and modulo number.
Decrypting the Data
In the second program, you will need to read the contents of the file and decrypt it. If
you wrote the length of the string and the modulo number to the file correctly in the
other program, you can then read those values to make it easier to decrypt:
1. Read the first two bytes of the text file. The first byte will be the length of the
input and the second will be the modulo number. Save these values.
2. Read the input. The number of bytes to read is the input length value you just
obtained in step 1.
3. If the modulo number is 0, then you will decrypt the string by flipping all of
the bits of the input string.
4. If the modulo number is not 0, then you will decrypt the string by rolling each
character to the right. This requires the exact same process as the encryption,
except you roll to the right instead of left.
Once the input string is decrypted you can output it to the console. It should appear
exactly as it did when first entered, even with the line breaks in the same places. You can
then repeat the process from step 1, since the next portion of input to decrypt will have
its own length and modulo number.
Checking if the File Exists
The first program should always create and overwrite the text file, but the second
program should only read the file. If the file does not exist, your program should write an
error message and then quit. You can check if it doesn’t exist by calling CreateFileA and
then GetLastError. The GetLastError function puts the program’s most recent error code
in the EAX register. The error code for “file not found” is 2, so if EAX = 2 after you try to
open the file and call GetLastError then there is no file to open.
Once you are finished, submit both of your complete .asm program files. Please submit
ALL of the files that you used in this project!
Project Templet (Fully Editable):

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER