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); } }else JOptionPane.showMessageDialog(this,"密码长度必须等于48!", "错误信息",JOptionPane.ERROR_MESSAGE); }catch(Exception e){ JOptionPane.showMessageDialog(this,"解密失败,请核对密码!", "提示",JOptionPane.OK_OPTION); } }
|