1004144588面向对象程序设计课程设计加密与解密.doc

上传人:仙人指路1688 文档编号:2385600 上传时间:2023-02-17 格式:DOC 页数:17 大小:239KB
返回 下载 相关 举报
1004144588面向对象程序设计课程设计加密与解密.doc_第1页
第1页 / 共17页
1004144588面向对象程序设计课程设计加密与解密.doc_第2页
第2页 / 共17页
1004144588面向对象程序设计课程设计加密与解密.doc_第3页
第3页 / 共17页
1004144588面向对象程序设计课程设计加密与解密.doc_第4页
第4页 / 共17页
1004144588面向对象程序设计课程设计加密与解密.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《1004144588面向对象程序设计课程设计加密与解密.doc》由会员分享,可在线阅读,更多相关《1004144588面向对象程序设计课程设计加密与解密.doc(17页珍藏版)》请在三一办公上搜索。

1、软 件 学 院课程设计报告书课程名称 面向对象程序设计课程设计 设计题目 加密与解密 专业班级 软件09-4 学 号 姓 名 指导教师 2011 年 6 月1 设计时间 32 设计目的 33设计任务 34 设计内容 3 4.1 抽象数据类型的定义 .34.1.1程序所需要引入的包 .34.1.2 其他定义 .34.2程序流程 .44.3模块间层次关系 .44.4 详细设计 .54.4.1 主函数代码 .54.4.2 选择文件路径代码 .54.4.3 加密监听代码 54.4.4 解密监听代码 64.4.5 加密与解密代码 . .64.5 测试结果 84.5.1 选择文件路径结果 .84.5.2

2、文件加密结果 .94.5.3 解密结果 .104.6 源代码 115 总结与展望 .15参考文献 .161 设计时间2011/6/7-2011/6/132 设计目的面向对象程序设计是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高学生的应用能力。3设计任务要求:采用图形用户界面,建立菜单(1)给定任意一个文本文件,进行加密,生成另一个文件。(2)对加密后的文件还原。4 设计内容

3、 4.1 抽象数据类型的定义4.1.1程序所需要引入的包import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;4.1.2其他定义new JPanel()new JTextField()new JButton( )new JTextArea()new JPanel()new ActionListener()4.2程序流程开始图弹出文件加密器对话框选择要加密的文件加密或解密是否获得文件路径是否文件是否已加密解密 否加密生成机密文件是生成解密文件结束 图1 程序流程图文件加密器、解密模块4.3模

4、块间层次关系文件选择模块解密模块加密模块解密操作模块加密操作模块图2层次(调用)关系图4.4详细设计4.4.1 主函数代码public static void main(String args)Swallow sw = new Swallow();4.4.2 选择文件路径代码class myjianting1 implements ActionListenerpublic void actionPerformed(ActionEvent e)JFileChooser fc = new JFileChooser(); / 实例化文件选择器fc.setFileSelectionMode(JFile

5、Chooser.FILES_AND_DIRECTORIES); / 设置文件选择模式,此处为文件和目录均可if (fc.showOpenDialog(Swallow.this) = JFileChooser.APPROVE_OPTION) / 弹出文件选择器,并判断是否点击了打开按钮String fileName = fc.getSelectedFile().getAbsolutePath(); / 得到选择文件或目录的绝对路径tf1.setText(fileName);4.4.3 加密监听代码class myjianting2 implements ActionListenerpublic

6、void actionPerformed(ActionEvent event)swallow1 sw = new swallow1();trysw.open(); sw.password();sw.showFileContent();catch(IOException e)nr.setText(文件不能打开!);4.4.4 解密监听代码class myjianting3 implements ActionListenerpublic void actionPerformed(ActionEvent event)swallow1 sw = new swallow1();try sw.open()

7、; sw.password();sw.showFileContent();catch (IOException e)nr.setText(文件解密有误!);4.4.5 加密与解密代码public class swallow1private File file;byte buf;RandomAccessFile raf,raf1;public swallow1()file = new File(tf1.getText();buf = new byte(int)file.length();public void open() throws FileNotFoundExceptionraf = ne

8、w RandomAccessFile(file, rw); raf1 = new RandomAccessFile(“加密后的文件.txt”, rw);public void password() throws IOExceptionraf.read(buf);for (int i = 0; i buf.length; i+)bufi = (byte)(bufi);raf.seek(0);raf.write(buf);public void showFileContent()File file = new File(“加密后的文件.txt”);BufferedReader reader = n

9、ull;tryreader = new BufferedReader(new FileReader(file);String temp = null;while (temp = reader.readLine() != null)nr.setText(temp);reader.close();catch (IOException e)/e.printStackTrace();nr.setText(文件无法解密!);finallyif (reader != null)tryreader.close();catch (IOException e) 4.5测试结果4.5.1 选择文件路径结果图3运行

10、程序图4选择加密文件图5 选择加密文件4.5.2 文件加密结果图6 加密后的文件显示4.5.3 解密结果图7 解密后的文件显示4.6 源代码import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class Swallow extends FrameJLabel la1;JButton bt1, bt2, bt3;JTextField tf1;JTextArea nr;public static void main(String args)new Swallow();publi

11、c Swallow()this.setTitle(Java加密和解密);JPanel p1 = new JPanel();la1 = new JLabel(选择文件路径);tf1 = new JTextField(20);bt1 = new JButton(浏览.);bt1.addActionListener(new myjianting1();p1.add(la1);p1.add(tf1);p1.add(bt1);add(p1, BorderLayout.NORTH);nr = new JTextArea();add(nr, BorderLayout.CENTER);bt2 = new JB

12、utton(加密);bt2.addActionListener(new myjianting2();bt3 = new JButton(解密);bt3.addActionListener(new myjianting3();JPanel p2 = new JPanel();JPanel p3 = new JPanel();p3.add(bt2);p3.add(bt3);p2.add(p3);add(p2, BorderLayout.SOUTH);setSize(500, 400);setLocation(200, 200);setVisible(true);this.addWindowList

13、ener(new myclosing();class myclosing extends WindowAdapterpublic void windowClosing(WindowEvent e)setVisible(false);System.exit(0);class myjianting1 implements ActionListenerpublic void actionPerformed(ActionEvent e)JFileChooser fc = new JFileChooser(); / 实例化文件选择器fc.setFileSelectionMode(JFileChooser

14、.FILES_AND_DIRECTORIES); / 设置文件选择模式,此处为文件和目录均可if (fc.showOpenDialog(Swallow.this) = JFileChooser.APPROVE_OPTION) / 弹出文件选择器,并判断是否点击了打开按钮String fileName = fc.getSelectedFile().getAbsolutePath(); / 得到选择文件或目录的绝对路径tf1.setText(fileName);class myjianting2 implements ActionListenerpublic void actionPerforme

15、d(ActionEvent event)swallow1 sw = new swallow1();trysw.open(); sw.password();sw.showFileContent();catch(IOException e)nr.setText(文件不能打开!);class myjianting3 implements ActionListenerpublic void actionPerformed(ActionEvent event)swallow1 sw = new swallow1();trysw.open(); sw.password();sw.showFileConte

16、nt();catch (IOException e)nr.setText(文件解密有误!);public class swallow1private File file;byte buf;RandomAccessFile raf,raf1;public swallow1()file = new File(tf1.getText();buf = new byte(int)file.length();public void open() throws FileNotFoundExceptionraf = new RandomAccessFile(file, rw); raf1 = new Rand

17、omAccessFile(“加密后的文件.txt”, rw);public void password() throws IOExceptionraf.read(buf);for (int i = 0; i buf.length; i+)bufi = (byte)(bufi);raf.seek(0);raf1.write(buf);public void showFileContent()File file = new File(“加密后的文件.txt”);BufferedReader reader = null;tryreader = new BufferedReader(new FileR

18、eader(file);String temp = null;while (temp = reader.readLine() != null)nr.setText(temp);reader.close();catch (IOException e)/e.printStackTrace();nr.setText(文件无法解密!);finallyif (reader != null)tryreader.close();catch (IOException e) 5 总结与展望通过此次课程设计,将我本学期所学的JAVA知识得到巩固和应用,在设计的过程中我遇到了很到问题,不过在老师和同学们的帮助和自己的思考下还是很好的完成了。这此课程设计还让我懂得了写程序不能闭门造车,要努力拓宽知识面,开阔视野,拓展思维。它还让我学会了在网上查阅那些无限的资源。参考文献1 刘万军,梁清华,王松波,宋强 等编著.Java 程序设计.实践教程.北京:清华大学出版社2 (美) Bruce Eckel 著.陈昊鹏 译.Java编程思想.第4版.北京:机械工业出版社3 张帆 等编著.Java 范例开发大全.北京:清华大学出版社4 百度: 5 百度文库: 成绩评定成绩 教师签字

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号