;-----------------------------------------------------------
;Discription: TO encrypt data from a file.
;Author: Dan Lee, Sun Yat-sen University All rights reserved
;-----------------------------------------------------------
.model small, c
.386
Encrypt proto cell:ptr byte ; function protatype
count=30
keyboard struct
maxin byte count
incount byte ?
buf byte count dup(?)
keyboard ends
.data
BufSize = 500
kydbdata keyboard <>
prompt byte "Please input file name under current directory...",10,13,'$'
fmsg byte "file not found...",10,13,'$'
Handle WORD ?
buffer BYTE BufSize DUP(?)
bytesRead WORD ?
.code
.startup
again:
;Prompt to input file name
mov dx,offset Prompt
mov ah,9h
int 21h
;Get file name from cosole
mov ah,0Ah
mov dx,offset kydbdata
int 21h ; input file name
mov bl,kydbdata.incount
cmp bl,0
je quit ; if no input filename, then exit the program
mov di,offset kydbdata
mov al,kydbdata.incount
xor ah,ah
add di,ax
mov ax,2
add di,ax
mov byte ptr [di],0 ; To make the filename end with 0
; Open the input file
mov ax,716Ch ; extended create or open
mov bx,0 ; mode = read-only
mov cx,0 ; normal attribute
mov dx,1 ; action: open
mov si,OFFSET kydbdata.buf
int 21h ; call MS-DOS
jnc succ ; quit if error
mov dx,offset fmsg
mov ah,9h
int 21h
jmp again
succ:
mov Handle,ax
; Read the input file
mov ah,3Fh ; read file or device
mov bx,Handle ; file handle
mov cx,BufSize ; max bytes to read
mov dx,OFFSET buffer ; buffer pointer
int 21h
jc quit ; quit if error
mov bytesRead,ax
; Display the buffer
mov ah,40h ; write file or device
mov bx,1 ; console output handle
mov cx,bytesRead ; number of bytes
mov dx,OFFSET buffer ; buffer pointer
int 21h
jc quit ; quit if error
; Close the file
mov ah,3Eh ; function: close file
mov bx,Handle ; input file handle
int 21h ; call MS-DOS
jc quit ; quit if error
;Encrypttion procession
mov cx,bytesRead
mov di,OFFSET buffer
mark:
invoke Encrypt,di
Inc di
loop mark
call WriteSpace ; Write Spaces to seperate the output
call WriteSpace
call WriteSpace
; Display the buffer
mov ah,40h ; write file or device
mov bx,1 ; console output handle
mov cx,bytesRead ; number of bytes
mov dx,OFFSET buffer ; buffer pointer
int 21h
jc quit
; Reopen the input file for write
mov ax,716Ch ; extended create or open
mov bx,1 ; mode = write
mov cx,0 ; normal attribute
mov dx,1 ; action: open
mov si,OFFSET kydbdata.buf
int 21h ; call MS-DOS
jc quit ; quit if error
mov Handle,ax ; save handle
; Write buffer to new file
mov ah,40h ; write file or device
mov bx,Handle ; output file handle
mov cx,bytesRead ; number of bytes
mov dx,OFFSET buffer ; buffer pointer
int 21h
jc quit ; quit if error
; Close the file
mov ah,3Eh ; function: close file
mov bx,Handle ; output file handle
int 21h ; call MS-DOS
quit:
.exit 0
;-----------------------------------------------------------
;Discription:This procedure implement Encrypting data
; using the demand algorithm of the book
; design by Dan Lee
;Recives: one byte char of its address
;Return: explicitly nothing, but in fact a string of
; characters by passing by reference
;-----------------------------------------------------------
Encrypt proc cell:ptr byte ;passing by reference
push si
push ax
mov si,cell ; get address
mov al,[si] ; get element
.if (AL>='A')&& (AL<='Z') ; process
mov al,'Z'
sub al,[si]
add al,'A'
mov [si],al
.endif
.if (AL>='a')&& (AL<='z')
mov al,'z'
sub al,[si]
add al,'a'
mov [si],al
.endif
pop ax
pop si
ret
Encrypt endp
;----------------------------------------------------------
WriteSpace PROC
; Write a single space to standard output.
; This procedure is copied from Assembly Language for
; Intel-based Computers fourth edition by Kip R.Irvine
;----------------------------------------------------------
push ax
mov ah,2 ; function: display character
mov dl,20h ; 20h = space
int 21h
pop ax
ret
WriteSpace ENDP
end