외로운 Nova의 작업실

Assembly 언어 공부 - 23(문자열 암호화 예제 프로그램) 본문

Programming/Assembly

Assembly 언어 공부 - 23(문자열 암호화 예제 프로그램)

Nova_ 2022. 8. 27. 12:28

안녕하세요. 이번 시간에는 문자열 암호화 예제 프로그램으로 XOR 문자열 암호화 프로그램을 어셈블리어로 작성해보도록 하겠습니다.

 

먼저 스터브 프로그램 작성을 해보도록 하겠습니다.

;this program is encryption or decryption


;-----------------INCLUDE-------------------------
;
include 	c:\assembly\irvine32.inc
includelib  c:\assembly\irvine32.lib
includelib  c:\assembly\kernel32.lib
includelib  c:\assembly\user32.lib
;-----------------------------------------------


.data
Key = 231
BUFMAX = 128

sPrompt BYTE "Enter the plain text :", 0
sEncrypt BYTE "Cipher text :     ", 0
sDecrypt BYTE "Decrypted :     ", 0
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
   

.code
main PROC

    call InputTheString
    call TranslateBuffer
    mov edx, OFFSET sEncrypt
    call DisplayMessage
    call TranslateBuffer
    mov edx, OFFSET sDecrypt
    call DisplayMessage
   
    exit
    
main ENDP

;-----------------------------------------------------------------
InputTheString PROC
; 
; Prompt user for a plaintext string, Save the string length to eax
; Receive : nothing
; Return : bufsize = string length
;----------------------------------------------------------------

InputTheString ENDP

;-----------------------------------------------------------------
TranslateBuffer PROC
;
; Translates the string by Exlusive-ORing each
; Byte with the encryption key byte.
; Receive : Nothing
; Return : nothing
;-----------------------------------------------------------------

TranslateBuffer ENDP

;-----------------------------------------------------------------
DisplayMessage PROC
;
; Display the encrypted or decrypted message.
; Receive : EDX pints to the message
; Returns : nothing
;----------------------------------------------------------------

DisplayMessage ENDP


END main

그럼 실제로 구현해보도록 하겠습니다.

;this program prompts the user for three integers
;store them it an array, calculates the su of the array, and display the sum


;-----------------INCLUDE-------------------------
;
include 	c:\assembly\irvine32.inc
includelib  c:\assembly\irvine32.lib
includelib  c:\assembly\kernel32.lib
includelib  c:\assembly\user32.lib
;-----------------------------------------------


.data
Key = 231
BUFMAX = 128

sPrompt BYTE "Enter the plain text :", 0
sEncrypt BYTE "Cipher text :     ", 0
sDecrypt BYTE "Decrypted :     ", 0
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
   

.code
main PROC

    call InputTheString
    call TranslateBuffer
    mov edx, OFFSET sEncrypt
    call DisplayMessage
    call TranslateBuffer
    mov edx, OFFSET sDecrypt
    call DisplayMessage
   
    exit
    
main ENDP

;-----------------------------------------------------------------
InputTheString PROC
; 
; Prompt user for a plaintext string, Save the string length to eax
; Receive : nothing
; Return : bufsize = string length
;----------------------------------------------------------------
    pushad
    mov edx, OFFSET sPrompt
    call WriteString
    mov ecx, BUFMAX
    mov edx, OFFSET buffer
    call ReadString
    mov bufsize, eax
    call Crlf
    popad
    ret

InputTheString ENDP

;-----------------------------------------------------------------
TranslateBuffer PROC
;
; Translates the string by Exlusive-ORing each
; Byte with the encryption key byte.
; Receive : Nothing
; Return : nothing
;-----------------------------------------------------------------
    pushad
    mov ecx, bufSize
    mov esi, 0
L1: xor buffer[esi], KEY
    inc esi
    loop L1
    popad
    ret

TranslateBuffer ENDP

;-----------------------------------------------------------------
DisplayMessage PROC
;
; Display the encrypted or decrypted message.
; Receive : EDX pints to the message
; Returns : nothing
;----------------------------------------------------------------
    pushad
    call WriteString
    mov edx, OFFSET buffer
    call WriteString
    call Crlf
    call crlf
    popad
    ret
DisplayMessage ENDP


END main

실제로 실행시켜보면 아래 결과와 같습니다.

 

이상으로 문자열 암호화 예제 프로그램을 마치도록 하겠습니다.

Comments