0

まあ、ファイルを作成した後にファイルに書き込むプロセスを取得していないと思います。私はこれの初心者なので、この課題の助けは私にとって大きなものになるでしょう.

説明 (対称暗号化): 1. エンコーディング a. ユーザーにテキストを入力してもらいます。この範囲 [1-255] 内で秘密鍵を入力するようにユーザーに依頼します。範囲の有効性チェックを実行します。c. 提供された秘密鍵を使用して入力テキストを暗号化し、暗号テキストをユーザーが指定したファイルに入れます。2. デコード デコードするファイルを指定するようにユーザーに依頼します。b. そのファイルから暗号文をロードし、秘密鍵がエンコードに使用されたものと同じであると想定せずに復号化を試みます。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

うまくいかなかったと思いますか?さて、私は退屈だったので、コードをクリーンアップし、いくつか修正しました。私はそれを簡単に許しませんでしたが、コードのどこかに小さなバグを追加したため、正しいコード/出力ファイルを送信できません。

キップ、初心者向けの良い本を書いています。ただし、彼は MASM のすべての機能を使用していません。初心者にとってMASMの素晴らしいところはそのinvokeマクロです。呼び出された proc パラメータをチェックし、多くのエラーをキャッチします。

他の投稿では、呼び出しのパラメーターがありませんでしたか? さて、invoke with を使用した場合WriteToFile、MASM はそれをキャッチして通知します。しかし、彼はそれを示していないと思いますか?

とにかく、これを試してください:

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 に答える
0

あなたはやみくもに物事をまとめているように見えます。

Irvine には彼のすべての関数のソースが含まれており、どのパラメーターを使用するかについてのコメントが含まれています。 WriteToFile3 つのパラメータを取ります - eax== 書き込むファイル ハンドル == ファイルに edx書き込むデータを含むバッファへのポインタ == ファイルに ecx書き込むデータの長さ。

CreateOutputFileすでにファイルハンドルを返していますが、正しく 設定eaxして上書きしています。書き込むデータのサイズは、間違った場所に もあります。mov eax, fileHdledxReadStringeaxpopad

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 に答える