还剩4页未读,继续阅读
文本内容:
生成密钥加密信息解密信息〃加密()生成密钥文件()利用密钥,加密文件()依据密钥,对DES123加密的文件解import;import;import;import;import;import;import;import;import;import;import;import;import;import;import;import;import;import;public classDES{public staticvoid mainStringargs[]{DemoWindowl dw=new DemoWindowlHDES加密程序”;dw.setBoundsdw.getToolkit.getScreenSize.width/3,dw.getToolkit.getScreenSize.height/3,dw.getToolkit.getScreenSize.width/3,dw.getToolkit.getScreenSize.height/3;dw.setDefaultCloseOperationJ Frame.EXIT_ON_CLOSE;dw.setVisibletrue;class DemoWindowlextends JFrameimplements ActionListener{JTextArea jta=new JTextArea;//设置文本区的滚动条intv=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;JScrollPane jsp=new JScrollPanejta,v,h;//菜单JMenuBar menuBar=new JMenuBar;加密”;JMenu mainMenu=new JMenuDES生成密钥;JMenultem generateitem=new JMenultem“力□密信息”;JMenultem encryptltem=new JMenultemJMenultem解密信息;decryptltem=new JMenultem//构造函数public DemoWindowlStringtitle{supertitle;//添加菜单mainMenu.addgenerateltem;mainMenu.addSeparator;mainMenu.addencryptltem;mainMenu.adddecryptltem;menuBar.addmainMenu;setJMenuBarmenuBar;//添加文本区addjsp;//添加事务监听generateitem.addActionListenerthis;encryptltem.addActionListenerthis;decryptltem.addActionListenerthis;public voidactionPerformedActionEvent e{//TODO Auto-generated methodstubtry//保存密钥文件的书目路径主要用来设置密钥的保存路径String fileName=null;//设置文件选择对话框JFileChooser jfc=new JFileChooser;“保存密钥”;jfc.setDialogTitle〃该文件选择对话框只能打开文件书目jfc.setFileSelectionModeJFileChooser.DIRECTORIES_ONLY;〃假如选中,则获得选择的书目名称if jfc.showSaveDialogthis==JFileChooser.APPROVE_OPTION{fileName=jfc.getSelectedFile.getPath;//假如没有选择书目,则退出if fileName==null{return;//推断书目是否存在,假如不存在,则创建File file=new FilefileName;if!file.existsfile.mkdir;//创建密钥生成器DESKeyGenerator keyGen=KeyGenerator.getlnstanceDES;//采纳位密钥56DESkeyGen.init56;//生成密钥DESKey key=keyGen.generateKey;key;//将密钥存储到指定书目下的“密钥”文件中.datObjectOutputStream out=new ObjectOutputStream密钥new FileOutputStreamffileName+”/.dat;out.writeObjectkey;out.close;}catch Exceptionel{//TODO Auto-generated catchblockel.printStackTrace;}try//读取密钥文件的完整路径String fileName=null;//设置文件选择对话框JFileChooser jfc=new JFileChooser;导入密钥文件”;jfc.setDialogTitle//假如选中,则获得选择文件的完整路径if jfc.showOpenDialogthis==JFileChooser.APPROVE_OPTION{fileName=jfc.getSelectedFile.getPath;//假如没有选择文件,则退出if fileName==null{return;//读取密钥文件Objectinputstream in=new ObjectlnputStreamnewFilelnputStreamfileName;Key key=Key in.readObject;//创建加密器DESCipher cipher=Cipher.getlnstanceDES;//设置加密模式cipher.initCipher.ENCRYPT_MODE,key;〃生成密文,存放在字节数组中cipherTextbyte[]cipherText=cipher.doFinaljta.getText.getBytes UTF-8;//设置文件选择对话框jfc=new JFileChooser;保存加密文本内容;jfc.setDialogTitle//假如选中,则获得选择的文件的完整路径if jfc.showSaveDialogthis==JFileChooser.APPROVE_OPTION{fileName=jfc.getSelectedFile.getPath;//假如没有选择文件,则退出if fileName==nullreturn;//将密文存储到指定文件中FileOutputStream fos=new FileOutputStreamfileName,false;fos.writecipherText;fos.close;}catch Exceptionel{//TODO Auto-generated catchblock el.printStackTrace;}}else ife.getSource==decryptltem{try//读取密钥文件的完整路径String fileName=null;//设置文件选择对话框JFileChooser jfc=new JFileChooser;打开加密文本内容;jfc.setDialogTitleif jfc.showOpenDialogthis==JFileChooser.APPROVE_OPTION{fileName=jfc.getSelectedFile.getPath;//假如没有选择文件,就退出if fileName==null return;//读取密文Fileinputstream fis=new FilelnputStreamfileName;byte[]cipherText=new byte[fis.available];fis.readcipherText;fis.close;//设置文件选择对话框jfc=new JFileChooser;“导入密钥文件”;jfc.setDialogTitleif jfc.showOpenDialogthis==JFileChooser.APPROVE_OPTION{fileName=jfc.getSelectedFile.getPath;//假如没有选择文件,则退出if fileName==nullreturn;//读取密钥Objectinputstream in=new ObjectlnputStreamnewFilelnputStreamfileName;Key key=Key in.readObject;//创建加密器DESCipher cipher=Cipher.getlnstanceDES;//设置解密模式cipher.initCipher.DECRYPT_MODE,key;〃生成原文,存放在字节数组中cipherText byte[]plainText=cipher.doFinalcipherText;//将原文显示在文本区中jta.setTextnew StringplainText,UTF-8;}catch Exceptionel{el.printStackTrace;}。
个人认证
优秀文档
获得点赞 0