Advertisement
Slanec

Encryptor - a quick implementation with Streams

Jun 12th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.51 KB | None | 0 0
  1. package encryptor;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.JProgressBar;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTextArea;
  23. import javax.swing.SwingUtilities;
  24. import javax.swing.SwingWorker;
  25. import javax.swing.UIManager;
  26. import javax.swing.WindowConstants;
  27.  
  28. public class Encryptor implements Runnable {
  29.  
  30.     public static void main(String[] args) {
  31.         try {
  32.             SwingUtilities.invokeAndWait(new Encryptor());
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.  
  38.     JFrame frame;
  39.     JButton bCancel, bEncrypt;
  40.     JTextArea statusArea;
  41.     JScrollPane statusScroll;
  42.     JProgressBar progressBar;
  43.     String fileType, fileName;
  44.     EncryptSwingWorker encryptWorker;
  45.     int[] key = { 26372, 15219, 53931, 50406, 26072, 23469, 25002, 37812 };
  46.     byte[] output;
  47.  
  48.     @Override
  49.     public void run() {
  50.         try {
  51.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  52.         } catch (Exception e) {
  53.         }
  54.         frame = new JFrame();
  55.         statusArea = new JTextArea();
  56.         statusArea.setEditable(false);
  57.         statusScroll = new JScrollPane(statusArea);
  58.         progressBar = new JProgressBar();
  59.         progressBar.setValue(0);
  60.         bCancel = new JButton("Cancel");
  61.         bCancel.setEnabled(false);
  62.         bCancel.addActionListener(new ActionListener() {
  63.  
  64.             @Override
  65.             public void actionPerformed(ActionEvent arg0) {
  66.                 int option = JOptionPane.showOptionDialog(frame,
  67.                         "Are you sure you want to cancel?", "Warning",
  68.                         JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  69.                         null, null);
  70.                 if (option == 0) {
  71.                     encryptWorker.cancel(true);
  72.                     statusArea.append("Encryption cancelled\n");
  73.                     progressBar.setValue(0);
  74.                     progressBar.setIndeterminate(false);
  75.                     bCancel.setEnabled(false);
  76.                     bEncrypt.setEnabled(true);
  77.                 }
  78.             }
  79.         });
  80.         bEncrypt = new JButton("Encrypt file...");
  81.         bEncrypt.addActionListener(new ActionListener() {
  82.  
  83.             @Override
  84.             public void actionPerformed(ActionEvent e) {
  85.                 encryptWorker = Encryptor.this.new EncryptSwingWorker();
  86.                 encryptWorker.execute();
  87.             }
  88.         });
  89.         JPanel top = new JPanel(new BorderLayout());
  90.         top.add(bEncrypt, BorderLayout.CENTER);
  91.         top.add(bCancel, BorderLayout.EAST);
  92.         frame.getContentPane().add(top, BorderLayout.NORTH);
  93.         frame.getContentPane().add(statusScroll, BorderLayout.CENTER);
  94.         frame.getContentPane().add(progressBar, BorderLayout.SOUTH);
  95.         frame.setSize(200, 150);
  96.         frame.setResizable(false);
  97.         frame.setTitle("sEncryptor");
  98.         frame.setVisible(true);
  99.         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  100.         frame.setIconImage(Toolkit.getDefaultToolkit().getImage("encryptor.png"));
  101.     }
  102.  
  103.     void encrypt() throws IOException {
  104.         JFileChooser fc = new JFileChooser();
  105.         int returnVal = fc.showOpenDialog(frame);
  106.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  107.             if (fc.getSelectedFile() == null) {
  108.                 JOptionPane.showOptionDialog(frame, "You did not select a file",
  109.                         "File Open Error", JOptionPane.DEFAULT_OPTION,
  110.                         JOptionPane.WARNING_MESSAGE, null, null, null);
  111.                 encrypt();
  112.                 return;
  113.             }
  114.             bCancel.setEnabled(true);
  115.             bEncrypt.setEnabled(false);
  116.             fileName = fc.getSelectedFile().getPath();
  117.             statusArea.append("Calculating size...\n");
  118.             progressBar.setIndeterminate(true);
  119.             int size = (int)(new File(fileName).length() / 32);
  120.             progressBar.setIndeterminate(false);
  121.             progressBar.setMaximum(size);
  122.             InputStream bR = new FileInputStream(fc.getSelectedFile());
  123.             byte[] input = new byte[(int)fc.getSelectedFile().length()];
  124.             int length = bR.read(input);
  125.             output = new byte[length];
  126.             statusArea.append("Encrypting...\n");
  127.             int pos = 0;
  128.             for (int i = 0; i < length; i++) {
  129.                 // progressbar removed, I didn't bother to leave it working
  130.                 if (encryptWorker.isCancelled()) {
  131.                     return;
  132.                 }
  133.                 // key might be better a byte[] twice as long as it is now, because righ now, half of the bits are unused
  134.                 output[i] = (byte)(input[i] ^ key[pos++]);
  135.                 if (pos == 8) {
  136.                     pos = 0;
  137.                 }
  138.             }
  139.             progressBar.setValue(0);
  140.             progressBar.setIndeterminate(true);
  141.             bR.close();
  142.             statusArea.append("Encryption finished\n");
  143.             progressBar.setIndeterminate(false);
  144.         }
  145.     }
  146.  
  147.     void save(String file) throws IOException {
  148.         statusArea.append("Saving...\n");
  149.         progressBar.setIndeterminate(true);
  150.         OutputStream bW = new BufferedOutputStream(new FileOutputStream(file));
  151.         bW.write(output);
  152.         bW.close();
  153.         progressBar.setIndeterminate(false);
  154.         statusArea.append("Done!\n");
  155.     }
  156.  
  157.     void saveAs() throws IOException {
  158.         JFileChooser fc = new JFileChooser();
  159.         int returnVal = fc.showSaveDialog(frame);
  160.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  161.             if (fc.getSelectedFile() == null) {
  162.                 JOptionPane.showOptionDialog(frame, "You did not select a file",
  163.                         "File Open Error", JOptionPane.DEFAULT_OPTION,
  164.                         JOptionPane.WARNING_MESSAGE, null, null, null);
  165.                 saveAs();
  166.                 return;
  167.             }
  168.             save(fc.getSelectedFile().getPath());
  169.         }
  170.     }
  171.  
  172.     class EncryptSwingWorker extends SwingWorker<Void, Void> {
  173.  
  174.         @Override
  175.         protected Void doInBackground() throws Exception {
  176.             encrypt();
  177.             if (EncryptSwingWorker.this.isCancelled())
  178.                 return null;
  179.             bCancel.setEnabled(false);
  180.             while (true) {
  181.                 Object[] options = { "Override", "Save As...", "Cancel" };
  182.                 int option = JOptionPane.showOptionDialog(frame,
  183.                         "Override existing file?", "Save",
  184.                         JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
  185.                         null, options, options[0]);
  186.                 if (option == 0)
  187.                     try {
  188.                         save(fileName);
  189.                     } catch (IOException e1) {
  190.                         e1.printStackTrace();
  191.                     }
  192.                 else if (option == 1)
  193.                     try {
  194.                         saveAs();
  195.                     } catch (IOException e1) {
  196.                         e1.printStackTrace();
  197.                     }
  198.                 else {
  199.                     int option1 = JOptionPane.showOptionDialog(frame,
  200.                             "The encryption will be lost if you continue\n"
  201.                                     + "Are you sure you want to cancel?", "Warning",
  202.                             JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
  203.                             null, null, null);
  204.                     if (option1 == 1)
  205.                         continue;
  206.                     break;
  207.                 }
  208.                 break;
  209.             }
  210.             bEncrypt.setEnabled(true);
  211.             return null;
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement