0

好吧,我想我在创建文件后没有得到写入文件的过程。我是这方面的初学者,所以这项任务的帮助对我来说是巨大的。

说明(对称加密): 1. 编码 a. 要求用户键入一些文本 b. 要求用户键入此范围 [1-255] 内的私钥。执行范围有效性检查。C。使用提供的私钥加密输入文本,将密文放在用户命名的文件中。2. 解码 要求用户指定要解码的文件。湾。从该文件加载密文并尝试对其进行解密,而不假设私钥与编码中使用的密钥相同。C。将所有试验结果放在一个由用户命名的单独文件中。d。找出最合理的结果(或原始明文)是什么。

INCLUDE Irvine32.inc
BUFMAX = 128                    ; maximum buffer size
KEYMAX = 128                    ; maximum buffer size
BUFFER_SIZE = 5000

.data
sPrompt BYTE        "Enter some text message:       ", 0
keyPrompt   BYTE        "Enter a private key [1-255]:       ", 0
cFile   BYTE        "Enter a filename for cypher text: ", 0
sEncrypt    BYTE        "Cypher text                    ", 0
sDecrypt    BYTE        "Decrypted:                 ", 0
error   BYTE        "The key must be within 1 - 255!    ", 0
buffer  BYTE         BUFMAX + 1 DUP(0)
bufSize DWORD    ?
keyStr  BYTE         KEYMAX + 1 DUP(0)
keySize DWORD    ?
key     DWORD    ?
filename    BYTE        "newfile.txt                    ", 0
fileHdl DWORD   ?
bufFile BYTE        BUFFER_SIZE DUP (?)
textMsg DWORD   ?

.code main PROC
call InputTheString             ; input the plain text
call InputTheKey                ; input the security key
call CypherFile             ; input a cypher filename
;call TranslateBuffer           ; encrypt the buffer
;mov edx, OFFSET sEncrypt           ; display encrypted message
;call DisplayMessage
;call TranslateBuffer           ; decrypt the buffer
;mov edx, OFFSET sDecrypt           ; display decrypted message
;call DisplayMessage
exit

main ENDP

InputTheKey PROC
pushad                      ; save 32-bit registers

LK: mov edx, OFFSET keyPrompt ; display a prompt call WriteString ; Enter a private key [1-255] call Crlf ; start a new line call ReadInt ; read int into system mov key, eax ; store int into keyStr cmp eax, 255 ; compare newly read int ja LC ; jump if above 255 to LC cmp eax, 1 ; compare newly read int jb LC ; jump if below 1 to LC jmp LR ; if between range jump to LR LC: mov edx, OFFSET error ; The key must be within 1 - 255! call WriteString ; Display the error call Crlf ; start a new line loop LK ; loop back to enter the security key LR: popad ; restore the registers ret InputTheKey ENDP

CypherFile PROC pushad mov edx, OFFSET cFile ; "Enter a filename for cypher text call WriteString ; Enter a name for encrypted file call Crlf ; Start a new line mov edx, OFFSET bufFile mov ecx, BUFMAX call ReadString ; Store the filename in eax mov edx, OFFSET bufFile call CreateOutputFile ;pop eax mov eax, fileHdl mov edx, OFFSET textMsg ;mov ecx, BUFFER_SIZE call WriteToFile popad call CloseFile ret
;mov filename, eax
;mov edx, OFFSET filename
;push eax
;mov eax, fileHdl
;mov edx, OFFSET bufFile
;mov ecx, BUFFER_SIZE
;mov edx, "C:\outputtext.txt"

;mov edx, OFFSET filename
;mov ecx, SIZEOF filename
;push eax
;mov eax, bufSize
;call WriteToFile

CypherFile ENDP

InputTheString PROC
pushad                      ; save 32-bit registers
mov edx, OFFSET sPrompt         ; display a prompt
call WriteString                ; "Enter some text message"
call Crlf                       ; start a new line
mov ecx, BUFMAX             ; maximum character count
mov edx, OFFSET buffer          ; point to the buffer
call ReadString             ; input the string
mov textMsg, eax
mov bufSize, eax                ; save the length
popad
ret

InputTheString ENDP

DisplayMessage PROC
pushad
call WriteString
mov edx, OFFSET buffer          ; display the buffer
call WriteString
call Crlf
call Crlf
popad
ret

DisplayMessage ENDP

TranslateBuffer PROC
pushad
mov ecx, bufSize                ; loop counter
mov esi, 0                  ; index 0 in buffer
mov edi, 0                  ; index 0 in the key

L1: mov al, keyStr[edi] ; get a character from encryption key xor buffer[esi], al ; translate a byte inc esi ; point to next byte inc edi ; go to next position in key cmp edi, keySize ; compare if equal to size of the key jb L2 mov edi, 0 ; reset to beginning of the key L2: loop L1 popad ret TranslateBuffer ENDP

END main
4

2 回答 2

0

看起来你只是盲目地把东西放在一起。

Irvine 包含了他所有函数的源代码,它们包括关于它们采用什么参数的注释。 WriteToFile接受 3 个参数 - eax== 要写入的文件句柄 edx== 指向包含要写入文件的数据的缓冲区的指针 == 要写入文件 ecx的数据长度。

CreateOutputFile已经返回了一个文件句柄,eax但是你正在正确mov eax, fileHdl 设置它edx,现在要写入的数据大小,ReadString返回这个eax 你也有popad错误的地方。

call    ReadString ; Store the filename in eax 
push    eax                 ; save string length

mov     edx, OFFSET bufFile 
call    CreateOutputFile 
mov     hFile, eax          ; save file handle
pop     ecx                 ; restore string length into ecx
mov     edx, OFFSET textMsg 
call    WriteToFile 

mov     eax, hFile          ; move hfile into ecx
call    CloseFile 
popad 
ret

看到不同?

你还有其他问题。你的密码不起作用。

于 2012-11-24T18:19:59.753 回答
0

猜猜你没有让它工作?好吧,我很无聊,所以清理了你的代码,并修复了一些东西。不过,我并没有那么容易让你离开,我在代码中的某个地方添加了一个小错误,因此你无法提交正确的代码/输出文件。

Kip,为初学者写了一本好书。他虽然没有使用 MASM 的全部功能。对于初学者来说,MASM 的伟大之处在于它的invoke宏。它会检查调用的 proc 参数并捕获许多错误。

在您的另一篇文章中,您缺少调用参数吗?好吧,如果您使用 invoke with WriteToFile,那么 MASM 会抓住并告诉您。但我猜他没有表现出来?

Anywhoo,试试这个:

INCLUDE d:\irvine32\Irvine32.inc
includelib d:\irvine32\irvine32.lib
includelib d:\irvine32\kernel32.lib
includelib d:\irvine32\user32.lib
BUFMAX = 128                                ; maximum buffer size
BUFFER_SIZE = 5000

.data
sPrompt     BYTE    "Enter some text message: ", 0
keyPrompt   BYTE    "Enter a private key [1-255]: ", 0
cFile       BYTE    "Enter a filename for cypher text: ", 0
sEncrypt    BYTE    "Cypher text: ", 0
sDecrypt    BYTE    "Decrypted: ", 0
error       BYTE    "The key must be within 1 - 255!", 0

.data?
bufSize     DWORD   ?
key         DWORD   ?
bufFile     BYTE    BUFFER_SIZE DUP (?)
buffer      BYTE    BUFMAX + 1 DUP (?)

.code 
main PROC
    call    InputTheString                  ; input the plain text
    call    InputTheKey                     ; input the security key

    call    TranslateBuffer                 ; encrypt the buffer

    mov     edx, offset sEncrypt
    call    WriteString

    mov     edx, OFFSET buffer              ; display encrypted message
    call    WriteString
    call    Crlf

    call    CypherFile 

    call    TranslateBuffer                 ; decrypt the buffer

    mov     edx, offset sDecrypt
    call    WriteString

    mov     edx, OFFSET buffer              ; display encrypted message
    call    WriteString
    call    Crlf

    call    WaitMsg
    exit

main ENDP

InputTheString PROC
    mov     edx, OFFSET sPrompt             ; display a prompt
    call    WriteString                     ; "Enter some text message"

    mov     ecx, BUFMAX                     ; maximum character count
    mov     edx, OFFSET buffer              ; point to the buffer
    call    ReadString                      ; input the string

    push    offset buffer
    call    Str_length
    mov     bufSize, eax                    ; save the length   
    ret
InputTheString ENDP

InputTheKey PROC

PromptForKey:
    mov     edx, OFFSET keyPrompt           ; display a prompt 
    call    WriteString                     ; Enter a private key [1-255] 
    call    ReadInt                         ; read int into system 
    test    eax, eax
    jz      BadKey
    cmp     eax, 255
    jg      BadKey

    mov     key, eax
    ret

BadKey:
    mov     edx, OFFSET error               ; The key must be within 1 - 255! 
    call    WriteString  
    call    Crlf
    jmp     PromptForKey
InputTheKey ENDP

CypherFile PROC 
    mov     edx, OFFSET cFile               ; "Enter a filename for cypher text 
    call    WriteString                     ; Enter a name for encrypted file 

    mov     edx, OFFSET bufFile 
    mov     ecx, BUFMAX 
    call    ReadString                      ; Store the filename in eax 

    mov     edx, OFFSET bufFile 
    call    CreateOutputFile 
    push    eax                             ; save file handle

    mov     ecx, bufSize      
    sub     ecx, 5         
    mov     edx, OFFSET buffer 
    call    WriteToFile 

    pop     eax                             ; restore file handle
    call    CloseFile 
    ret
CypherFile ENDP

TranslateBuffer PROC
    mov     ecx, bufSize
    mov     eax, key
    xor     esi, esi
XorByte:
    xor     buffer[esi], al
    inc     esi
    cmp     esi, ecx
    jne     XorByte
    ret
TranslateBuffer ENDP

DisplayMessage PROC

    mov     edx, OFFSET buffer          ; display the buffer
    call    WriteString
    call    Crlf
    call    Crlf
    ret

DisplayMessage ENDP
END main
于 2012-11-25T04:08:45.910 回答