0

Im sorry if this seems like a dumb question, but is it possible for anyone to tell me if it is possible to add a JProgress bar at the part where my program says "Generating a 56-bit DES key..." and if yes how to go about doing it?

And also, is it possible for me to automatically close the dialog that says "Your key has been generated!"?

The purpose of the progress bar is purely for aesthetics.

Thanks for the help!

Code:

public class myDesCbc2 {


public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {


        JFrame frame = null;
        JFileChooser fChoose = new JFileChooser(System.getProperty("user.home"));
        int returnVal = fChoose.showOpenDialog(frame);
        File myFile = fChoose.getSelectedFile();

        FileInputStream fis = new FileInputStream(myFile);
        BufferedReader stream = new BufferedReader(new InputStreamReader(fis, "ISO-8859-1"));
        String file;
        while ((file = stream.readLine()) != null) {

            JOptionPane.showOptionDialog(
                    null, "Generating a 56-bit DES key...", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

        }
        // Create an 8-byte initialization vector
        SecureRandom sr = new SecureRandom();
        byte[] iv = new byte[8];
        sr.nextBytes(iv);
        IvParameterSpec IV = new IvParameterSpec(iv);

        // Create a 56-bit DES key
        KeyGenerator kg = KeyGenerator.getInstance("DES");

        // Initialize with keysize
        kg.init(56);
        Key mykey = kg.generateKey();

        JOptionPane.showOptionDialog(
                null, "Your key has been generated!", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

        // Create a cipher object and use the generated key to initialize it
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, mykey, IV);

        byte[] plaintext = file.getBytes("UTF8");

        // Encrypt the text
        byte[] ciphertext = cipher.doFinal(plaintext);

        JOptionPane.showMessageDialog(
                null, "Your ciphertext is" + asHex(ciphertext), "Done!", JOptionPane.PLAIN_MESSAGE);

    }
}
Noob_Programmer
  • 111
  • 5
  • 14

1 Answers1

0

Yes, but you're going to have to completely re-work your code.

I'd prefer to use a SwingWorker to do this as it gives me more control, but in a pinch you could use ProgressMonitor or ProgressMonitorInputStream

Take a look at How to use progress bars for more details and examples

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366