JProgressbar的程序

时间:2014-01-13 03:54:07

标签: java jprogressbar

我很抱歉,如果这看起来像一个愚蠢的问题,但是任何人都可以告诉我是否有可能在我的程序“生成56位DES密钥......”的部分添加JProgress栏。如果是的话怎么去做呢?

而且,我是否可以自动关闭“您的密钥已生成!”的对话框?

进度条的目的纯粹是为了美学。

感谢您的帮助!

代码:

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);

    }
}

1 个答案:

答案 0 :(得分:0)

是的,但您必须完全重新编写代码。

我更愿意使用SwingWorker来执行此操作,因为它可以让我获得更多控制权,但在紧要关头,您可以使用ProgressMonitorProgressMonitorInputStream

请查看How to use progress bars了解更多详情和示例