Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package encryptor;
- import java.awt.BorderLayout;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.SwingUtilities;
- import javax.swing.SwingWorker;
- import javax.swing.UIManager;
- import javax.swing.WindowConstants;
- public class Encryptor implements Runnable {
- public static void main(String[] args) {
- try {
- SwingUtilities.invokeAndWait(new Encryptor());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- JFrame frame;
- JButton bCancel, bEncrypt;
- JTextArea statusArea;
- JScrollPane statusScroll;
- JProgressBar progressBar;
- String fileType, fileName;
- EncryptSwingWorker encryptWorker;
- int[] key = { 26372, 15219, 53931, 50406, 26072, 23469, 25002, 37812 };
- byte[] output;
- @Override
- public void run() {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- }
- frame = new JFrame();
- statusArea = new JTextArea();
- statusArea.setEditable(false);
- statusScroll = new JScrollPane(statusArea);
- progressBar = new JProgressBar();
- progressBar.setValue(0);
- bCancel = new JButton("Cancel");
- bCancel.setEnabled(false);
- bCancel.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- int option = JOptionPane.showOptionDialog(frame,
- "Are you sure you want to cancel?", "Warning",
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- null, null);
- if (option == 0) {
- encryptWorker.cancel(true);
- statusArea.append("Encryption cancelled\n");
- progressBar.setValue(0);
- progressBar.setIndeterminate(false);
- bCancel.setEnabled(false);
- bEncrypt.setEnabled(true);
- }
- }
- });
- bEncrypt = new JButton("Encrypt file...");
- bEncrypt.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- encryptWorker = Encryptor.this.new EncryptSwingWorker();
- encryptWorker.execute();
- }
- });
- JPanel top = new JPanel(new BorderLayout());
- top.add(bEncrypt, BorderLayout.CENTER);
- top.add(bCancel, BorderLayout.EAST);
- frame.getContentPane().add(top, BorderLayout.NORTH);
- frame.getContentPane().add(statusScroll, BorderLayout.CENTER);
- frame.getContentPane().add(progressBar, BorderLayout.SOUTH);
- frame.setSize(200, 150);
- frame.setResizable(false);
- frame.setTitle("sEncryptor");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- frame.setIconImage(Toolkit.getDefaultToolkit().getImage("encryptor.png"));
- }
- void encrypt() throws IOException {
- JFileChooser fc = new JFileChooser();
- int returnVal = fc.showOpenDialog(frame);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- if (fc.getSelectedFile() == null) {
- JOptionPane.showOptionDialog(frame, "You did not select a file",
- "File Open Error", JOptionPane.DEFAULT_OPTION,
- JOptionPane.WARNING_MESSAGE, null, null, null);
- encrypt();
- return;
- }
- bCancel.setEnabled(true);
- bEncrypt.setEnabled(false);
- fileName = fc.getSelectedFile().getPath();
- statusArea.append("Calculating size...\n");
- progressBar.setIndeterminate(true);
- int size = (int)(new File(fileName).length() / 32);
- progressBar.setIndeterminate(false);
- progressBar.setMaximum(size);
- InputStream bR = new FileInputStream(fc.getSelectedFile());
- byte[] input = new byte[(int)fc.getSelectedFile().length()];
- int length = bR.read(input);
- output = new byte[length];
- statusArea.append("Encrypting...\n");
- int pos = 0;
- for (int i = 0; i < length; i++) {
- // progressbar removed, I didn't bother to leave it working
- if (encryptWorker.isCancelled()) {
- return;
- }
- // key might be better a byte[] twice as long as it is now, because righ now, half of the bits are unused
- output[i] = (byte)(input[i] ^ key[pos++]);
- if (pos == 8) {
- pos = 0;
- }
- }
- progressBar.setValue(0);
- progressBar.setIndeterminate(true);
- bR.close();
- statusArea.append("Encryption finished\n");
- progressBar.setIndeterminate(false);
- }
- }
- void save(String file) throws IOException {
- statusArea.append("Saving...\n");
- progressBar.setIndeterminate(true);
- OutputStream bW = new BufferedOutputStream(new FileOutputStream(file));
- bW.write(output);
- bW.close();
- progressBar.setIndeterminate(false);
- statusArea.append("Done!\n");
- }
- void saveAs() throws IOException {
- JFileChooser fc = new JFileChooser();
- int returnVal = fc.showSaveDialog(frame);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- if (fc.getSelectedFile() == null) {
- JOptionPane.showOptionDialog(frame, "You did not select a file",
- "File Open Error", JOptionPane.DEFAULT_OPTION,
- JOptionPane.WARNING_MESSAGE, null, null, null);
- saveAs();
- return;
- }
- save(fc.getSelectedFile().getPath());
- }
- }
- class EncryptSwingWorker extends SwingWorker<Void, Void> {
- @Override
- protected Void doInBackground() throws Exception {
- encrypt();
- if (EncryptSwingWorker.this.isCancelled())
- return null;
- bCancel.setEnabled(false);
- while (true) {
- Object[] options = { "Override", "Save As...", "Cancel" };
- int option = JOptionPane.showOptionDialog(frame,
- "Override existing file?", "Save",
- JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
- null, options, options[0]);
- if (option == 0)
- try {
- save(fileName);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- else if (option == 1)
- try {
- saveAs();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- else {
- int option1 = JOptionPane.showOptionDialog(frame,
- "The encryption will be lost if you continue\n"
- + "Are you sure you want to cancel?", "Warning",
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
- null, null, null);
- if (option1 == 1)
- continue;
- break;
- }
- break;
- }
- bEncrypt.setEnabled(true);
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement