import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
*
* @author netarrow
*/
private int f;
private int ret;
this.f = f;
this.path = path;
dest
= new File((f
== 0 ? path.
toString() + ".jcc":path.
toString().
substring(0, path.
toString().
lastIndexOf(".jcc"))));
initComponents();
}
int count = 0, i = 0;
int key;
try {
do {
i = sf.read();
if(i != -1) {
count++;
df.write((i - key * count) ^ key);
}
} while(i != -1);
JOptionPane.
showMessageDialog(this,
"Decodifica riuscita (se il testo rimande incomprensibile la password è errata)",
"JavaCripter",
JOptionPane.
INFORMATION_MESSAGE);
setVisible(false);
return;
} finally {
count = 0;
sf.close();
df.close();
}
}
int count = 0, i = 0;
int key;
try {
if(key <= 0) throw new IllegalPasswordException();
do {
i = sf.read();
if(i != -1) {
count++;
df.write(((i ^ key) + key * count));
}
} while(i != -1);
setVisible(false);
setVisible(false);
return;
} finally {
count = 0;
df.close();
sf.close();
}
}
private void initComponents() {
getContentPane().setLayout(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jLabel2.setText("Inserire password(numerica)");
getContentPane().add(jLabel2);
jLabel2.setBounds(0, 50, jLabel2.getPreferredSize().width, jLabel2.getPreferredSize().height);
getContentPane().add(jTextField1);
jTextField1.setBounds(170, 50, 190, 22);
jButton2.setText("Ok");
jButton2.addActionListener(this);
getContentPane().add(jButton2);
jButton2.setBounds(60, 80, 90, 25);
jButton3.setText("Annulla");
jButton3.addActionListener(this);
getContentPane().add(jButton3);
jButton3.setBounds(270, 80, 90, 25);
switch(f) {
case 0:
setTitle("Codifica " + "\"" + path.getName() + "\"");
break;
case 1:
setTitle("Decodifica " + "\"" + path.getName() + "\"");
break;
}
setSize(400, 150);
win=getSize();
setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
}
if(e.getActionCommand().equalsIgnoreCase("ok")) {
switch(f) {
case 0:
try {
codifica(path, dest, jTextField1.getText());
setVisible(false);
return;
}
break;
case 1:
try {
decodifica(path, dest, jTextField1.getText());
return;
}
break;
}
} else
if(e.getActionCommand().equalsIgnoreCase("annulla")) {
setVisible(false);
}
}
}
/**
* @author netarrow
*/
int value;
/** Creates new form JFrame */
public JavaCripter() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
chooser.addActionListener(this);
getContentPane().setLayout(null);
exitForm(evt);
}
});
jButton1.setText("Codifica");
getContentPane().add(jButton1);
jButton1.setBounds(160, 30, 120, 25);
jButton2.setText("Decodifica");
getContentPane().add(jButton2);
jButton2.setBounds(0, 30, 120, 25);
jLabel1.setText("JavaCripter version 1.1");
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 10, 280, 16);
jMenu1.setText("File");
jMenuItem1.setText("Codifica");
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Decodifica");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("Esci");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("?");
jMenuItem4.setText("About");
jMenu2.add(jMenuItem4);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
setSize(286, 105);
setTitle("JavaCripter");
win=getSize();
setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
setResizable(false);
show();
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jMenuItem1.addActionListener(this);
jMenuItem2.addActionListener(this);
jMenuItem3.addActionListener(this);
jMenuItem4.addActionListener(this);
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm
(WindowEvent evt
) {//GEN-FIRST:event_exitForm
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) {
new JavaCripter().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private int f;
/** (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
String source
= e.
getActionCommand();
if(source.equals("Codifica")) {
value = chooser.showDialog(this, "Codifica");
f = 0;
} else
if(source.equals("Decodifica")) {
value = chooser.showDialog(this, "Decodifica");
f = 1;
} else
if(source.equals("Esci")) {
} else
if(source.equals("About")) {
JOptionPane.
showMessageDialog(this,
"JavaCripter version 1.1 by Netarrow",
"About JavaCripter",
JOptionPane.
INFORMATION_MESSAGE);
return;
}
document
= new Document(chooser.
getSelectedFile(), f
);
document.show();
}
}
}
class IllegalPasswordException
extends Exception {
return "La password deve esser maggiore di 0";
}
}