JA V A 程序设计报告----------------------------------------------------------------------------------------------------------------------------------------加密与解密辅导老师:冯艳君学号:1103050403姓名:卢雨情目录一课程设计总要求。
3 二题目及要求。
3三程序设计思路。
3 四程序代码。
10五总结。
17一课程设计总要求本次课程设计是对前面学过的所有面向对象的编程思想以及编程方法的一个总结、回顾和实践,因此,开始设计前学生一定要先回顾以前所学的内容,明确本次作业设计所要用到的技术点并到网上搜索以及查阅相关的书籍来搜集资料。
通过编写一个基于JA V A的应用系统综合实例,来掌握Java语言编程技巧。
二题目及要求加密与解密要求:采用图形用户界面1、给定任意一个文本文件,进行加密,生成另一个文件。
2、对加密后的文件还原。
三程序设计思路1 This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method isalways regenerated by the Form Editor.代码:public class FileEncrypterT extends javax.swing.JFrame {public static final int WIDTH = 550;public static final int HEIGHT = 200;2 加密函数输入:要加密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如: AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746其中:AD67EA2F3BE6E5AD DES密码一D368DFE03120B5DF DES密码二92A8FD8FEC2F0746 DES密码三输出:对输入的文件加密后,保存到同一文件夹下增加了".tdes"扩展名的文件中。
代码:public static void main(String args[]) {FileEncrypter fe = new FileEncrypter();fe.show();}FileEncrypterT(){this.setSize(WIDTH,HEIGHT);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);Toolkit tk = Toolkit.getDefaultToolkit();Dimension screenSize = tk.getScreenSize();this.setLocation((screenSize.width - WIDTH)/2,(screenSize.height - HEIGHT)/2);this.setTitle("文件加密器(TriDES)");Container c = this.getContentPane();c.setLayout( new FlowLayout());final FilePanel fp = new FilePanel("文件选择");c.add(fp);final KeyPanel pp = new KeyPanel("密码");c.add(pp);JButton jbE = new JButton("加密");c.add(jbE);jbE.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){File file = new File(fp.getFileName());if (file.exists())encrypt(file.getAbsoluteFile(),pp.getKey());elseJOptionPane.showMessageDialog(null,"请选择文件!","提示",JOptionPane.OK_OPTION); }});JButton jbD = new JButton("解密");c.add(jbD);jbD.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){File file = new File(fp.getFileName());if (file.exists())decrypt(file.getAbsoluteFile(),pp.getKey());elseJOptionPane.showMessageDialog(null,"请选择文件!","提示",JOptionPane.OK_OPTION); }});}private void encrypt(File fileIn,String sKey){try{if(sKey.length() == 48){byte[] bytK1 = getKeyByStr(sKey.substring(0,16));byte[] bytK2 = getKeyByStr(sKey.substring(16,32));byte[] bytK3 = getKeyByStr(sKey.substring(32,48));FileInputStream fis = new FileInputStream(fileIn);byte[] bytIn = new byte[(int)fileIn.length()];for(int i = 0;i<fileIn.length();i++){bytIn[i] = (byte)fis.read();}//加密byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(bytIn,bytK1),bytK2),bytK3);String fileOut = fileIn.getPath() + ".tdes";FileOutputStream fos = new FileOutputStream(fileOut);for(int i = 0;i<bytOut.length;i++){fos.write((int)bytOut[i]);}fos.close();JOptionPane.showMessageDialog(this,"加密成功!","提示",JOptionPane.OK_OPTION);}elseJOptionPane.showMessageDialog(this,"密码长度必须等于48!","错误信息",JOptionPane.ERROR_MESSAGE);}catch(Exception e){e.printStackTrace();}}3 解密函数输入:要解密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746其中:AD67EA2F3BE6E5AD DES密码一D368DFE03120B5DF DES密码二92A8FD8FEC2F0746 DES密码三输出:对输入的文件解密后,保存到用户指定的文件中。
代码:private void decrypt(File fileIn,String sKey){try{if(sKey.length() == 48){String strPath = fileIn.getPath();if(strPath.substring(strPath.length()-5).toLowerCase().equals(".tdes"))strPath = strPath.substring(0,strPath.length()-5);else{JOptionPane.showMessageDialog(this,"不是合法的加密文件!","提示",JOptionPane.OK_OPTION);return;}JFileChooser chooser = new JFileChooser();chooser.setCurrentDirectory(new File("."));chooser.setSelectedFile(new File(strPath));//用户指定要保存的文件int ret = chooser.showSaveDialog(this);if(ret==JFileChooser.APPROVE_OPTION){byte[] bytK1 = getKeyByStr(sKey.substring(0,16));byte[] bytK2 = getKeyByStr(sKey.substring(16,32));byte[] bytK3 = getKeyByStr(sKey.substring(32,48));FileInputStream fis = new FileInputStream(fileIn);byte[] bytIn = new byte[(int)fileIn.length()];for(int i = 0;i<fileIn.length();i++){bytIn[i] = (byte)fis.read();}//解密byte[] bytOut = decryptByDES(decryptByDES(decryptByDES(bytIn,bytK3),bytK2),bytK1);File fileOut = chooser.getSelectedFile();fileOut.createNewFile();FileOutputStream fos = new FileOutputStream(fileOut);for(int i = 0;i<bytOut.length;i++){fos.write((int)bytOut[i]);}fos.close();JOptionPane.showMessageDialog(this,"解密成功!","提示",JOptionPane.OK_OPTION);}}elseJOptionPane.showMessageDialog(this,"密码长度必须等于48!","错误信息",JOptionPane.ERROR_MESSAGE);}catch(Exception e){JOptionPane.showMessageDialog(this,"解密失败,请核对密码!","提示",JOptionPane.OK_OPTION); }}4 用DES方法加密输入的字节bytKey需为8字节长,是加密的密码代码:private byte[] encryptByDES(byte[] bytP,byte[] bytKey) throws Exception{ DESKeySpec desKS = new DESKeySpec(bytKey);SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");SecretKey sk = skf.generateSecret(desKS);Cipher cip = Cipher.getInstance("DES");cip.init(Cipher.ENCRYPT_MODE,sk);return cip.doFinal(bytP);}5 用DES方法解密输入的字节bytKey需为8字节长,是解密的密码代码:private byte[] decryptByDES(byte[] bytE,byte[] bytKey) throws Exception{ DESKeySpec desKS = new DESKeySpec(bytKey);SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");SecretKey sk = skf.generateSecret(desKS);Cipher cip = Cipher.getInstance("DES");cip.init(Cipher.DECRYPT_MODE,sk);return cip.doFinal(bytE);}6 输入密码的字符形式,返回字节数组形式。