Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sending encrypted file over com port

Hey, my program is ment to encrypt "c:\temp\Transmisson.txt" with a password string (that you type in using the scanf function) and then send the encrypted file over the com port. The program keeps crashing though as soon as it gets to the first scanf as it doesnt seem to store the value of the password string. Below is my code, any questions please message me, any help will be appreciated.


#include
#include
#include


#define CNTRLZ 0x9A
#define constkeysize 2
#define BLOCK_SIZE 1000
#define ENDSIGN 64

char Com_port_1[] ="COM1"; // COM1 string - used by fopen to open COM1 port
char Com_port_mode[] ="w"; // String used by fopen to open specified port in write mode
char File_port_1[128] = "C:\temp\Transmisson.txt"; // Default address of transmit source file
char File_port_mode[] ="r"; // String used by fopen to open transmit source file in read mode
char password_file[] ="C:\temp\Key.txt"; // Address of Encryption key file
char Key_port_mode[] ="r"; // String used by fopen to open encryption key file in read mode
char scans[] ="%s"; // String used by scanf to expect string input from keyboard

char dstmessage[] ="
Please enter location for file to transmit
";
char key_error_message_1[] ="
Error opening password key file
";
char key_error_message_2[] ="
Error reading from password key file
";
char transmit_file_error_message[] ="
Error opening from transmit source file
";
char com1_error_message[] ="
Error opening from COM1 port
";
char finish_message[] ="
Finished Transmitting
";
char password[100];

int next_tx_char;
int return_value;
int encrypted_char;
int password_key_size = 100;

FILE *com_port; // Pointer to COM1 port for transmission
FILE *transmit_file; // Pointer to transmit source file
FILE *key_file; // Pointer to encryption key file


int main(void)
{
__asm

{
; Get name of file to transmit from keyboard

lea eax, dstmessage ; Pass pointer to "Ask for Filename" message on stack
push eax
call printf ; Print out "Ask for Filename" message on screen
add esp, 4 ; Scrub stack
lea eax, File_port_1 ; Pass pointer to "Response Input" message on stack
push eax
lea eax, scans ; Expecting a string to be typed in
push eax
call scanf ; Get filename input from keyboard
add esp, 8 ; Scrub stack

; Open specified transmit source file for reading

lea eax, File_port_mode ; Pass "r" to fopen on stack to indicate open in read mode
push eax
lea eax, File_port_1 ; Pass source file pointer to fopen on stack
push eax
call fopen ; Open transmit source file in read mode
mov transmit_file,eax ; Save pointer to transmit source file in "transmit_file" variable
add esp, 8 ; Scrub stack
cmp transmit_file, NULL ; Check if fopen call was successful
jne open_encryption_file ; If so continue with program
lea eax, transmit_file_error_message ; Pass pointer to "key_error_message" message on stack
push eax
call printf ; Print out Encryption key error message on screen
add esp, 4 ; Scrub stack
jmp end ; Finish

; Open encryption key file ("C:\temp\Key.txt") for reading
open_encryption_file:
lea eax, Key_port_mode ; Pass "r" to fopen on stack to indicate open in read mode
push eax
lea eax, password_file ; Pass encryption file pointer to fopen on stack
push eax
call fopen ; Open encryption key file in read mode
mov key_file,eax ; Save pointer to encryption key file in "key_file" variable
add esp, 8 ; Scrub stack
cmp key_file, NULL ; Check if fopen call was successful
jne read_encryption_file ; If so continue with program
lea eax, key_error_message_1 ; Pass pointer to "key_error_message" message on stack
push eax
call printf ; Print out Encryption key error message on screen
add esp, 4 ; Scrub stack
jmp end ; Finish

; Read encryption key into "password" array
read_encryption_file:
lea eax, key_file ; Initialise register with the address of the encryption key
push eax
mov eax, 100 ; Max size of file
push eax
lea eax, password ; Pass pointer for password onto the stack
push eax
call fgets ; Read encryption string from key file
mov return_value, eax
add esp, 12 ; Scrub stack
cmp return_value, NULL ; Check if fgets call was successful
jne open_com_port ; If so continue with program
lea eax, key_error_message_2 ; Pass pointer to "key_error_message" message on stack
push eax
call printf ; Print out Encryption key error message on screen
add esp, 4 ; Scrub stack
jmp end ; Finish

; Open COM1 port for writing
open_com_port:
lea eax, Com_port_mode ; Pass "w" to fopen on stack to indicate open in write mode
push eax
lea eax, Com_port_1 ; Pass pointer to COM1 port on stack
push eax
call fopen ; Open COM1 port in write mode
mov com_port, eax ; Save pointer to COM1 port in "com_port" variable
add esp, 8 ; Scrub stack
cmp com_port, NULL ; Check if fopen call was successful
jne start_encryption ; If so continue with program
lea eax, com1_error_message ; Pass pointer to "com1_error_message" message on stack
push eax
call printf ; Print out com1 error message on screen
add esp, 4 ; Scrub stack
jmp end ; Finish
////////////////////////End of Initialisation///////////////////

start_encryption:

lea ebx, password ; Set ebx to point to password string
next_char:
lea eax, transmit_file ; Pass pointer to transmit file on stack
push eax
call fgetc ; Get next character from transmit file
mov next_tx_char, eax ; Save next character to transmit in "next_tx_char" variable
add esp, 4 ; Scrub stack
cmp next_tx_char, EOF ; Go to "end" if end of transmit file reached
je end
cmp [ebx], 0 ; End of password string ?
jne continue
lea ebx, password ; If so reset back to beginning of password string
continue:
mov eax, next_tx_char
xor eax, [ebx] ; Encrypt using exclusive OR operation between encryption string character and password string character
mov encrypted_char, eax ; Store encrypted value in "encrypted_char"
lea eax, com_port
push eax
lea eax, encrypted_char
push eax
call fputc ; Transmit encrypted character
add esp, 8 ; Scrub stack
inc ebx ; Point to next character of password string
jmp next_char ; Repeat

end:
mov eax, com_port ; Flush the buffer to send character
push eax
call fflush
add esp, 4 ; Scrub stack
mov eax, com_port ; Close COM1 port
push eax
call fclose
add esp, 4 ; Scrub stack

mov eax, transmit_file ; Close trasnmit source file
push eax
call fclose
add esp, 4 ; Scrub stack



lea eax, finish_message ; Pass pointer to "Finished" message on stack
push eax
call printf ; Print out "Ask for Filename" message on screen
add esp, 4 ; Scrub stack
lea eax, File_port_1 ; Pass pointer to "Response Input" message on stack
push eax
lea eax, scans ; Expecting a string to be typed in
push eax
call scanf ; Get filename input from keyboard
add esp, 8 ; Scrub stack

}
return 0;

}
Sign In or Register to comment.