汇编语言简易加密解密文件的实现

来源:互联网 发布:以网络为话题的作文 编辑:程序博客网 时间:2024/04/29 16:34

汇编语言简易加密解密软件实现思路

加密思路:

1.从键盘上输入待加密的文件的路径
2.将待加密的文件的内容读取至程序定义好的缓冲区中
3.从键盘上输入加密密钥
4.将根据加密算法和加密密钥修改缓冲区的内容
5.从键盘上输入加密后的文件路径
6.将缓冲区的文件内容写入文件
至此,加密就结束啦

解密思路:

1.从键盘上输入待解密的文件的路径
2.将待解密的文件的内容读取至程序定义好的缓冲区中
3.从键盘上输入解密密钥
4.将根据解密算法和解密密钥修改缓冲区的内容
5.从键盘上输入解密后的文件路径
6.将缓冲区的文件内容写入文件
其实,解密文件就是加密文件的逆过程

注意问题:
1.十号功能从键盘读取文件名时,需要注意处理缓冲区中的回车键的ascall码值,需将0dh处理为00h,否则可能会提示文件问找到
2.在定义类型时,也需要注意,比如将一个十六位的数赋给一个8位的就有可能使程序出错,即在我的程序中,我将打开文件成功后的保存在ax中文件代号赋值给一个db类型的地址空间时,在解密以后,就将第一个字符覆盖了

程序截图





源代码

data    segment                                                             

  2         fileKey db 2,?,2 dup(?) ;存储密钥

  3         fileHandle db 2 dup(?) ;存储文件代号

  4         filePath db 20,?,20 dup(?),00 ;存储文件路径

  5         newFilePath db 10,?,10 dup(?),00 ;存储加密或者解密的文件的路径

  6         fileBuffer  db 10000 dup(?) ;定义缓冲区

  7         openErrorTip db 'open file error !!$';提示打开文件失败

  8         readErrorTip db 'read file error !!$' ;提示读文件失败

  9         encryptErrorTip db 'encrypt file error !!$' ;提示加密文件失败

 10         encryptTrueTip db 'encrypt file true !!$';提示加密文件成功

 11         decryptErrorTip db 'decrypt file error!!$';提示解密文件失败

 12         decryptTrueTip db 'decrypt file true!!$';提示解密文件成功

 13         choiceTip db 'please input choise:$';提示功能选择

 14         choiceErrorTip db 'your choice is false ,choice again!!$'

 15         fileInputTip db 'please input source file path:$';提示源文件路径

 16         newFileInputTip db 'please input new file path:$';                      提示加密或者解密后的文件的路径

 17         keyTip db 'please input Key:$' ;提示输入密钥

 18         menu1 db '1.decrypt file$';菜单1,解密文件内容

 19         menu2 db '2.encrypt file$';菜单2,加密文件内容

 20         menu3 db '3.exit$';退出本程序

 21         divide db '************************$';分割符

 22         choice db 2,?,2 dup(?) ;存储选择

 23         fileCount db  ? ;定义实际读取的字节数

 24 data    ends                                                                

 25 code    segment

 26         assume ds:data,cs:code

 27 main:

 28         ;展示菜单

 29         call displayMenu

 30         ;选择功能号

 31         call getChoice

 32         ;返回dos

 33         call returnDos

 34 ;***************

 35 ;展示本软件功能

 36 displayMenu proc near

 37         mov ax,data

 38         mov ds,ax

 39         ;9号功能调用,显示分隔符

 40         mov dx,offset divide

 41         mov ah,9

42         int 21h

 43         ;换行处理

 44         call nextLine

 45         ;9号功能调用,显示菜单1

 46         mov dx,offset menu1

 47         mov ah,9

 48         int 21h

 49         ;换行处理

 50         call nextLine

 51         ;9号功能调用,显示菜单2

 52         mov dx,offset menu2

 53         mov ah,9

 54         int 21h

 55         ;换行处理

 56         call nextLine

 57         ;显示菜单3

 58         mov dx,offset menu3

 59         mov ah,9                                                            

 60         int 21h

 61         ;换行处理

 62         call nextLine

63         ;显示分隔符

 64         mov dx,offset divide

 65         mov ah,9                                                            

 66         int 21h

 67         ;换行处理

 68         call nextLine

 69         ret

 70 displayMenu endp

 71 ;***************

 72  

 73 ;***************

 74 ;记录选择

 75 getChoice proc near

 76         ;9号功能调用,提示选择

 77         mov dx,offset choiceTip

 78         mov ah,9

 79         int 21h

 80         ;10号功能调用,将选择的序号保存在choice中

 81         mov dx,offset choice

 82         mov ah,10

83         int 21h

 84         ;换行处理

 85         call nextLine

 86         ;取得choice缓冲区中实际存储的ascall码,即选择的数

 87         lea bx,choice+2

 88         ;清空ax

 89         xor ax,ax

 90         ; 将choice缓冲区中的值送给al

 91         mov al,[bx]

 92         ;比较choice是不是和1相等

 93         cmp al,31h

 94         ;相等,则跳转getChoice1

 95         je getChoice1

 96         ; 不相等,则判断是不是和2相等

 97         cmp al,32h

 98         ;相等,则跳转getChoice2

 99         je getChoice2

100         ;不相等,则判断是不是和3相等                                        

101         cmp al,33h

102         ;相等,则跳转getChoice3

103         je getChoice3

104         ;不相等,则跳转choiceError,提示选择错误,并重新选择

105         jmp choiceError

106 getChoice1:

107         ; 调用解密

108         call decrypt                                                        

109         ret

110 getChoice2:

111         ;调用加密

112         call encrypt

113         ret

114 getChoice3:

115         ;返回dos,退出程序

116         call returnDos

117         ret

118 choiceError:

119         ;9号功能调用,提示选择错误

120         mov dx,offset choiceErrorTip

121         mov ah,9

122         int 21h

123         ;换行处理

124         call nextLine

125         ;重新调用getChoice, 重新选择

126         call getChoice

127         ret

128 getChoice endp

129 ;***************

130 ;***************

131 ;创建文件,cf=0,返回文件代号,保存在ax中;cf=1,创建失败

132 createFile proc near

133 create:

134         ;9号功能调用,提示输入新的文件名加路径

135         mov dx,offset newFileInputTip

136         mov ah,9

137         int 21h

138         ;10号功能调用,将新文件名艺伎路径保存在newFilePath中

139         mov dx,offset newFilePath                                           

140         mov ah,10

141         int 21h

142         ;换行处理

143         call nextLine

144         ;取得newFilePath缓冲区中实际输入的字符的个数

145         lea bx,newFilePath+1

146         ;清空cx

147         xor cx,cx

148         ;将newFilePath缓冲区实际存储的个数送给cx

149         mov cl,[bx]

150         ;判断是否输入

151         cmp cx,00h

152         ;如果输入的不为空,则跳转continue

153         jnz continue

154         ;如果输入的为空,则重新输入

155         jmp create

156 continue:

157         ;取得实际的存储路径

158         lea bx,newFilePath+2

159         ;处理enter的ascall码

160         call findEnter                                                      

161         ;系统调用,创建文件

162         mov ah,3ch

163         mov cx,00

164         lea dx,newFilePath+2

165         int 21h

166         ; 将文件代号保存在fileHandle中

167         lea si,fileHandle

168         mov [si],ax

169         ret

170 createFile endp

171 ;***************

172  

173 ;***************

174 ;打开文件,Cf=0,返回文件代号,保存在ax中;cf=1,打开失败

175 openFile proc near

176         ;9号功能调用,提示输入文件名

177         mov dx,offset fileInputTip

178         mov ah,9

179         int 21h

180         ;10号功能调用,将文件名存储在filePath中

181         mov dx,offset filePath                                              

182         mov ah,10

183         int 21h

184         ;换行处理

185         call nextLine

186         ;取得键盘上输入的实际的个数

187         lea bx,filePath+1

188         ; 清空cx

189         xor cx,cx

190         ;将键盘上实际输入的个数送给cx

191         mov cl,[bx]

192         ;取得文件实际的存储路径

193         lea bx,filePath+2

194         ;处理enter的ascall码

195         call findEnter

196         ;系统调用,打开文件

197         mov ah,3dh

198         mov al,00

199         lea dx,filePath+2

200         int 21h

201         ; 若CF为1,打开失败

202         jc  openError                                                       

203         ;若CF为0,打开成功,获取返回的ax中的文件代号

204         jmp getFileHandle

205 getFileHandle:

206         ;取得fileHandle的地址空间

207         lea bx,fileHandle

208         ;将文件代号保存在fileHandle中

209         mov [bx],ax

210         ret

211 openError:

212         ;9号功能调用,提示打开文件失败

213         mov dx,offset openErrorTip

214         mov ah,9

215         int 21h

216         ;返回dos

217         call returnDos

218         ret

219 openFile endp

220 ;***************

221  

222 ;***************

223 ;读文件                                                                     

224 readFile proc near

225         ;系统调用,读文件

226         mov ah,3fh

227         lea si,fileHandle

228         mov bx,[si]

229         mov cx,10000

230         lea dx,fileBuffer

231         int 21h

232         ;cf=1,读文件失败,cf=0,读文件成功

233         ;提示读取文件失败

234         jc readError

235         jmp getFileCount

236 getFileCount:

237         ;将实际读取的字节数放在fileCount中

238         lea bx,fileCount

239         mov [bx],ax

240         ret

241 readError:

242         ;9号功能调用,提示读文件失败

243         mov dx,offset readErrorTip

244         mov ah,9                                                            

245         int 21h

246         ;返回dos

247         call returnDos

248         ret

249 readFile endp

250 ;***************

251  

252 ;***************

253 ;向文件写数据

254 writeFile proc near

255         ;系统调用,写文件

256         mov ah,40h

257         lea si,fileHandle

258         mov bx,[si]

259         lea di,fileCount

260         mov cx,[di]

261         lea dx,fileBuffer

262         int 21h

263         ;cf=1,写文件错误

264         jc writeError

265         jmp writeTrue                                                       

266 writeTrue:

267         ;提示写文件成功

268         lea bx,choice+2

269         xor ax,ax

270         mov al,[bx]

271         cmp ax,31h

272         je  decryptTrue

273         jmp encryptTrue

274 decryptTrue:

275         ;解密成功

276         mov dx,offset decryptTrueTip

277         mov ah,9

278         int 21h

279         ret

280 encryptTrue:

281         ;加密成功

282         mov dx,offset encryptTrueTip

283         mov ah,9

284         int 21h

285         ret

286 writeError:

287 decryptFalse:

288         ;解密失败

289         mov dx,offset decryptErrorTip

290         mov ah,9

291         int 21h

292         ret

293 encryptFalse:

294         ;加密失败

295         mov dx,offset encryptErrorTip

296         mov ah,9

297         int 21h                                                                                                               

298         ret

299 writeFile endp

300 ;***************

301  

302 ;***************

303 ;关闭文件

304 closeFile proc near

305         mov ah,3eh

306         int 21h

307         ret

308 closeFile endp

309 ;***************

310  

311 ;***************

312 ;返回dos

313 returnDos proc near

314         mov ah,4ch

315         int 21h

316         ret

317 returnDos endp

318 ;***************                                                            

319  

320 ;***************

321 ;换行处理

322 nextLine proc near

323         mov dl,0dh

324         mov ah,2

325         int 21h

326         mov dl,0ah

327         mov ah,2

328         int 21h

329         ret

330 nextLine endp

331 ;***************

332  

333 ;***************

334 ;加密文件内容

335 encrypt proc near

336         call openFile

337         call readFile

338         call closeFile

339         ;清cx                                                               

340         xor cx,cx

341         ;将实际读取的子节数送至cx

342         lea si,fileCount

343         mov cx,[si]

344         lea bx,fileBuffer

345         call saveKey

346         ;加密文件内容

347 do:

348         mov dl,[bx]

349         mov di,offset fileKey+2

350         mov al,[di]

351         add al,dl

352         mov [bx],al

353         inc bx

354         loop do

355         call createFile

356         call writeFile

357         call closeFile

358         ret

359 encrypt endp

360 ;***************                                                            

361  

362 ;***************

363 ;解密文件内容

364 decrypt proc near

365         ; 打开文件

366         call openFile

367         ;读文件

368         call readFile

369         ;关闭文件

370         call closeFile

371         ;清cx

372         xor cx,cx

373         ;将文件实际的字节数送至cx

374         lea si,fileCount

375         mov cx,[si]

376         ;取缓冲区的地址

377         lea bx,fileBuffer

378         ;保存密钥

379         call saveKey

380 undo:

381         ; 解密文件内容                                                      

382         mov al,[bx]

383         mov di,offset fileKey+2

384         mov dl,[di]

385         sub al,dl

386         mov [bx],al

387         inc bx

388         loop undo

389         call createFile

390         call writeFile

391         call closeFile

392         ret

393 decrypt endp

394 ;***************

395  

396 ;***************

397 ;将回车所占的地址空间的值设为00h

398 findEnter proc near

399 next1:

400         inc bx

401         loop next1

402         mov al,00h                                                          

403         mov [bx],al

404         ret

405 findEnter endp

406 ;***************

407  

408 ;***************

409 ;提示输入密钥,并保存在fileKey中

410 saveKey proc near

411         ;9号功能调用,提示输入密钥

412         mov dx,offset keyTip

413         mov ah,9

414         int 21h

415         ;10号功能调用,将密钥保存在fileKey中

416         mov dx,offset fileKey

417         mov ah,10

418         int 21h

419         call nextLine

420         ret

421 saveKey endp

422 ;***************

423 code    ends

424     end     main    






0 0
原创粉丝点击