| |
|
| |
Get Paid for Your Tech Turorials / Tips
|
|
|
|
|
| Home > Computer Science > CS 01 |
| |
|
|
| Question : 2 (i) |
| |
Program in 8086 assembly language
(i) to convert an input file to a code(encrypted) form
(ii) to reconvert this encrypted file back to original |
| |
.model small
.data
;Messages for user
s0 db 0dh,0ah,'PROGRAM FOR FILE ENCRYPTION AND DECRYPTION ..By Kailas Jagtap$'
s1 db 0dh,0ah,'Do you want to ENCRYPT(E) or DECRYPT(D) a file ? : $'
s2 db 0dh,0ah,'Enter Name of File(to be encrypted): $'
s22 db 0dh,0ah,'Enter Name of File(which will store encrypted data): $'
s3 db 0dh,0ah,'Enter Name of encrypted File(to be decrypted): $'
s33 db 0dh,0ah,'Enter Name of File(which will store decrypted data): $'
s4 db 0dh,0ah,'***************** ERROR!!! - INVALID INPUT ****************** $'
s44 db 0dh,0ah,'***************** ERROR IN FILE OPERATION !!!!!! ************ $'
s5 db 0dh,0ah,'$'
s6 db 0dh,0ah,'.............. File ENCRYPTED Successfully ! ................. $'
s7 db 0dh,0ah,'.............. File DECRYPTED Successfully ! ................. $'
;buffer for file(to be encrypted/decrypted) name read from kb
fname db 80 ;storage for max.no.of char to read, say 80
db 0 ;storage for no.of char.actually read
db 80 dup(0) ;storage for string read from kb terminated by CR(0dh)
;buffer for file(encrypted/decrypted) name read from kb
newf1 db 80 ;storage for max.no.of char to read, say 80
db 0 ;storage for no.of char.actually read
db 80 dup(0) ;storage for string read from kb terminated by CR(0dh)
save_ax dw 1 dup(0) ;storage for count of no.of char. actually read
buffer db 512 dup(0) ;buffer for storing chars. read from file
endbuffer db '$' ;$ shows end of buffer
handle dw ? ;storage for handle of file(to be encrypted/decrypted)
newhandle dw ? ;storage for handle of file(encrypted/decrypted)
.code
start:
mov ax,@data ;initialise ds with data seg. addr.
mov ds,ax
call disp_nl ;move cursor to new line
mov ah,9 ;display initial msg.
lea dx,s0
int 21h
rpt_disp: call disp_nl ;move cursor to new line
call disp_nl ;move cursor to new line
call disp_nl ;move cursor to new line
mov ah,9 ;display encryp/decryp choice msg.
lea dx,s1
int 21h
mov ah,1 ;get E/D selection from kb(user)
int 21h
cmp al,'E' ;if E or e entered, excute encr.pgm
je encr
cmp al,'e'
je encr
cmp al,'D' ;if D or d entered, excute decr.pgm
je decr2
cmp al,'d'
je decr2
cmp al,'X' ;if X or x entered, then terminate the pgm
je stop2
cmp al,'x'
je stop2
;if any other char. entered then
call disp_nl ;go to next line on screen
call disp_nl ;go to next line on screen
mov ah,9 ;display invalid i/p msg.
lea dx,s4
int 21h
jmp rpt_disp ;and loop again for geting correct i/p char.
;
decr2: jmp decr1 ;used because jump is going out of range(-127 to 128)
stop2: jmp stop1 ; --"-- |
| |
| ---------------------open an existing file ------ |
encr: call disp_nl ;go to next line on screen
mov ah,9 ;display enter file(to be encr) name msg.
lea dx,s2
int 21h
lea dx,fname ;get start addr. of file(to be encr.)name storage
call getname ;get file name from kb
mov ah,3dh ;open existing file whose name is in fname
lea dx,fname+2 ;get file name string start addr.
mov al,0 ;ensure al=0, as it is used further
int 21h ;after opening, ax contains file handling info.
mov handle,ax ;save it in handle(buf)
jc err_out2 ;if carry set, go to display err msg. |
| |
| -----------------create a new file(for storing encrypted data)------ |
call disp_nl ;go to next line on screen
mov ah,9 ;display enter file(encr) name msg.
lea dx,s22
int 21h
lea dx,newf1 ;get start addr. of file(encr.)name storage
call getname ;get file name from kb
mov ah,3ch ;create new file whose name is in newf1
lea dx,newf1+2 ;get file name string start addr.
mov cx,0 ;ensure cx=0, as it is used further
int 21h ;after creating, ax contains file handling info.
mov newhandle,ax ;save it in newhandle(buf)
jc err_out ;if carry set, go to display err msg. |
| |
| ----------------------read the file & transfer the data to buffer------- |
readnext: lea dx,buffer ;get start addr.of buffer in which file data will be transfered
mov cx,512 ;no.of chars.to be read(at a time) is say 512
mov bx,handle ;get start addr.of file info. storage
mov ah,3fh ;get function no. for read
int 21h ;read 512 bytes from file
jc err_out ;if carry set, go to display err msg.
;otherwise read is successful,ax=no.of bytes read
;check for end of file
mov save_ax,ax ;save the count(no.of bytes read)
cmp ax,0 ;end of file? i.e. no.of bytes read is 0?
je done1 ;if yes, go to close both files and come out |
| |
| ------------------------convert the buffer in coded(encrypted) form----- |
| |
| call currupt ;encrypt the data in buf |
| |
| ------------------write buffer into the new file------------------ |
lea dx,buffer ;get start addr. of buf
mov ah,40h ;get function no. for write
mov bx,newhandle ;get sart addr.of file(encr) info buf
mov cx,save_ax ;get saved read count for writing the same no. of bytes
int 21h ;write
jc err_out ;if write is not successful, disp.err msg & come out
jmp readnext ;otherwise(successful), go to read next 512 bytes
;
err_out2: jmp err_out
decr1: jmp decr ;used because jump is going out of range(-127 to 128)
stop1: jmp stop ; ----"-----
;
done1: lea dx,s6 ;display encryp-successful msg.
jmp done3
done2: lea dx,s7 ;display decryp-successful msg.
done3: mov ah,9
int 21h ;display msg
jmp done ;go to close both files
;
stop: mov ah,4ch ;Hand over control to dos
int 21h
;
getname proc near
mov ah,0ah ;read file name from kb
int 21h ;after read,dx remains start addr.of file name buf
mov si,dx ;use si for indexing first 3 byte locations in buf
mov bl,[si+1] ;get no.of char.read count(2nd byte)
add si,02 ;point to 3rd byte,where actual name string starts
sub bh,bh ;clear bh, as used further
mov BYTE PTR[si+bx],0 ;replace cr(0dh) at end of string with 0
ret ;to make it useful to other functions
getname endp
;---------------------------------------------
err_out: mov ah,9 ;display err msg.
lea dx,s44
int 21h
jmp stop ;come out
;close both files
done: mov ah,3eh ;get function code for closing file
mov bx,handle ;get start addr. of file info.(1st file)
int 21h ;close 1st file
mov bx,newhandle ;get start addr. of file info.(2nd file)
int 21h ;close 2nd file
jmp stop ;come out
;
done4: jmp done2 ;used because jump is going out of range(-127 to 128)
;------------------------------------------------------------------------------
currupt proc near ;procedure for encr.(encoding) data
lea si,buffer ;get start addr.of buf containing bytes to be encrypted
mov cx,save_ax ;get no.of bytes to be encr.
again: mov al,[si] ;get a byte from buf
add al,07h ;encr. it
mov [si],al ;restore it at same location in buf
inc si ;point to next byte in buf
loop again ;reapeat until all bytes in buf are encr.
ret
currupt endp
;
err_out1: jmp err_out |
| |
| ------------------open an existing currupted file c:\masm51\f1cod.txt------ |
decr: call disp_nl ;go to next line on screen
mov ah,9 ;display enter file(to be decr) name msg.
lea dx,s3
int 21h
lea dx,fname ;read file(to be decr) name from kb
call getname ;further comments are same as that for encr.
mov ah,3dh
lea dx,fname+2
mov al,0
int 21h
mov handle,ax
jc err_out |
| |
| -----------------create a new file(for storing decrypted data) c:\masm61\f1dec.txt------ |
call disp_nl ;go to next line on screen
mov ah,9 ;display enter file(decr) name msg.
lea dx,s33
int 21h
lea dx,newf1 ;read file(encr) name from kb
call getname ;further comments are same as that for encr.
mov ah,3ch
lea dx,newf1+2
mov cx,0
int 21h
mov newhandle,ax
jc err_out |
| |
| --------------read the file & transfer the corrupted data to buffer------- |
readnext1: lea dx,buffer ;get start addr.of buffer in which file data will be transfered
mov cx,512 ;further comments are same as that for encr.
mov bx,handle
mov ah,3fh
int 21h
jc err_out1
;check for end of file
mov save_ax,ax ;save the count(no.of bytes read)
cmp ax,0 ;end of file? i.e. no.of bytes read is 0?
je done4 ;if yes, go to close both files and come out |
| |
| ------------------convert the buffer in decoded form----- |
| |
| call decurrupt ;get data decrypted |
| |
| ------------------write into the new file------------------ |
lea dx,buffer ;get start addr. of buf
mov ah,40h ;further comments are same as that for encr.
mov bx,newhandle
mov cx,save_ax
int 21h
jc err_out1 ;if write is not successful, disp.err msg & come out
jmp readnext1 ;otherwise(successful), go to read next 512 bytes
;-----------------------------------------------------------------------
decurrupt proc near ;procedure for decr.(decoding) data
lea si,buffer ;further comments are same as that for encr.
mov cx,512
again1: mov al,[si]
sub al,07h
mov [si],al
inc si
loop again1
ret
decurrupt endp
disp_nl PROC NEAR ;move cursor to new line on screen
mov ah,9 ;get function code for disp. string
lea dx,s5 ;get start addr. of new line msg string
int 21h ;display string(cr & lf)
ret
disp_nl ENDP
end start
end |
| |
|
|
|