Question: X86 ASSEMBLY LANGUAGE GIVEN THE FOLLOWING CODE IMPLEMNT THE CIPHER PROCEDURE INCLUDE Irvine32.inc .data KeyPrompt BYTE Enter the passphrase: ,0 TextPrompt BYTE Enter the plaintest:

X86 ASSEMBLY LANGUAGE

GIVEN THE FOLLOWING CODE IMPLEMNT THE CIPHER PROCEDURE

INCLUDE Irvine32.inc

.data

KeyPrompt BYTE "Enter the passphrase: ",0

TextPrompt BYTE "Enter the plaintest: ",0

str1 BYTE "The passphrase has length:",0

str2 BYTE "The plaintest has length:",0

KeyIs BYTE "The passphrase: ",0

PlainTextIs BYTE "The plaintext: ",0

CipherTextIs BYTE "The ciphertext: ",0

KMAX = 64 ; passphrase buffer maximum size

BMAX = 128 ; test buffer maximum size

KSize DWORD KMAX;

BSize DWORD BMAX;

Key BYTE KMAX DUP(0) ; buffer for passphrase

Buffer BYTE BMAX DUP(0) ; buffer for (en/de-cryption) text

.code

main PROC

; Get the passphrase from the user (put it in Key)

mov esi, OFFSET KeyPrompt

mov edi, OFFSET Key

mov ecx, KSize

call GetString

mov KSize, eax

; show it back to the user (debug/verification - this will be removed)

mov esi, OFFSET KeyIs

mov edi, OFFSET Key

call DisplayStrings

mov edx, OFFSET str1

call WriteString

call WriteDec

call CRLF

; Get the plaintext from the user (put it in Buffer)

mov esi, OFFSET TextPrompt

mov edi, OFFSET Buffer

mov ecx, BSize

call GetString

mov BSize, eax

; show it back to the user (debug/verification - this will be removed)

mov esi, OFFSET PlainTextIs

mov edi, OFFSET Buffer

call DisplayStrings

mov edx, OFFSET str2

call WriteString

call WriteDec

call CRLF

; Show how the plain/cipher-text will 'lineup' with the passphrase

; This will be removed It is just a sample of how we might deal

; with the buffers and indices

mov esi, OFFSET Buffer

mov edi, OFFSET Key

mov ecx, BSize

mov edx, KSize

call MatchUpBuffers ; puts address of a buffer with passpharse

call WriteString ; layout will line up the Text - show

; call Transform and then CipherChar

mov esi, OFFSET Buffer

mov edi, OFFSET Key

mov ecx, BSize

mov ebx, KSize

mov edx, 0 ; encrypt

call TransformBuffer

call CRLF

exit

main ENDP

; ---------------------------------------------------- -

;getString that

; recieves:

; esi - the assdress of a prompt (aks the user for something)

; edi - the address of the buffer where to put the string from the user

; ecx - the size of the buffer

; returns

; eax - number of char read

; have it restore the values of all register (except eax)

; ---------------------------------------------------- -

GetString PROC USES esi edi edx ecx

mov edx, esi

call WriteString

mov edx, edi

call ReadString

ret

GetString ENDP

; ---------------------------------------------------- -

; DisplayStrings that

; recieves

; esi - the assdress of a prompt or discription

; edi - the address the second string with some info

; returns: nothing

; it should printout the prompt, newline, 2nd string, and newline

; have it restore the values of all register (except eax)

; ---------------------------------------------------- -

DisplayStrings PROC USES eax

mov edx, esi

call WriteString

call CRLF

mov edx, edi

mov eax, 0

mov al, "'";

call WriteChar

call WriteString

call WriteChar

call CRLF

ret

DisplayStrings ENDP

; ---------------------------------------------------- -

; Match up passphrase with buffer -- for DEBUGGING

; Show witch char from the passphrase are going to be

; to use shift the plain/cipher-test

; fills out a bufer called shift with layout

;

; Receives: ESI - adress of buffer to encrypt/decypt

; EDI - address of keybuffer

; ECX - lengthof buffer

; EDX - lengthof PassphraseBuffer

; Returns: EAX - address of Shift buffer

; But changes buffer

; uses al, ecx, esi, edi,

; ---------------------------------------------------- -

MatchUpBuffers PROC

.data

msg BYTE "Plaintext and passphrase are going to lineup as follows:",13,10,0

shift BYTE LENGTHOF Buffer DUP(0)

.code

pushad ; save all regs

mov ebx, 0 ; index into key

mov eax, 0 ; zero out upper

push edx ; save - EDX holds lengthof passphrase

mov edx,OFFSET msg ; show message msg

call WriteString

pop edx ; restore lenght of buffer(need it latter)

push esi ; save esi (starting address of Buffer) and ecx (loop count)

push ecx

L2 :

mov al, [esi] ; move key char to register

call WriteChar ; show char from Buffer

inc esi

loop L2

pop ecx ; restore loop count (for next loop)

pop esi ; set esi to begining of Buffer

call CRLF

L1 :

mov al, [edi+ebx] ; copy passphrase char to register

call WriteChar

inc ebx

cmp ebx, edx ; check if index greater then length of key string

jb Skip

mov ebx, 0 ; if edi > keySize reset it to zero

Skip:

loop L1

call CRLF

popad ; restore all the register (see next line)

mov edx, OFFSET shift ; move address into EDX so client can call WriteString

ret

MatchUpBuffers ENDP

; ---------------------------------------------------- -

;

; TransformBuffer

; walks through the Text buffer matching up the characters

; from the Text buffer and Passphase buffer

; It will eventually call the 'cipher' procedure to

; en/de-crypt each charater in TEXT buffer (with appropriate

; Key char)

;

; It loop through the characters in text buffer (esi) and find the

; appropriate character is passphrase buffer (edi) and then call the

; cipher proc (hold off on this last step - until we have the

; 'cipher' proc

;

; Remenber the two buffers are not nessarrly the same size to

; it is very likey the second is much smaller so make sure the index

; of the second gets reset

;

;

; Receives:

; esi - address of the TEXT Buffer

; edi - address of the Passpharse buffer

; ecx - length of TEXT Buffer

; ebx - length of Passphrase

; edx - zero (0) if encrypt edx=0ffff????h if decrypt

; Uses:

; edx - index into PassPhrase

; eax - chars

; - al - buffer

; - ah - passphrase

; - bits 31-16 set to 1 if decryption (encryption otherwise)

; Returns: noting

; But changes buffer

; ---------------------------------------------------- -

TransformBuffer PROC

pushad ; save all the registes (could use USES)

mov eax, 0 ; zero chacters

mov eax, edx ; copy over upper 16 bits (en/de-crytion)

mov ax, 0 ; zero out lower part of eax

mov edx, 0 ; zero INDEX

L1 :

mov al, [esi] ; char from Buffer

mov ah, [edi+edx] ; char from Passphrase

call CipherChar ; transform char

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; currently things are not working correctly

;; you need to increament the indices

;; remember the passphrase index may need to be reset (edx=0)

;; to help make sure things are working

push ax ; save chars from Passphrase::Text

push bx ; save length passphrase

mov bl,ah ; copy passphrase char into bl

mov ah,0 ; overwrite ah (so WriteChar works correctly)

call WriteChar ; print char from Text

mov al, ":" ; lets make the output nic eand readable

call WriteChar

mov al,bl ; no print shift letter (from passphrase)

call WriteChar

call CRLF ; new line

pop bx ; restore length of passphrase

pop ax ; restore chars from Passphrase::Text

;; remove all this once we know things are working correctly

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

mov [esi], al ; overwrite orginal char with cipher version

inc eax ; increament point into BUFFER

inc edx ; increament the INDEX in to passphrase

cmp edx,ebx ; check if index greater then length of key string

jb Skip

mov edx,0 ; if edi > keySize reset it to zero

Skip: ; if still index < length we are okay

loop L1

popad ; restore all registers

ret

TransformBuffer ENDP

; Printable ASCII Character

; and values (upper ASCII values (32-126) - lower for shift (32-94)

;

; 111111111111111111111111111

;33333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222

;23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456

;

; !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

;^

;00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999

;01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234

;

; --------------------------------------------------

; CipherChar - Does range shift (32-126)

;

; EAX - VALUE ( char to en/de-crypt )

; ECX - SHIFT ( amount to shift either add or sub )

; if(VALUE < LOWER) return - nonprintable ascii skip

; if(VALUE > UPPER) return - out of range skip

; VALUE = VALUE - LOWER a space(32) becomes 0 (see above)

; SHIFT = SHIFT - LOWER

; VALUE = VALUE +/- SHIFT shift the char

; VALUE = VALUE % UPPER) make sure stays in range

; VALUE = VALUE + LOWER

;

; So takes an ascii value 0-127

; if its between 32 and 126 (inclusively) (printible ascii)

; the converts number to range 0 to 94 (inclusively)

; shift by passphrase value (add encrypt/ sub decrypt)

; if value now out of range move back into (modulo)

; adds back the 32 so back in orginal ASCII (32-126)

;

; Receives: EAX

; al - char to shift

; ah - amount to shift

; upper part of eax encrypte(0) or decrypt (F)

;

; Returns: transformed char in ax

; --------------------------------------------------

CipherChar PROC USES EBX ECX EDX ESI EDI

; just a stub for now - does nothing currently

; will add code soon

ret

CipherChar ENDP

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!