will.socialengineer
March 10th, 2004, 06:11 PM
Note: I'm still fresh when it comes to ASM. :)
Ok, I am writing a simple text encoder (and a separate program for decoding)... However i have a few bugs I need to work out. Actually, I have just one that I would like help on right now, I can probably get the rest myself - however this one has me stumped.
Anyway, this part of the code (at line 146, in the full source) handles the actual encryption of the text. It just XORs the original character's value with a number from the key file... Pretty easy to figure out what it is doing from the snippet here:
mov esi,0
mov ecx,SIZEOF msgbuffer
mov edi,0
L1:
mov al,msgbuffer[esi]
xor al,kbuffer[edi]
daa
.IF edi == SIZEOF kbuffer
mov edi,0
.ENDIF
mov encbuffer[esi],al
inc esi
inc edi
loop L1
It doesn't work, though. I'm guessing because I'm not actually XORing with a numeric value (from the key - considering it is reading it as a string). So, I guess I am asking... How do I get it to read it as a number? I'm assemblying it with MASM. Here is the fulll source (note: This isn't complete by any means. I'm not actually reading the key file from the command trail at the moment). If you want to try it out, you need a file called "message" (the original message), and "key.k" (the key file).
TITLE Simple Encryption Messenger Encoder, Win32 alpha release (sem.w32.asm)
;*******************************************************************************
;This program allows for basic encrypted communcation between 2 people. The
;encryption operates with a simple key concept; The key stores the encryption
;mask, which is a series of numbers used in mathematical operations to the ASCII
;values of each letter. The decryption reverses this process.
;*******************************************************************************
;
;EXAMPLE USAGE:
;
; sample.k = a key file used to encrypt the message. It is a basic text file
; with the contents "12345" (minus quotes).
; message = the text file to encrypt. It is a basic text file with the
; contents "Shoot, I can't read this crap!".
; semw32enc.exe = the encryption program.
; semw32dec.exe = the decryption program.
; --------------------------------------------------
; The user will type the following at the command prompt:
; semw32enc.exe sample.k
; which will encrypt the file "message" with sample.k, assuming all the files
; are in the present working directory. The
; program would output he encrypted message into the file messagecrypt.
; this file could then be sent to the recipient via e-mail, sneakernet, whatever
; - it doesn't really matter right now, as I haven't coded the program used to
; send the messages. Once the other user recieves the message, he/she would
; type the following to then decrypt the message:
; semw32dec.exe sample.k
; which will then output the decrypted message to messagedecrypted.
;
;PLANS FOR NEXT RELEASE:
; Implement the IM protocol (using C++).
; Implement stronger encryption (currently just xors the #s of the key in order)
INCLUDE Irvine32.inc
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.data
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msgbuffer BYTE 500 DUP(?) ;buffer used when reading the original file
msgbufSize = ($-msgbuffer)
kbuffer BYTE 500 DUP(?) ;buffer used when reading the key file
kbufSize = ($-kbuffer)
errKeyMsg BYTE "Can't open the key file.",0dh,0ah,0
errTextMsg BYTE "Can't open the message file.",0dh,0ah,0
errEncMsg BYTE "Can't open the encoding file.",0dh,0ah,0
msgfn BYTE "message",0 ;filename of the message
encmsgfn BYTE "messagecrypt",0 ;filename of the encrypted message
keyfn BYTE "key.k",0 ;129 DUP(?) ;filename of the key file
msgHandle DWORD ?
keyHandle DWORD ?
encHandle DWORD ?
msgbyteCount DWORD ?
keybyteCount DWORD ?
encbytesWritten DWORD ?
encbuffer BYTE 500 DUP(?) ;buffer used when writing encrypted file
encbufSize = ($-encbuffer)
testmsg BYTE "Test!",0dh,0ah,0
var1 LABEL DWORD
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.code
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main PROC
;Lets get the command tail first thing...
;mov ax,@data
;mov ds,ax
;mov dx,OFFSET keyfn
;call Get_Commandtail
;Then we are going to read the message file...
INVOKE CreateFile, ;Opens the message file
ADDR msgfn,
GENERIC_READ,
DO_NOT_SHARE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
mov msgHandle,eax ;Takes care of saving the file handle
.IF eax== INVALID_HANDLE_VALUE
mov edx,OFFSET errTextMsg ;Displays the error message if it doesn't exist.
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE ReadFile, ;Reads the message into msgbuffer
msgHandle,
ADDR msgbuffer,
msgbufSize,
ADDR msgbyteCount,
0
INVOKE CloseHandle, ;Closes the file
msgHandle
mov esi,msgbyteCount
mov msgbuffer[esi],0
mov edx,OFFSET msgbuffer
call WriteString
;Here we get the key file.
INVOKE CreateFile, ;Opens the key file
ADDR keyfn,
GENERIC_READ,
DO_NOT_SHARE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
mov keyHandle,eax ;Takes care of saving the file handle
.IF eax== INVALID_HANDLE_VALUE
mov edx,OFFSET errKeyMsg ;Displays the error message if it doesn't exist.
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE ReadFile, ;Reads the key file into kbuffer
keyHandle,
ADDR kbuffer,
kbufSize,
ADDR keybyteCount,
0
INVOKE CloseHandle, ;Closes the file
keyHandle
mov esi,keybyteCount
mov kbuffer[esi],0
mov edx,OFFSET kbuffer
call WriteString
;Done reading file, getting ready to copy the msgbuffer into encbuffer, while
;encrypting using the key.
mov esi,0
mov ecx,SIZEOF msgbuffer
mov edi,0
L1:
mov al,msgbuffer[esi]
xor al,kbuffer[edi]
daa
.IF edi == SIZEOF kbuffer
mov edi,0
.ENDIF
mov encbuffer[esi],al
inc esi
inc edi
loop L1
exit
;that was easy... Now writing encoded message into messagecrypt file.
INVOKE CreateFile, ;Opens the file
ADDR encmsgfn,
GENERIC_WRITE,
DO_NOT_SHARE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0
mov encHandle,eax ;saves file handle
.IF eax == INVALID_HANDLE_VALUE
mov edx,OFFSET errEncMsg ;write error message to screen
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE WriteFile, ;writes the encoded message to the file
encHandle,
ADDR encbuffer,
encbufSize,
ADDR encbytesWritten,
0
INVOKE CloseHandle, encHandle
; jmp Quit
;Quit:
INVOKE ExitProcess,0 ;KILL ME NOW!
main ENDP
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
END main
*edit* after doing some more checking, it seems that it probably isn't my encryption loop that is having issues. Not sure what it is.
*edit again* nope, I got it working now with a constant key #. Jeez ;) I really should write the decryption program now.
Ok, I am writing a simple text encoder (and a separate program for decoding)... However i have a few bugs I need to work out. Actually, I have just one that I would like help on right now, I can probably get the rest myself - however this one has me stumped.
Anyway, this part of the code (at line 146, in the full source) handles the actual encryption of the text. It just XORs the original character's value with a number from the key file... Pretty easy to figure out what it is doing from the snippet here:
mov esi,0
mov ecx,SIZEOF msgbuffer
mov edi,0
L1:
mov al,msgbuffer[esi]
xor al,kbuffer[edi]
daa
.IF edi == SIZEOF kbuffer
mov edi,0
.ENDIF
mov encbuffer[esi],al
inc esi
inc edi
loop L1
It doesn't work, though. I'm guessing because I'm not actually XORing with a numeric value (from the key - considering it is reading it as a string). So, I guess I am asking... How do I get it to read it as a number? I'm assemblying it with MASM. Here is the fulll source (note: This isn't complete by any means. I'm not actually reading the key file from the command trail at the moment). If you want to try it out, you need a file called "message" (the original message), and "key.k" (the key file).
TITLE Simple Encryption Messenger Encoder, Win32 alpha release (sem.w32.asm)
;*******************************************************************************
;This program allows for basic encrypted communcation between 2 people. The
;encryption operates with a simple key concept; The key stores the encryption
;mask, which is a series of numbers used in mathematical operations to the ASCII
;values of each letter. The decryption reverses this process.
;*******************************************************************************
;
;EXAMPLE USAGE:
;
; sample.k = a key file used to encrypt the message. It is a basic text file
; with the contents "12345" (minus quotes).
; message = the text file to encrypt. It is a basic text file with the
; contents "Shoot, I can't read this crap!".
; semw32enc.exe = the encryption program.
; semw32dec.exe = the decryption program.
; --------------------------------------------------
; The user will type the following at the command prompt:
; semw32enc.exe sample.k
; which will encrypt the file "message" with sample.k, assuming all the files
; are in the present working directory. The
; program would output he encrypted message into the file messagecrypt.
; this file could then be sent to the recipient via e-mail, sneakernet, whatever
; - it doesn't really matter right now, as I haven't coded the program used to
; send the messages. Once the other user recieves the message, he/she would
; type the following to then decrypt the message:
; semw32dec.exe sample.k
; which will then output the decrypted message to messagedecrypted.
;
;PLANS FOR NEXT RELEASE:
; Implement the IM protocol (using C++).
; Implement stronger encryption (currently just xors the #s of the key in order)
INCLUDE Irvine32.inc
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.data
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
msgbuffer BYTE 500 DUP(?) ;buffer used when reading the original file
msgbufSize = ($-msgbuffer)
kbuffer BYTE 500 DUP(?) ;buffer used when reading the key file
kbufSize = ($-kbuffer)
errKeyMsg BYTE "Can't open the key file.",0dh,0ah,0
errTextMsg BYTE "Can't open the message file.",0dh,0ah,0
errEncMsg BYTE "Can't open the encoding file.",0dh,0ah,0
msgfn BYTE "message",0 ;filename of the message
encmsgfn BYTE "messagecrypt",0 ;filename of the encrypted message
keyfn BYTE "key.k",0 ;129 DUP(?) ;filename of the key file
msgHandle DWORD ?
keyHandle DWORD ?
encHandle DWORD ?
msgbyteCount DWORD ?
keybyteCount DWORD ?
encbytesWritten DWORD ?
encbuffer BYTE 500 DUP(?) ;buffer used when writing encrypted file
encbufSize = ($-encbuffer)
testmsg BYTE "Test!",0dh,0ah,0
var1 LABEL DWORD
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.code
;|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main PROC
;Lets get the command tail first thing...
;mov ax,@data
;mov ds,ax
;mov dx,OFFSET keyfn
;call Get_Commandtail
;Then we are going to read the message file...
INVOKE CreateFile, ;Opens the message file
ADDR msgfn,
GENERIC_READ,
DO_NOT_SHARE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
mov msgHandle,eax ;Takes care of saving the file handle
.IF eax== INVALID_HANDLE_VALUE
mov edx,OFFSET errTextMsg ;Displays the error message if it doesn't exist.
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE ReadFile, ;Reads the message into msgbuffer
msgHandle,
ADDR msgbuffer,
msgbufSize,
ADDR msgbyteCount,
0
INVOKE CloseHandle, ;Closes the file
msgHandle
mov esi,msgbyteCount
mov msgbuffer[esi],0
mov edx,OFFSET msgbuffer
call WriteString
;Here we get the key file.
INVOKE CreateFile, ;Opens the key file
ADDR keyfn,
GENERIC_READ,
DO_NOT_SHARE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
mov keyHandle,eax ;Takes care of saving the file handle
.IF eax== INVALID_HANDLE_VALUE
mov edx,OFFSET errKeyMsg ;Displays the error message if it doesn't exist.
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE ReadFile, ;Reads the key file into kbuffer
keyHandle,
ADDR kbuffer,
kbufSize,
ADDR keybyteCount,
0
INVOKE CloseHandle, ;Closes the file
keyHandle
mov esi,keybyteCount
mov kbuffer[esi],0
mov edx,OFFSET kbuffer
call WriteString
;Done reading file, getting ready to copy the msgbuffer into encbuffer, while
;encrypting using the key.
mov esi,0
mov ecx,SIZEOF msgbuffer
mov edi,0
L1:
mov al,msgbuffer[esi]
xor al,kbuffer[edi]
daa
.IF edi == SIZEOF kbuffer
mov edi,0
.ENDIF
mov encbuffer[esi],al
inc esi
inc edi
loop L1
exit
;that was easy... Now writing encoded message into messagecrypt file.
INVOKE CreateFile, ;Opens the file
ADDR encmsgfn,
GENERIC_WRITE,
DO_NOT_SHARE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0
mov encHandle,eax ;saves file handle
.IF eax == INVALID_HANDLE_VALUE
mov edx,OFFSET errEncMsg ;write error message to screen
call WriteString
INVOKE ExitProcess,0 ;KILL ME NOW!
.ENDIF
INVOKE WriteFile, ;writes the encoded message to the file
encHandle,
ADDR encbuffer,
encbufSize,
ADDR encbytesWritten,
0
INVOKE CloseHandle, encHandle
; jmp Quit
;Quit:
INVOKE ExitProcess,0 ;KILL ME NOW!
main ENDP
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
END main
*edit* after doing some more checking, it seems that it probably isn't my encryption loop that is having issues. Not sure what it is.
*edit again* nope, I got it working now with a constant key #. Jeez ;) I really should write the decryption program now.