package net.sourceforge.jfilecrypt;
//Download to http://www.codefans.net
//~--- JDK imports ------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.security.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import java.util.zip.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.*;
import javax.swing.event.*;
//~--- classes ----------------------------------------------------------------
public class jFileCryptFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JButton btChoose = new JButton();
private JProgressBar pbCryptProgress = new JProgressBar();
private JPasswordField pfPassword = new JPasswordField();
private JLabel lbAlgorithm = new JLabel();
private JLabel lbSource = new JLabel();
private JTextField tfSourceFile = new JTextField();
private JLabel lbPassword = new JLabel();
private JComboBox cmbAlgorithm = new JComboBox();
private JComboBox cmbCompressionLevel = new JComboBox();
private JButton btDecrypt = new JButton();
private JButton btEncrypt = new JButton();
private JFileChooser fchooser = new JFileChooser();
private JCheckBox chbUseCompression = new JCheckBox();
private Properties prop = new Properties();
private long read = 0;
private int start_for_entry_path = 0;
private String dir_for_encrypted ="";
private long size_all_files = 0;
//~--- constructors -------------------------------------------------------
/**
* This is the main-constructor. It calls the @see jbinit() method.
*/
public jFileCryptFrame() {
super();
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
//~--- methods ------------------------------------------------------------
private void decrypt(final File f) {
if(f.isDirectory()) {
File[] files = f.listFiles();
for(int i = 0; i<files.length; i++) {
decrypt(files[i]);
}
} else {
new Thread() {
public void run() {
try {
String kind = (String) cmbAlgorithm.getSelectedItem(); // Which algorithm?
int index = kind.indexOf("(");
kind = kind.substring(0, index);
Cipher c = Cipher.getInstance(kind);
Key k = new SecretKeySpec(
new String(pfPassword.getPassword()).getBytes(), kind);
c.init(Cipher.DECRYPT_MODE, k);
String filename = f.getCanonicalPath();
if(filename.endsWith(prop.getProperty(kind))) {
filename = filename.substring(
0, filename.length()
- prop.getProperty(kind).length()); // -suffix
} else {
displayError("Error: Wrong file chosen",
"Ending of file and chosen algorithm do not match! Filename must end with: " + prop.getProperty(kind));
return;
}
FileInputStream fis =
new FileInputStream(f.getCanonicalPath());
FileOutputStream fos = new FileOutputStream(filename);
CipherInputStream cis = new CipherInputStream(fis, c);
byte[] buffer = new byte[0xFFFF];
final long size = f.length();
pbCryptProgress.setMaximum((int) size);
for (int len; (len = cis.read(buffer)) != -1;) {
fos.write(buffer, 0, len);
read += len;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pbCryptProgress.setValue((int)read);
pbCryptProgress.repaint();
}
}); // Set Progress
}
cis.close();
fos.flush();
fos.close();
fis.close();
pbCryptProgress.setMaximum(100);
pbCryptProgress.setValue(0);
read = 0;
} catch (Exception x) {
x.printStackTrace();
}
}
}.start();
}
}
private void encrypt(final File f) {
if(f.isDirectory()) {
File[] files = f.listFiles();
for(int i = 0; i<files.length; i++) {
encrypt(files[i]);
}
} else {
new Thread() {
public void run() {
try {
String kind = (String) cmbAlgorithm.getSelectedItem(); // Which algorithm?
int index = kind.indexOf("(");
kind = kind.substring(0, index);
Cipher c = Cipher.getInstance(kind);
Key k = new SecretKeySpec(
new String(pfPassword.getPassword()).getBytes(), kind);
c.init(Cipher.ENCRYPT_MODE, k);
FileInputStream fis =
new FileInputStream(f.getCanonicalPath());
FileOutputStream fos =
new FileOutputStream(f.getCanonicalPath()
+ prop.getProperty(kind));
CipherOutputStream cos = new CipherOutputStream(fos, c);
final int size = (int) f.length();
byte[] buffer = new byte[0xFFFF];
pbCryptProgress.setMaximum(size);
for (int len; (len = fis.read(buffer)) != -1; ) {
cos.write(buffer, 0, len);
read += len;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pbCryptProgress.setValue((int)read);
pbCryptProgress.repaint();
}
}); // Set Progress
}
cos.flush();
cos.close();
fos.flush();
fos.close();
fis.close();
pbCryptProgress.setMaximum(100);
pbCryptProgress.setValue(0);
read = 0;
} catch (Exception x) {
x.printStackTrace();
}
}
}.start();
}
}
/**
* This method shows a @see javax.swing.JFileChooser and calls then
* the method @see setFileChosen(boolean b).
* @param e
*/
private void btChoose_actionPerformed(ActionEvent e) {
try {
if (fchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String path = fchooser.getSelectedFile().getCanonicalPath();
tfSourceFile.setText(path);
}
} catch (IOException ioex) {
}
}
/**
* This method is called by a click of btEncrypt. It calls
* the @see zip_encrypt(final file f, ZipOutputstream zos) or @see encrypt(final File f).
* @param e
*/
private void btEncrypt_actionPerformed(ActionEvent e) {
String path = tfSourceFile.getText();
if(! path.equals("")) { // File chosen?
File file = new File(path);
if(file.exists()) { // Does file exist?
if(chbUseCompression.isSelected()) {
String root_dire