/* * DecryptExternalFileDialog.java * * * ==================================================== Professional Data Security (PDS) http://crypto.brettlee.com ==================================================== Copyright (c) 2009-2011, Brett Lee All rights reserved. Portions Copyright (C) 1995-2008, Sun Microsystems, Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the ORGANIZATION nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ============================================================================= */ package com.brettlee.crypto; import java.awt.Cursor; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import javax.swing.filechooser.FileNameExtensionFilter; /** * DecryptExternalFileDialog - File-> Encrypt/Decrypt -> Decrypt Existing... * */ class DecryptExternalFileDialog { /** * */ private Gui mGui; JDialog decryptExternalFileDialog; JCheckBox decryptFromFile = new JCheckBox("File:", true); JCheckBox decryptFromRaw = new JCheckBox("Tape or Raw Device:", false); JCheckBox decryptToDefaultDir = new JCheckBox("Create in the encrypted files default directory", true); JCheckBox decryptToSameDir = new JCheckBox("Create in the same directory as the source", false); JCheckBox decryptToSelectedDir = new JCheckBox("Select a directory", false); JCheckBox decryptDirAndExtract = new JCheckBox("Extract All Files", true); JCheckBox decryptDirToZip = new JCheckBox("Create a Zip File", true); JButton jButtonDecrypt = new JButton("Decrypt"); final JTextField cryptoFileNameField = new JTextField("", 31); final JTextField cryptoRawNameField = new JTextField("", 22); final JTextField decryptToSelectedDirField = new JTextField("", 35); String newline = System.getProperty("line.separator"); // Constructor public DecryptExternalFileDialog ( Gui theGui, final CryptoFileExternal cryptoFile, int calledFileOrDir ) { ///////////////////////////////////////////////////////////// // Build the Dialog mGui = theGui; decryptExternalFileDialog = new JDialog(mGui.mainFrame, "Decrypt a PDS File", true); JPanel contentPane = new JPanel(); decryptExternalFileDialog.getContentPane().add(contentPane); GroupLayout layout = new GroupLayout(contentPane); contentPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); FlowLayout flowLayoutRight = new FlowLayout(); flowLayoutRight.setAlignment(FlowLayout.RIGHT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel( new GridLayout(2,1,0,0) ); JPanel p4 = new JPanel( flowLayoutLeft ); JPanel p5 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) .addComponent(p5) )); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) .addComponent(p5) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = null; if ( calledFileOrDir == 0 ) { jlabel = new JLabel("Decrypt a PDS File"); decryptExternalFileDialog.setTitle("Professional Data Security (PDS) - Decrypt a PDS File"); } else if ( calledFileOrDir == 1 ) { decryptExternalFileDialog.setTitle("Decrypt a PDS Directory"); jlabel = new JLabel("Decrypt a PDS Directory"); decryptExternalFileDialog.setTitle("Professional Data Security (PDS) - Decrypt a PDS Directory"); } p1.add(jlabel); // p2 - Assign the New File a Name if ( calledFileOrDir == 0 ) { p2.setBorder(new TitledBorder("Filename")); } else { p2.setBorder(new TitledBorder("Location of the Encrypted Directory")); } JPanel p2SubPanel = new JPanel(new GridLayout(2,1,0,0)); JPanel p2SubPanelFile = new JPanel(flowLayoutLeft); JPanel p2SubPanelRaw = new JPanel(flowLayoutLeft); final JButton iconOpenp2File = new JButton(Gui.createImageIcon("images/folder.gif", 28)); iconOpenp2File.setFocusable(false); final JButton iconOpenp2Raw = new JButton(Gui.createImageIcon("images/folder.gif", 28)); iconOpenp2Raw.setFocusable(false); p2SubPanel.add(p2SubPanelFile); p2SubPanel.add(p2SubPanelRaw); decryptFromFile.setSelected(true); decryptFromFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptFromFile.isSelected() ) { cryptoFileNameField.setEditable(true); iconOpenp2File.setEnabled(true); decryptFromRaw.setSelected(false); iconOpenp2Raw.setEnabled(false); cryptoRawNameField.setEditable(false); decryptToSameDir.setEnabled(true); } else { cryptoFileNameField.setEditable(false); iconOpenp2File.setEnabled(false); } } }); p2SubPanelFile.add(decryptFromFile); p2SubPanelFile.add(cryptoFileNameField); decryptFromRaw.setSelected(false); decryptFromRaw.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptFromRaw.isSelected() ) { cryptoRawNameField.setEditable(true); iconOpenp2Raw.setEnabled(true); decryptFromFile.setSelected(false); iconOpenp2File.setEnabled(false); cryptoFileNameField.setEditable(false); decryptToSameDir.setSelected(false); decryptToSameDir.setEnabled(false); } else { cryptoRawNameField.setEditable(false); iconOpenp2Raw.setEnabled(false); decryptToSameDir.setEnabled(true); } } }); p2SubPanelRaw.add(decryptFromRaw); cryptoRawNameField.setEditable(false); p2SubPanelRaw.add(cryptoRawNameField); p2.add(p2SubPanel); Gui.enterPressesWhenFocused(iconOpenp2File); iconOpenp2File.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { final JFileChooser fileChooser; if (mGui.stateObject.getDefaultEncryptedFilePath() instanceof String) { fileChooser = new JFileChooser ( mGui.stateObject.getDefaultEncryptedFilePath() ); } else { fileChooser = new JFileChooser ( mGui.runTimePath ); } fileChooser.setDialogTitle( "KeyStore Location" ); fileChooser.setApproveButtonText("Select"); FileNameExtensionFilter filterPDS = new FileNameExtensionFilter("Professional Data Security Files (PDS)", "PDS"); FileNameExtensionFilter filterJCEKS = new FileNameExtensionFilter("Java Cryptographic Extension (JCE) KeyStores (JCEKS)", "JCEKS"); FileNameExtensionFilter filterJKS = new FileNameExtensionFilter("Java KeyStores (JKS)", "JKS"); FileNameExtensionFilter filterKeyStores = new FileNameExtensionFilter("Java KeyStores (JCEKS, JKS)", "JCEKS", "JKS"); fileChooser.addChoosableFileFilter( filterJKS ); fileChooser.addChoosableFileFilter( filterJCEKS ); fileChooser.addChoosableFileFilter(filterKeyStores); fileChooser.addChoosableFileFilter( filterPDS ); int returnVal = fileChooser.showOpenDialog(mGui.mainFrame); if ( returnVal == 0 ) { cryptoFile.setFullyPathedFileName ( fileChooser.getSelectedFile().getPath() ); cryptoFile.setFileName( fileChooser.getSelectedFile().getName() ); cryptoFileNameField.setText( cryptoFile.getFullyPathedFileName() ); // System.out.println("Crypto File Name: " + cryptoFile.getFullyPathedFileName() ); } else { // System.out.println("File name not returned from the chooser. Cancel."); return; } } }); iconOpenp2File.setToolTipText("Select File"); p2SubPanelFile.add(iconOpenp2File); Gui.enterPressesWhenFocused(iconOpenp2Raw); iconOpenp2Raw.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { final JFileChooser fileChooser; if (mGui.stateObject.getDefaultEncryptedFilePath() instanceof String) { fileChooser = new JFileChooser ( mGui.stateObject.getDefaultEncryptedFilePath() ); } else { fileChooser = new JFileChooser ( mGui.runTimePath ); } fileChooser.setDialogTitle( "Location" ); fileChooser.setApproveButtonText("Select"); int returnVal = fileChooser.showOpenDialog(mGui.mainFrame); if ( returnVal == 0 ) { cryptoFile.setFullyPathedFileName ( fileChooser.getSelectedFile().getPath() ); cryptoFile.setFileName( fileChooser.getSelectedFile().getName() ); cryptoRawNameField.setText( cryptoFile.getFullyPathedFileName() ); // System.out.println("Crypto Path to File: " + cryptoFile.getFullyPathedFileName() ); // System.out.println("Crypto File Name: " + cryptoFile.getFileName() ); } else { // System.out.println("File name not returned from the chooser. Cancel."); return; } } }); iconOpenp2Raw.setToolTipText("Select Tape or Raw device"); iconOpenp2Raw.setEnabled(false); p2SubPanelRaw.add(iconOpenp2Raw); // p3 - Define location for new decrypted file to be written p3.setBorder(new TitledBorder("Destination for the Decrypted Data")); final JButton p3iconOpen = new JButton(Gui.createImageIcon("images/folder.gif", 28)); p3iconOpen.setFocusable(false); JPanel p3SubPanel = new JPanel( new GridLayout(3,1,0,0) ); decryptToDefaultDir.setSelected(true); decryptToDefaultDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptToDefaultDir.isSelected() ) { decryptToSameDir.setSelected(false); decryptToSelectedDir.setSelected(false); p3iconOpen.setEnabled(false); } } }); p3SubPanel.add(decryptToDefaultDir); decryptToSameDir.setSelected(false); decryptToSameDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptToSameDir.isSelected() ) { decryptToDefaultDir.setSelected(false); decryptToSelectedDir.setSelected(false); p3iconOpen.setEnabled(false); } } }); p3SubPanel.add(decryptToSameDir); decryptToSelectedDir.setSelected(false); decryptToSelectedDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptToSelectedDir.isSelected() ) { decryptToDefaultDir.setSelected(false); decryptToSameDir.setSelected(false); p3iconOpen.setEnabled(true); } else { p3iconOpen.setEnabled(false); } } }); p3SubPanel.add(decryptToSelectedDir); p3.add(p3SubPanel); JPanel p3SubPanel2 = new JPanel(flowLayoutRight); decryptToSelectedDirField.setEditable(false); p3SubPanel2.add(decryptToSelectedDirField); Gui.enterPressesWhenFocused(p3iconOpen); p3iconOpen.setEnabled(false); p3iconOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JFrame frame = new JFrame(); JFileChooser fileChooser = new JFileChooser ( mGui.stateObject.getDefaultEncryptedFilePath() ); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle( "Location" ); fileChooser.setApproveButtonText("Select"); int returnVal = fileChooser.showOpenDialog(frame); if ( returnVal == 0 ) { // Get Absolute Path: String dirName = (fileChooser.getSelectedFile().getPath() + mGui.fileSeparator); // System.out.println("Dir Selected: " + dirName); cryptoFile.setDecFilePath(dirName); // System.out.println("Dec File Path in CryptoFile: " + cryptoFile.getDecFilePath()); decryptToSelectedDirField.setText(cryptoFile.getDecFilePath()); decryptToSelectedDirField.setCaretPosition(0); } } }); p3SubPanel2.add(p3iconOpen); p3.add(p3SubPanel2); // p4 - Extract or Create Zip JPanel p4subPanel = new JPanel( new GridLayout(2,1,0,0) ); if ( calledFileOrDir == 0 ) { decryptDirAndExtract.setSelected(false); } else if ( calledFileOrDir == 1 ) { decryptDirAndExtract.setSelected(true); } decryptDirAndExtract.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptDirAndExtract.isSelected() ) { decryptDirToZip.setSelected(false); } } }); p4subPanel.add(decryptDirAndExtract); decryptDirToZip.setSelected(false); decryptDirToZip.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( decryptDirAndExtract.isSelected() ) { decryptDirAndExtract.setSelected(false); } } }); p4subPanel.add(decryptDirToZip); if ( calledFileOrDir == 1 ) { p4.setBorder(new TitledBorder("Output Format")); p4.add(p4subPanel); } // p5 - Decrypt & Cancel Buttons jButtonDecrypt.setMnemonic(KeyEvent.VK_D); Gui.enterPressesWhenFocused(jButtonDecrypt); jButtonDecrypt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Decrypting..."); decryptFile(evt, cryptoFile); } }); p5.add(jButtonDecrypt); JButton jButtonCancel = new JButton("Cancel"); jButtonCancel.setMnemonic(KeyEvent.VK_C); Gui.enterPressesWhenFocused(jButtonCancel); jButtonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Canceling..."); decryptExternalFileDialog.setVisible(false); decryptExternalFileDialog.dispose(); } }); p5.add(jButtonCancel); ///////////////////////////////////////////////////////////// // Finish up decryptExternalFileDialog.pack(); decryptExternalFileDialog.setLocationRelativeTo(null); decryptExternalFileDialog.setVisible(true); } void decryptFile(ActionEvent evt, CryptoFileExternal cryptoFile) { // First, the input... // if ( ( ! decryptFromFile.isSelected() ) && ( ! decryptFromRaw.isSelected() )) { // notify if output dir is not specified JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Please select a source for decryption." + newline, "Decryption source not selected", JOptionPane.WARNING_MESSAGE); return; } if ( ( ! decryptToDefaultDir.isSelected() ) && ( ! decryptToSameDir.isSelected() ) && ( ! decryptToSelectedDir.isSelected() ) ) { // notify if output dir is not specified JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Please select a destination directory." + newline, "Destination directory not selected", JOptionPane.WARNING_MESSAGE); return; } if ( decryptFromFile.isSelected() ) { cryptoFile.setDeviceType( 0 ); // First, just see if there was *nothing* provided // if ( cryptoFileNameField.getText().equals("")) { // notify if file not specified JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Please select the data file to be decrypted.", "Encryted data not provided", JOptionPane.WARNING_MESSAGE); return; } // Check to see if the data was pathed // will then assume absolute path - not the best assumption, but... // Pattern pattern = Pattern.compile("(.*)[/\\\\](.*)"); Matcher matcher = pattern.matcher( cryptoFileNameField.getText() ); if ( matcher.matches() ) { // *IS* pathed // cryptoFile.setFullyPathedFileName( cryptoFileNameField.getText() ); // set the Filename (file.PDS, etc.) // pattern = Pattern.compile("(.*)[/\\\\](.*)"); matcher = pattern.matcher( cryptoFileNameField.getText() ); if( matcher.matches() ) { // System.out.println("Contains forward/back slashes: " // + cryptoFileNameField.getText() ); cryptoFile.setFileName( matcher.group(2) ); // System.out.println("File Name Only : " + cryptoFile.getFileName() ); } } else { // *NOT* pathed - prepend default path to Filename // cryptoFile.setFullyPathedFileName( mGui.stateObject.getDefaultEncryptedFilePath() + cryptoFileNameField.getText()); // and Set the Filename cryptoFile.setFileName( cryptoFileNameField.getText() ); } // The encrypted data should exist // if ( ! new java.io.File( cryptoFile.getFullyPathedFileName() ).exists() ) { // notify if specified but does not exist // JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "The file selected for decryption cannot be found:" + newline + cryptoFile.getFullyPathedFileName(), "File does not exist", JOptionPane.WARNING_MESSAGE); return; } } else if ( decryptFromRaw.isSelected() ) { // Device decryption // cryptoFile.setDeviceType( 1 ); // Input validation // if ( cryptoRawNameField.getText().equals("")) { // notify if device path is not specified // JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Please provide the absolute path to the device" + newline + "that contains the encrypted directory", "File name not provided", JOptionPane.WARNING_MESSAGE); return; } // Device should exist // if ( ! new java.io.File( cryptoRawNameField.getText() ).exists() ) { // notify if device path was specified but does not exist // JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "The device selected for decryption cannot be found:" + newline + cryptoRawNameField.getText() + newline + "Please provide the absolute path to the device" + newline + "that contains the encrypted directory", "Device cannot be found", JOptionPane.WARNING_MESSAGE); return; } else { cryptoFile.setFullyPathedFileName( cryptoRawNameField.getText() ); // set the Filename (st0, etc.) // Pattern pattern = Pattern.compile("(.*)[/\\\\](.*)"); Matcher matcher = pattern.matcher( cryptoRawNameField.getText() ); if( matcher.matches() ) { // System.out.println("Contains forward/back slashes: " // + cryptoFileNameField.getText() ); cryptoFile.setFileName( matcher.group(2) + "_Restored" ); // System.out.println("File Name Only : " + cryptoFile.getFileName() ); } } // System.out.println("File/Device name: " + cryptoFile.getFileName() ); // System.out.println("Decrypting from: " + cryptoFile.getFullyPathedFileName() ); } else { // how did we get here? System.out.println("Unknown encryption type in DecryptExternalFileDialog."); return; } // We have "something" for Input! Next, setup the Output. // Possibilities: // - decrypt PDS file in Base 64 format to original file // - decrypt PDS file in binary format to original file // - decrypt PDS directory and extract all files // - decrypt PDS directory and create a Zip file // Currently do *NOT* know the format of the encryted data. // - Only know what has been requested: // - decryptionTypeRequested: 1 = file, 2 = directory // Set the Destination // // *NOT the final format - PDS extension or filename to be removed // if ( decryptToDefaultDir.isSelected() ) { if ( cryptoFile.getDecryptionTypeRequested() == 0 ) { // need to remove PDS extension cryptoFile.setDecFilePath( mGui.stateObject.getDefaultEncryptedFilePath() + cryptoFile.getFileName() ); } else if ( cryptoFile.getDecryptionTypeRequested() == 1 ) { cryptoFile.setDecFilePath( mGui.stateObject.getDefaultEncryptedFilePath() ); } } if ( decryptToSameDir.isSelected() ) { // for TypeRequested = 0, need to remove PDS extension // for TypeRequested = 1, need to remove file name cryptoFile.setDecFilePath( cryptoFile.getFullyPathedFileName() ); } if ( decryptToSelectedDir.isSelected() ) { // Confirm that (if needed) an output directory was specified // if ( decryptToSelectedDirField.getText().equals("") ) { // notify if file format is not specified JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Please select an output directory." + newline, "Output directory not selected", JOptionPane.WARNING_MESSAGE); return; } // Confirm that the directory exists // try { if ( ! new java.io.File( decryptToSelectedDirField.getText() ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "The directory provided does not exist." + newline + newline + decryptToSelectedDirField.getText() + newline + newline + "Would you like to create it?" + newline + newline, "Directory does not exist", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); if ( n != 0 ) { // System.out.println("Canceling..."); return; } else { // System.out.println("Creating the directory..."); boolean success = (new File(decryptToSelectedDirField.getText())).mkdirs(); if (success) { // System.out.println("Directory: " + decryptToSelectedDirField.getText() + " created"); } else { JOptionPane.showMessageDialog( frame, "Unable to Create the Directory:" + newline + decryptToSelectedDirField.getText() + newline + newline + "Please check the permissions or the path" + newline + "and try again.", "Unable to Create the Directory", JOptionPane.ERROR_MESSAGE); decryptExternalFileDialog.setVisible(false); decryptExternalFileDialog.dispose(); return; } } } } catch (Exception e) { System.out.println("Unable to get fully pathed filename: " + e); return; } if ( cryptoFile.getDecryptionTypeRequested() == 0 ) { // need to remove PDS extension cryptoFile.setDecFilePath( decryptToSelectedDirField.getText() + cryptoFile.getFileName() ); } else if ( cryptoFile.getDecryptionTypeRequested() == 1 ) { cryptoFile.setDecFilePath( decryptToSelectedDirField.getText() ); } } // System.out.println("Pre-Final destination: " + cryptoFile.getDecFilePath()); // for TypeRequested = 0, need to remove PDS extension // if ( cryptoFile.getDecryptionTypeRequested() == 0 ) { // Strip off PDS extension for the decrypted file name (if it exists) // Pattern pattern = Pattern.compile("(.*).PDS", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(cryptoFile.getDecFilePath()); if( matcher.matches() ) { // System.out.println("Contains extension: " + cryptoFile.getDecFilePath()); cryptoFile.setDecFilePath( matcher.group(1) ); // System.out.println("Decrypting to this path: " + cryptoFile.getDecFilePath() ); } } else { if ( decryptToSameDir.isSelected() ) { // System.out.println("Same dir or Raw - decrypt here prior to change: " + cryptoFile.getDecFilePath() ); // Strip off device/file name (if it exists) // // Set just the filename // Pattern pattern = Pattern.compile("(.*)[/\\\\](.*)"); Matcher matcher = pattern.matcher( cryptoFile.getDecFilePath() ); if( matcher.matches() ) { cryptoFile.setDecFilePath( matcher.group(1) + java.io.File.separator ); // System.out.println("Same dir or Raw - decrypt here after change: " + cryptoFile.getDecFilePath() ); } } } // Output Format: 0 = Extract files, 1 = Create Zip File // if ( decryptDirAndExtract.isSelected() ) { cryptoFile.setDecryptedOutputFormat( 0 ); } else if ( decryptDirToZip.isSelected() ) { cryptoFile.setDecryptedOutputFormat( 1 ); // Strip off PDS extension for the decrypted file name (if it exists) // Pattern pattern = Pattern.compile("(.*).PDS", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(cryptoFile.getDecFilePath() + cryptoFile.getFileName()); if( matcher.matches() ) { // System.out.println("Contains extension: " + cryptoFile.getDecFilePath()); cryptoFile.setDecFilePath( matcher.group(1) ); // System.out.println("Decrypting to this path: " + cryptoFile.getDecFilePath() ); } if ( cryptoFile.getDeviceType() == CryptoFileExternal.kDEVICE_FILE ) { cryptoFile.setDecFilePath( cryptoFile.getDecFilePath() + ".ZIP"); } else if ( cryptoFile.getDeviceType() == CryptoFileExternal.kDEVICE_RAW ) { cryptoFile.setDecFilePath( cryptoFile.getDecFilePath() + cryptoFile.getFileName() + ".ZIP"); } } // Here's where it is going // System.out.println("Final destination: " + cryptoFile.getDecFilePath()); // Does a "file" (file or directory) already exist where we want to write? try { if (cryptoFile.getDecryptionTypeRequested() == 0) { if ( new java.io.File( cryptoFile.getDecFilePath() ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, newline + "About to decrypt:" + newline + cryptoFile.getFullyPathedFileName() + newline + "to:" + newline + cryptoFile.getDecFilePath() + newline + newline + "A file with this name currently exists. If you continue," + newline + "this file will be overwritten and any data in the file will be lost." + newline + newline + "Continue?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); if ( n != 0 ) { // System.out.println("Canceling..."); return; } } } else if ((cryptoFile.getDecryptionTypeRequested() == 1) && (cryptoFile.getDecryptedOutputFormat() == 1 )) { // Is the ZIP file already there? if ( new File( cryptoFile.getDecFilePath() ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "The Zip file already exists: " + newline + cryptoFile.getDecFilePath() + newline + "Overwrite the existing file?" + newline, "Overwrite existing file?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); if ( n != 0 ) { // System.out.println("No..."); return; } } } } catch (Exception e) { System.out.println("Unable to get fully pathed filename: " + e); return; } // System.out.println("Going to Verify the KeyStore..."); if ( ! new VerifyKeyStore(mGui).verifyLocation ( cryptoFile, 0 ) ) { return; } // System.out.println("Closing Dialog..."); decryptExternalFileDialog.setVisible(false); decryptExternalFileDialog.dispose(); // Decrypt job will be dispatched to a Thread - should return right away // mGui.mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); if (! new CryptoFileExternal().decryptFile( cryptoFile ) ){ // Was the the action canceled // Is this still valid? I don't think so. // if ( StateObject.getFileChooserCancel() ) { StateObject.setFileChooserCancel(false); mGui.mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); // System.out.println("Decryption was canceled."); return; // This is still valid } else { mGui.mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); // notify if failure JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Error requesting decryption." + newline + "Check the console for additional information or try again.", "Error requesting encryption.", JOptionPane.ERROR_MESSAGE); } } mGui.mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); return; } }