📄 jfilecryptframe.java
字号:
}
);
btDecrypt.setText("Decrypt");
btDecrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btDecrypt_actionPerformed(e);
}
});
lbSource.setText("Source:");
lbSource.setVerticalAlignment(JLabel.CENTER);
lbSource.setHorizontalAlignment(JLabel.RIGHT);
lbAlgorithm.setText("Algorithm:");
lbAlgorithm.setVerticalAlignment(JLabel.CENTER);
lbAlgorithm.setHorizontalAlignment(JLabel.RIGHT);
lbPassword.setText("Password:");
lbPassword.setVerticalAlignment(JLabel.CENTER);
lbPassword.setHorizontalAlignment(JLabel.RIGHT);
cmbCompressionLevel.setEnabled(false);
prop.setProperty("Blowfish", ".blowfish");
prop.setProperty("DES", ".des");
prop.setProperty("TripleDES", ".3des");
prop.setProperty("AES", ".aes");
prop.setProperty("RC4", ".rc4");
cmbAlgorithm.addItem("Blowfish(optional length)");
cmbAlgorithm.addItem("DES(8)");
cmbAlgorithm.addItem("TripleDES(24)");
cmbAlgorithm.addItem("AES(16)");
cmbAlgorithm.addItem("RC4(optional length)");
for(int i=0;i<10;i++){
cmbCompressionLevel.addItem(new Integer(i));
}
fchooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// setup boxed layouts with panels
JPanel jpInput = new JPanel();
JPanel jpCompression = new JPanel();
JPanel jpCryptButtons = new JPanel();
JPanel jpProgress = new JPanel();
this.getContentPane().setLayout(new GridBagLayout());
this.getContentPane().add(jpInput, new GridBagConstraints(
0, 0, 1, 1, 1.0, 3.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,8,8,8), 0, 0
));
this.getContentPane().add(jpCompression, new GridBagConstraints(
0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0
));
this.getContentPane().add(jpCryptButtons, new GridBagConstraints(
0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,8,0,8), 0, 0
));
this.getContentPane().add(jpProgress, new GridBagConstraints(
0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,8,8,8), 0, 0
));
// add components to the panels
jpInput.setLayout(new GridBagLayout());
jpInput.add(lbSource, new GridBagConstraints(
0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0,0,0,8), 0, 0
));
jpInput.add(tfSourceFile, new GridBagConstraints(
1, 0, 1, 1, 3.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0,0,0,8), 0, 0
));
jpInput.add(btChoose, new GridBagConstraints(
2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0
));
jpInput.add(lbAlgorithm, new GridBagConstraints(
0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,0,0,8), 0, 0
));
jpInput.add(cmbAlgorithm, new GridBagConstraints(
1, 1, 1, 1, 3.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,0,0,8), 0, 0
));
jpInput.add(lbPassword, new GridBagConstraints(
0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,0,0,8), 0, 0
));
jpInput.add(pfPassword, new GridBagConstraints(
1, 2, 1, 1, 3.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(8,0,0,8), 0, 0
));
jpCompression.setLayout(new FlowLayout());
jpCompression.add(chbUseCompression);
jpCompression.add(new JLabel("Compression level: "));
jpCompression.add(cmbCompressionLevel);
GridLayout cryptButtonsGrid = new GridLayout(1, 2);
cryptButtonsGrid.setHgap(8);
jpCryptButtons.setLayout(cryptButtonsGrid);
jpCryptButtons.add(btEncrypt);
jpCryptButtons.add(btDecrypt);
jpProgress.setLayout(new GridLayout(1, 1));
jpProgress.add(pbCryptProgress);
}
private Vector getFileList(File file){
if(file.isDirectory()){
Vector vec = new Vector();
File[] list = file.listFiles();
for(int i=0; i < list.length; i++){
Vector v = getFileList(list[i]);
for(int j = 0; j < v.size(); j++){
vec.add(v.get(j));
}
}
return vec;
} else {
Vector vec = new Vector();
vec.add(file);
size_all_files += file.length();
return vec;
}
}
private void zip_decrypt(final ZipInputStream zis) {
new Thread() {
public void run() {
try {
ZipEntry entry = zis.getNextEntry();
if(entry == null){
pbCryptProgress.setMaximum(100);
read = 0;
pbCryptProgress.setValue(0);
return;
}
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 = dir_for_encrypted+entry.getName();
if (filename.endsWith(prop.getProperty(kind))) {
filename = filename.substring(
0, filename.length()
- prop.getProperty(kind).length()); // -suffix
}
createFiles(new File(filename));
FileOutputStream fos = new FileOutputStream(filename);
CipherInputStream cis = new CipherInputStream(zis, c);
byte[] buffer = new byte[0xFFFF];
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
}
fos.flush();
fos.close();
zip_decrypt(zis);
} catch (Exception x) {
x.printStackTrace();
}
}
}.start();
}
private void zipVectorEncrypt(final File f) {
new Thread() {
public void run() {
String kind = (String) cmbAlgorithm.getSelectedItem(); // Which algorithm?
int index = kind.indexOf("(");
Vector vec = getFileList(f);
kind = kind.substring(0, index);
Cipher c;
try {
c = Cipher.getInstance(kind);
Key k = new SecretKeySpec(new String(pfPassword.getPassword()).getBytes(), kind);
c.init(Cipher.ENCRYPT_MODE, k);
String zip_file_name;
if(f.isFile()) {
zip_file_name = f.getName().substring(0, f.getName().lastIndexOf("."))+".zip";
} else {
zip_file_name = f.getName()+".zip";
}
String zip_path = f.getAbsolutePath().substring(0,f.getAbsolutePath().length()-f.getName().length())+zip_file_name;
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip_path));
zos.setLevel(Integer.parseInt(cmbCompressionLevel.getSelectedItem().toString()));
pbCryptProgress.setMaximum((int) size_all_files);
for(int i=0; i < vec.size(); i++) {
File file = (File) vec.get(i);
if(!file.getName().equals(".DS_Store")){
String entry_path = file.getAbsolutePath().substring(start_for_entry_path)+prop.getProperty(kind);
ZipEntry entry = new ZipEntry(entry_path);
zos.putNextEntry(entry);
FileInputStream fis = new FileInputStream(file);
CipherOutputStream cos = new CipherOutputStream(new MyOutputStream(zos), c);
//final int size = fis.available();
byte[] buffer = new byte[0xFFFF];
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();
fis.close();
}
}
zos.finish();
zos.close();
pbCryptProgress.setMaximum(100);
read = 0;
pbCryptProgress.setValue((int)read);
}
catch(Exception x){
System.out.println(x);
}
}
}.start();
}
private void chbUseCompression_stateChanged(ChangeEvent e) {
if(chbUseCompression.isSelected()) {
cmbCompressionLevel.setEnabled(true);
} else {
cmbCompressionLevel.setEnabled(false);
}
}
private void createFiles(File f){
try {
if(!f.getParentFile().exists()){
createParents(f.getParentFile());
f.createNewFile();
}
//f.createNewFile();
} catch (IOException e) {
displayError("Error: Can't create file",
"Can't create File: " + f.getPath());
}
}
private void createParents(File file) {
if(!file.exists()){
createParents(file.getParentFile());
file.mkdir();
}
}
private boolean zip_Has_Only_One_File(File f) throws ZipException,
IOException {
ZipFile zip = new ZipFile(f);
int entries_count = 0;
Enumeration e = zip.entries();
for(; e.hasMoreElements() == true;){
e.nextElement();
entries_count++;
}
if(entries_count < 2){
return true;
}
else{
return false;
}
}
private class MyOutputStream extends BufferedOutputStream{
public void close() {
}
public MyOutputStream(OutputStream out){
super(out);
}
}
private void displayError(String title, String text) {
JOptionPane.showMessageDialog(this, text, title, JOptionPane.ERROR_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -