Question: X86 Assembly Language Encrypt and Decrypt code Help to implement the CipherChar Procedure at the end of the given code INCLUDE Irvine32.inc .data KeyPrompt BYTE

X86 Assembly Language Encrypt and Decrypt code

Help to implement the CipherChar Procedure at the end of the given code

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) 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) 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 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 PROC USES esi edi edx ecx mov edx, esi call WriteString mov edx, edi call ReadString ret GetString ENDP

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

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 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 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 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

; -------------------------------------------------- ; 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 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!