面向对象程序设计课程设计之加密与解密.docx
- 文档编号:18182480
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:18
- 大小:241.60KB
面向对象程序设计课程设计之加密与解密.docx
《面向对象程序设计课程设计之加密与解密.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计课程设计之加密与解密.docx(18页珍藏版)》请在冰点文库上搜索。
面向对象程序设计课程设计之加密与解密
软件学院
课程设计报告书
课程名称面向对象程序设计课程设计
设计题目加密与解密
专业班级软件09-4
学号
姓名
指导教师
2011年6月
1设计时间3
2设计目的3
3设计任务3
4设计内容3
4.1抽象数据类型的定义………………………………………………………………………………………………………..3
4.1.1程序所需要引入的包……………………………………………………………………………………………..3
4.1.2其他定义……………………………………………………………………………….....................................3
4.2程序流程……………………………………………………………………………………………………………………………….4
4.3模块间层次关系…………………………………………………………………………………………………………………..4
4.4详细设计…………………………………….……………………………………………………………………………………….5
4.4.1主函数代码…………………………………………………………………………………….……………………….5
4.4.2选择文件路径代码………………………………………………………………………..………………………5
4.4.3加密监听代码…………………………………………………………………………………………………………5
4.4.4解密监听代码…………………………………………………………………………………………………………6
4.4.5加密与解密代码…………………………………………………………………………………………………...6
4.5测试结果………………………………………………………………………………………………………………………………8
4.5.1选择文件路径结果………………………………………………………………………………………………..8
4.5.2文件加密结果………………………………………………………………………………………………………..9
4.5.3解密结果………………………………………………………………………………………………………………..10
4.6源代码…………………………………………………………………………………………………………………………………11
5总结与展望……………………………………………………………………………………………………………….…………….15
参考文献………………………………………………………………………………………………………………………………………..16
1设计时间
2011/6/7--2011/6/13
2设计目的
《面向对象程序设计》是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。
其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高学生的应用能力。
3设计任务
要求:
采用图形用户界面,建立菜单
(1)给定任意一个文本文件,进行加密,生成另一个文件。
(2)对加密后的文件还原。
4设计内容
4.1抽象数据类型的定义
4.1.1程序所需要引入的包
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;
4.1.2其他定义
newJPanel()
newJTextField()
newJButton()
newJTextArea()
newJPanel()
newActionListener()
4.2程序流程
图
是否
否
是
图1程序流程图
4.3模块间层次关系
图2层次(调用)关系图
4.4详细设计
4.4.1主函数代码
publicstaticvoidmain(Stringargs[]){
Swallowsw=newSwallow();
}
4.4.2选择文件路径代码
classmyjianting1implementsActionListener{
publicvoidactionPerformed(ActionEvente)
{
JFileChooserfc=newJFileChooser();//实例化文件选择器
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置文件选择模式,此处为文件和目录均可
if(fc.showOpenDialog(Swallow.this)==JFileChooser.APPROVE_OPTION){//弹出文件选择器,并判断是否点击了打开按钮
StringfileName=fc.getSelectedFile().getAbsolutePath();//得到选择文件或目录的绝对路径
tf1.setText(fileName);
}
}
}
4.4.3加密监听代码
classmyjianting2implementsActionListener{
publicvoidactionPerformed(ActionEventevent){
swallow1sw=newswallow1();
try{
sw.open();
sw.password();
sw.showFileContent();
}
catch(IOExceptione){
nr.setText("文件不能打开!
");
}
}
}
4.4.4解密监听代码
classmyjianting3implementsActionListener{
publicvoidactionPerformed(ActionEventevent){
swallow1sw=newswallow1();
try{
sw.open();
sw.password();
sw.showFileContent();
}
catch(IOExceptione){
nr.setText("文件解密有误!
");
}
}
}
4.4.5加密与解密代码
publicclassswallow1{
privateFilefile;
byte[]buf;
RandomAccessFileraf,raf1;
publicswallow1(){
file=newFile(tf1.getText());
buf=newbyte[(int)file.length()];
}
publicvoidopen()throwsFileNotFoundException{
raf=newRandomAccessFile(file,"rw");
raf1=newRandomAccessFile(“加密后的文件.txt”,"rw");
}
publicvoidpassword()throwsIOException{
raf.read(buf);
for(inti=0;i buf[i]=(byte)(~buf[i]); raf.seek(0); raf.write(buf); } publicvoidshowFileContent(){ Filefile=newFile(“加密后的文件.txt”); BufferedReaderreader=null; try{ reader=newBufferedReader(newFileReader(file)); Stringtemp=null; while((temp=reader.readLine())! =null){ nr.setText(temp); } reader.close(); } catch(IOExceptione){ //e.printStackTrace(); nr.setText("文件无法解密! "); } finally{ if(reader! =null){ try{ reader.close(); } catch(IOExceptione){} } } } } 4.5测试结果 4.5.1选择文件路径结果 图3运行程序 图4选择加密文件 图5选择加密文件 4.5.2文件加密结果 图6加密后的文件显示 4.5.3解密结果 图7解密后的文件显示 4.6源代码 importjava.awt.*; importjava.awt.event.*; importjava.io.*; importjavax.swing.*; publicclassSwallowextendsFrame{ JLabella1; JButtonbt1,bt2,bt3; JTextFieldtf1; JTextAreanr; publicstaticvoidmain(Stringargs[]){ newSwallow(); } publicSwallow(){ this.setTitle("Java加密和解密"); JPanelp1=newJPanel(); la1=newJLabel("选择文件路径"); tf1=newJTextField(20); bt1=newJButton("浏览..."); bt1.addActionListener(newmyjianting1()); p1.add(la1); p1.add(tf1); p1.add(bt1); add(p1,BorderLayout.NORTH); nr=newJTextArea(); add(nr,BorderLayout.CENTER); bt2=newJButton("加密"); bt2.addActionListener(newmyjianting2()); bt3=newJButton("解密"); bt3.addActionListener(newmyjianting3()); JPanelp2=newJPanel(); JPanelp3=newJPanel(); p3.add(bt2); p3.add(bt3); p2.add(p3); add(p2,BorderLayout.SOUTH); setSize(500,400); setLocation(200,200); setVisible(true); this.addWindowListener(newmyclosing()); } classmyclosingextendsWindowAdapter{ publicvoidwindowClosing(WindowEvente){ setVisible(false); System.exit(0); } } classmyjianting1implementsActionListener{ publicvoidactionPerformed(ActionEvente){ JFileChooserfc=newJFileChooser();//实例化文件选择器 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置文件选择模式,此处为文件和目录均可 if(fc.showOpenDialog(Swallow.this)==JFileChooser.APPROVE_OPTION){//弹出文件选择器,并判断是否点击了打开按钮 StringfileName=fc.getSelectedFile().getAbsolutePath();//得到选择文件或目录的绝对路径 tf1.setText(fileName); } } } classmyjianting2implementsActionListener{ publicvoidactionPerformed(ActionEventevent){ swallow1sw=newswallow1(); try{ sw.open(); sw.password(); sw.showFileContent(); } catch(IOExceptione){ nr.setText("文件不能打开! "); } } } classmyjianting3implementsActionListener{ publicvoidactionPerformed(ActionEventevent){ swallow1sw=newswallow1(); try{ sw.open(); sw.password(); sw.showFileContent(); } catch(IOExceptione){ nr.setText("文件解密有误! "); } } } publicclassswallow1{ privateFilefile; byte[]buf; RandomAccessFileraf,raf1; publicswallow1(){ file=newFile(tf1.getText()); buf=newbyte[(int)file.length()]; } publicvoidopen()throwsFileNotFoundException{ raf=newRandomAccessFile(file,"rw"); raf1=newRandomAccessFile(“加密后的文件.txt”,"rw"); } publicvoidpassword()throwsIOException{ raf.read(buf); for(inti=0;i buf[i]=(byte)(~buf[i]); raf.seek(0); raf1.write(buf); } publicvoidshowFileContent(){ Filefile=newFile(“加密后的文件.txt”); BufferedReaderreader=null; try{ reader=newBufferedReader(newFileReader(file)); Stringtemp=null; while((temp=reader.readLine())! =null){ nr.setText(temp); } reader.close(); } catch(IOExceptione){ //e.printStackTrace(); nr.setText("文件无法解密! "); } finally{ if(reader! =null){ try{ reader.close(); } catch(IOExceptione){} } } } } } 5总结与展望 通过此次课程设计,将我本学期所学的JAVA知识得到巩固和应用,在设计的过程中我遇到了很到问题,不过在老师和同学们的帮助和自己的思考下还是很好的完成了。 这此课程设计还让我懂得了写程序不能闭门造车,要努力拓宽知识面,开阔视野,拓展思维。 它还让我学会了在网上查阅那些无限的资源。 参考文献 [1]刘万军,梁清华,王松波,宋强等编著.Java程序设计.实践教程.北京: 清华大学出版社 [2](美)BruceEckel著.陈昊鹏译.Java编程思想.第4版.北京: 机械工业出版社 [3]张帆等编著.Java范例开发大全.北京: 清华大学出版社 [4]XX: [5]XX文库: 成绩评定 成绩教师签字
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 程序设计 课程设计 加密 解密
![提示](https://static.bingdoc.com/images/bang_tan.gif)