Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JavaCripter_1.1 - JavaCripter.java

JavaCripter.java

Caricato da: Netarrow
Scarica il programma completo

  1. import java.awt.Dimension;
  2. import java.awt.Toolkit;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JDialog;
  14. import javax.swing.JFileChooser;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JMenu;
  18. import javax.swing.JMenuBar;
  19. import javax.swing.JMenuItem;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JTextField;
  22.  
  23. /**
  24.  *
  25.  * @author  netarrow
  26.  */
  27.  class Document extends JDialog implements ActionListener {
  28.         private int f;
  29.         private File path;
  30.         private File dest;
  31.         private JButton jButton1;
  32.         private JButton jButton2;
  33.         private JButton jButton3;
  34.         private JTextField p;
  35.         private JLabel jLabel2;
  36.         private JTextField jTextField1;
  37.         private FileInputStream sf;
  38.         private FileOutputStream df;
  39.         private int ret;
  40.  
  41.          Document(File path, int f) {
  42.                 this.f = f;
  43.                 this.path = path;
  44.                 dest = new File((f == 0 ? path.toString() + ".jcc":path.toString().substring(0, path.toString().lastIndexOf(".jcc"))));
  45.                 initComponents();
  46.                 }  
  47.                
  48.                 public void decodifica(File sff, File dff, String keys) throws IOException {
  49.                         int count = 0, i = 0;
  50.                         int key;
  51.                         try {
  52.                         key = Integer.parseInt(keys);
  53.                         sf = new FileInputStream(sff);
  54.                         df = new FileOutputStream(dff);
  55.                         do {
  56.                         i = sf.read();
  57.                         if(i != -1) {
  58.                         count++;
  59.                         df.write((i - key * count) ^ key);
  60.                         }
  61.                                 } while(i != -1);
  62.                         JOptionPane.showMessageDialog(this, "Decodifica riuscita (se il testo rimande incomprensibile la password è errata)", "JavaCripter", JOptionPane.INFORMATION_MESSAGE);
  63.                         setVisible(false);
  64.                         }catch(Exception exc) {
  65.                         JOptionPane.showMessageDialog(this, exc.toString(), "Errore", JOptionPane.ERROR_MESSAGE);
  66.                         return;
  67.                         } finally {
  68.                         count = 0;
  69.                         sf.close();
  70.                         df.close();
  71.                         }
  72.                 }
  73.                
  74.                 public void codifica(File sff, File dff, String keys) throws IOException {
  75.                         int count = 0, i = 0;
  76.                         int key;
  77.                         try {
  78.                                 key = Integer.parseInt(keys);
  79.                                 if(key <= 0) throw new IllegalPasswordException();
  80.                                 sf = new FileInputStream(sff);
  81.                                 df = new FileOutputStream(dff);
  82.                                 do {
  83.                                 i = sf.read();
  84.                                 if(i != -1) {
  85.                         count++;
  86.                         df.write(((i ^ key) + key * count));
  87.                         }
  88.         } while(i != -1);
  89.         JOptionPane.showMessageDialog(this, "Codifica riuscita", "JavaCripter", JOptionPane.INFORMATION_MESSAGE);
  90.         setVisible(false);
  91. }catch(Exception exc) {
  92. JOptionPane.showMessageDialog(this, exc.toString(), "Errore", JOptionPane.ERROR_MESSAGE);
  93. setVisible(false);
  94. return;
  95. } finally {
  96.         count = 0;
  97.     df.close();
  98.     sf.close();
  99. }
  100. }
  101.        
  102.   private void initComponents() {
  103.         jLabel2 = new JLabel();
  104.         jTextField1 = new JTextField();
  105.         jButton2 = new JButton();
  106.         jButton3 = new JButton();
  107.  
  108.         getContentPane().setLayout(null);
  109.        
  110.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  111.  
  112.         jLabel2.setText("Inserire password(numerica)");
  113.         getContentPane().add(jLabel2);
  114.         jLabel2.setBounds(0, 50, jLabel2.getPreferredSize().width, jLabel2.getPreferredSize().height);
  115.  
  116.         getContentPane().add(jTextField1);
  117.         jTextField1.setBounds(170, 50, 190, 22);
  118.  
  119.         jButton2.setText("Ok");
  120.                 jButton2.addActionListener(this);
  121.         getContentPane().add(jButton2);
  122.         jButton2.setBounds(60, 80, 90, 25);
  123.  
  124.         jButton3.setText("Annulla");
  125.                 jButton3.addActionListener(this);
  126.         getContentPane().add(jButton3);
  127.         jButton3.setBounds(270, 80, 90, 25);
  128.             switch(f) {
  129.                 case 0:
  130.                 setTitle("Codifica " + "\"" + path.getName() + "\"");
  131.                 break;
  132.                 case 1:
  133.                         setTitle("Decodifica " + "\"" + path.getName() + "\"");
  134.                         break;
  135.             }
  136.         setSize(400, 150);
  137.                 Toolkit t = Toolkit.getDefaultToolkit();
  138.                 Dimension d=t.getScreenSize();
  139.                 Dimension win=getSize();
  140.                 win=getSize();
  141.                 setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
  142.     }
  143.    
  144.         public void actionPerformed(ActionEvent e) {
  145.                 if(e.getActionCommand().equalsIgnoreCase("ok")) {
  146.                         switch(f) {
  147.                                 case 0:
  148.                                 try {
  149.                                         codifica(path, dest, jTextField1.getText());
  150.                                         setVisible(false);
  151.                                 } catch (Exception e1) {
  152.                                         return;
  153.                                 }
  154.                                 break;
  155.                                 case 1:
  156.                                 try {
  157.                                         decodifica(path, dest, jTextField1.getText());
  158.                                 } catch (IOException e2) {
  159.                                         return;
  160.                                 }
  161.                                 break;
  162.                         }
  163.                 } else
  164.                 if(e.getActionCommand().equalsIgnoreCase("annulla")) {
  165.                         setVisible(false);
  166.                 }
  167.         }
  168.  }
  169.  
  170.  
  171.  
  172.  
  173. /**
  174.  * @author netarrow
  175.  */
  176. public class JavaCripter extends JFrame implements ActionListener {
  177.     private JFileChooser chooser;
  178.     private Document document;
  179.     int value;
  180.     /** Creates new form JFrame */
  181.     public JavaCripter() {
  182.         initComponents();
  183.     }
  184.      
  185.     /** This method is called from within the constructor to
  186.      * initialize the form.
  187.      * WARNING: Do NOT modify this code. The content of this method is
  188.      * always regenerated by the Form Editor.
  189.      */
  190.     private void initComponents() {//GEN-BEGIN:initComponents
  191.         chooser = new JFileChooser();
  192.         chooser.addActionListener(this);
  193.         jButton1 = new JButton();
  194.         jButton2 = new JButton();
  195.         jLabel1 = new JLabel();
  196.         jMenuBar1 = new JMenuBar();
  197.         jMenu1 = new JMenu();
  198.         jMenuItem1 = new JMenuItem();
  199.         jMenuItem2 = new JMenuItem();
  200.         jMenuItem3 = new JMenuItem();
  201.         jMenu2 = new JMenu();
  202.         jMenuItem4 = new JMenuItem();
  203.  
  204.         getContentPane().setLayout(null);
  205.  
  206.         addWindowListener(new WindowAdapter() {
  207.             public void windowClosing(WindowEvent evt) {
  208.                 exitForm(evt);
  209.             }
  210.         });
  211.  
  212.         jButton1.setText("Codifica");
  213.         getContentPane().add(jButton1);
  214.         jButton1.setBounds(160, 30, 120, 25);
  215.  
  216.         jButton2.setText("Decodifica");
  217.         getContentPane().add(jButton2);
  218.         jButton2.setBounds(0, 30, 120, 25);
  219.  
  220.         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  221.         jLabel1.setText("JavaCripter version 1.1");
  222.         getContentPane().add(jLabel1);
  223.         jLabel1.setBounds(0, 10, 280, 16);
  224.  
  225.         jMenu1.setText("File");
  226.         jMenuItem1.setText("Codifica");
  227.         jMenu1.add(jMenuItem1);
  228.  
  229.         jMenuItem2.setText("Decodifica");
  230.         jMenu1.add(jMenuItem2);
  231.  
  232.         jMenuItem3.setText("Esci");
  233.         jMenu1.add(jMenuItem3);
  234.  
  235.         jMenuBar1.add(jMenu1);
  236.  
  237.         jMenu2.setText("?");
  238.         jMenuItem4.setText("About");
  239.         jMenu2.add(jMenuItem4);
  240.  
  241.         jMenuBar1.add(jMenu2);
  242.  
  243.         setJMenuBar(jMenuBar1);
  244.  
  245.         pack();
  246.         setSize(286, 105);
  247.         setTitle("JavaCripter");
  248.                 Toolkit t = Toolkit.getDefaultToolkit();
  249.  
  250.                 Dimension d=t.getScreenSize();
  251.  
  252.                 Dimension win=getSize();
  253.  
  254.                 win=getSize();
  255.  
  256.                 setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
  257.                
  258.                 setResizable(false);
  259.                
  260.                 show();
  261.                
  262.                 jButton1.addActionListener(this);
  263.                 jButton2.addActionListener(this);
  264.                 jMenuItem1.addActionListener(this);
  265.                 jMenuItem2.addActionListener(this);
  266.                 jMenuItem3.addActionListener(this);
  267.                 jMenuItem4.addActionListener(this);
  268.                
  269.     }//GEN-END:initComponents
  270.    
  271.     /** Exit the Application */
  272.     private void exitForm(WindowEvent evt) {//GEN-FIRST:event_exitForm
  273.         System.exit(0);
  274.     }//GEN-LAST:event_exitForm
  275.    
  276.     /**
  277.      * @param args the command line arguments
  278.      */
  279.     public static void main(String args[]) {
  280.         new JavaCripter().show();
  281.     }
  282.    
  283.     // Variables declaration - do not modify//GEN-BEGIN:variables
  284.     private int f;
  285.     private JButton jButton1;
  286.     private JButton jButton2;
  287.     private JLabel jLabel1;
  288.     private JMenu jMenu1;
  289.     private JMenu jMenu2;
  290.     private JMenuBar jMenuBar1;
  291.     private JMenuItem jMenuItem1;
  292.     private JMenuItem jMenuItem2;
  293.     private JMenuItem jMenuItem3;
  294.     private JMenuItem jMenuItem4;
  295.         /** (non-Javadoc)
  296.          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  297.          */
  298.         public void actionPerformed(ActionEvent e) {
  299.                 String source = e.getActionCommand();
  300.                 if(source.equals("Codifica")) {
  301.                          value = chooser.showDialog(this, "Codifica");
  302.                          f = 0;
  303.                 } else
  304.                 if(source.equals("Decodifica")) {
  305.                          value = chooser.showDialog(this, "Decodifica");
  306.                          f = 1;                
  307.                 } else
  308.                 if(source.equals("Esci")) {
  309.                         System.exit(0);
  310.                 } else
  311.                 if(source.equals("About")) {
  312.                         JOptionPane.showMessageDialog(this, "JavaCripter version 1.1 by Netarrow", "About JavaCripter", JOptionPane.INFORMATION_MESSAGE);
  313.                         return;
  314.                 }
  315.                 if(value == JFileChooser.APPROVE_OPTION) {
  316.                         document = new Document(chooser.getSelectedFile(), f);
  317.                         document.show();
  318.                 }
  319.         }
  320. }
  321.  
  322. class IllegalPasswordException extends Exception {
  323.        
  324.         public String toString() {
  325.                 return "La password deve esser maggiore di 0";
  326.         }
  327.        
  328. }