项目文档见我上传的资源
点击这里下载资源
package xyz.slienceme;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Encrypt extends javax.swing.JFrame {
public Encrypt() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
savePathTxt = new javax.swing.JTextField();
savePathBtn = new javax.swing.JButton();
filePathBtn = new javax.swing.JButton();
SimEncryBtn = new javax.swing.JButton();
ComDecryBtn = new javax.swing.JButton();
SimDecryBtn = new javax.swing.JButton();
ComEncryBtn = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
filePathTxt = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
savePathTxt.setToolTipText("");
savePathTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savePathTxtActionPerformed(evt);
}
});
savePathBtn.setText("保存路径:");
savePathBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savePathBtnActionPerformed(evt);
}
});
filePathBtn.setText("文件路径:");
filePathBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
filePathBtnActionPerformed(evt);
}
});
SimEncryBtn.setText("简单加密");
SimEncryBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SimEncryBtnActionPerformed(evt);
}
});
ComDecryBtn.setText("复杂解密");
ComDecryBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ComDecryBtnActionPerformed(evt);
}
});
SimDecryBtn.setText("简单解密");
SimDecryBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SimDecryBtnActionPerformed(evt);
}
});
ComEncryBtn.setText("复杂加密");
ComEncryBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ComEncryBtnActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("微软雅黑", 1, 18));
jLabel1.setText("文件加密小助手");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(filePathBtn)
.addComponent(savePathBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(filePathTxt, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(savePathTxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(209, 209, 209)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(75, 75, 75)
.addComponent(SimEncryBtn)
.addGap(27, 27, 27)
.addComponent(SimDecryBtn)
.addGap(18, 18, 18)
.addComponent(ComEncryBtn)
.addGap(18, 18, 18)
.addComponent(ComDecryBtn)))
.addContainerGap(57, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jLabel1)
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(savePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(savePathBtn))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(filePathBtn)
.addComponent(filePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SimEncryBtn)
.addComponent(SimDecryBtn)
.addComponent(ComEncryBtn)
.addComponent(ComDecryBtn))
.addContainerGap(89, Short.MAX_VALUE))
);
pack();
}
private void savePathTxtActionPerformed(java.awt.event.ActionEvent evt) {
}
private void filePathBtnActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(0);
jfc.setCurrentDirectory(new File("D://1Amycode//Java//CourseDesign"));
int state = jfc.showOpenDialog(null);
if (state == 1) {
return;
} else {
File f = jfc.getSelectedFile();
filePathTxt.setText(f.getAbsolutePath());
}
}
private void SimEncryBtnActionPerformed(java.awt.event.ActionEvent evt) {
String newpath = savePathTxt.getText().trim();
System.out.println("SimEncryBtnActionPerformed=" + newpath);
String oldpath = filePathTxt.getText().trim();
System.out.println("SimEncryBtnActionPerformed=" + oldpath);
if (newpath.equals("")) {
JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
} else if (filePathTxt.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
} else {
File file = new File(oldpath);
String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
System.out.println("filetype=" + filetype);
String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
System.out.println("fileprefix=" + fileprefix);
String newFilepath = newpath + "\\"+ "SimEncrypt" + fileprefix + filetype;
System.out.println("newFilepath=" + newFilepath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
}
}
try {
SimEncryption(oldpath, newFilepath);
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "简单加密完成!", "提示", 2);
}
}
private void savePathBtnActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(1);
jfc.setCurrentDirectory(new File("D://1Amycode//Java//CourseDesign"));
int state = jfc.showOpenDialog(null);
if (state == 1) {
return;
} else {
File f = jfc.getSelectedFile();
savePathTxt.setText(f.getAbsolutePath());
}
}
private void ComDecryBtnActionPerformed(java.awt.event.ActionEvent evt) {
String newpath = savePathTxt.getText().trim();
System.out.println("ComEncryBtnActionPerformed=" + newpath);
String oldpath = filePathTxt.getText().trim();
System.out.println("ComEncryBtnActionPerformed=" + oldpath);
if (newpath.equals("")) {
JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
} else if (filePathTxt.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
} else {
File file = new File(oldpath);
String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
System.out.println("filetype=" + filetype);
String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
System.out.println("fileprefix=" + fileprefix);
String newFilepath = newpath + "\\"+ "ComDe_" + fileprefix + filetype;
System.out.println("newFilepath=" + newFilepath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
}
}
try {
ComDecryption(oldpath, newFilepath);
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "复杂解密完成!", "提示", 2);
}
}
private void SimDecryBtnActionPerformed(java.awt.event.ActionEvent evt) {
String newpath = savePathTxt.getText().trim();
System.out.println("SimEncryBtnActionPerformed=" + newpath);
String oldpath = filePathTxt.getText().trim();
System.out.println("SimEncryBtnActionPerformed=" + oldpath);
if (newpath.equals("")) {
JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
} else if (filePathTxt.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
} else {
File file = new File(oldpath);
String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
System.out.println("filetype=" + filetype);
String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
System.out.println("fileprefix=" + fileprefix);
String newFilepath = newpath + "\\"+ "SimDe_" + fileprefix+ filetype;
System.out.println("newFilepath=" + newFilepath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
}
}
creatPath(newpath);
try {
SimDecryption(oldpath, newFilepath);
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "简单解密完成!", "提示", 2);
}
}
private void ComEncryBtnActionPerformed(java.awt.event.ActionEvent evt) {
String newpath = savePathTxt.getText().trim();
System.out.println("ComEncryBtnActionPerformed=" + newpath);
String oldpath = filePathTxt.getText().trim();
System.out.println("ComEncryBtnActionPerformed=" + oldpath);
if (newpath.equals("")) {
JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
} else if (filePathTxt.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
} else {
File file = new File(oldpath);
String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
String newFilepath = newpath + "\\"+ "ComEncrypt" + fileprefix + filetype;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
}
}
creatPath(newpath);
try {
ComEncryption(oldpath, newFilepath);
} catch (IOException ex) {
Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "复杂加密完成!", "提示", 2);
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Encrypt().setVisible(true);
}
});
}
public void creatPath(String oldpath) {
String temppath = oldpath.replace(oldpath.substring(oldpath.lastIndexOf("\\"), oldpath.length()), "");
File file = new File(temppath);
if (!file.exists()) {
file.mkdir();
System.out.println("路径不存在,但是已经成功创建了" + temppath);
}
}
public void SimEncryption(String oldpath, String newpath) throws IOException {
File file = new File(oldpath);
FileInputStream inputFile = new FileInputStream(file);
FileWriter fw = new FileWriter(newpath);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
for (int i = 0; i < buffer.length; i++) {
buffer[i] += 3;
fw.write(buffer[i]);
}
fw.close();
}
public void SimDecryption(String oldpath, String newpath) throws IOException {
File file = new File(oldpath);
FileInputStream inputFile = new FileInputStream(file);
FileWriter fw = new FileWriter(newpath);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
for (int i = 0; i < buffer.length; i++) {
buffer[i] -= 3;
fw.write(buffer[i]);
}
fw.close();
}
public void ComEncryption(String oldpath, String newpath) throws IOException {
File file = new File(oldpath);
FileInputStream inputFile = new FileInputStream(file);
FileWriter fw = new FileWriter(newpath);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
String string = new String(buffer);
String result = "";
System.out.println("string="+ string.toString());
for (int i = 0; i < string.length(); i++) {
char x = string.toString().charAt(i);
if(x >= 'a' && x <= 'm'){fw.write((char)(x-3));}
else if(x >= 'n' && x <= 'z'){fw.write((char)(x+5));}
else if(x >= 'A' && x <= 'M'){fw.write((char)(x-4));}
else if(x >= 'N' && x <= 'Z'){fw.write((char)(x+3));}
else if(x>='0' && x<='5'){fw.write((char)(x-4));}
else if(x>='6' && x<='9'){fw.write((char)(x+3));}
else{fw.write((char)(x));}
}
fw.close();
}
public void ComDecryption(String oldpath, String newpath) throws IOException {
File file = new File(oldpath);
FileInputStream inputFile = new FileInputStream(file);
FileWriter fw = new FileWriter(newpath);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
String string = new String(buffer);
System.out.println("string="+string.toString());
for (int i = 0; i < string.length(); i++) {
char x = string.charAt(i);
if(x >= 'a'-3 && x <= 'm'-3){fw.write((char)(x+3));}
else if(x >= 'n'+5 && x <= 'z'+5){fw.write((char)(x-5));}
else if(x >= 'A'-4 && x <= 'M'-4){fw.write((char)(x+4));}
else if(x >= 'N'+3 && x <= 'Z'+3){fw.write((char)(x-3));}
else if(x >= '0'-4 && x <= '5'-4){fw.write((char)(x+4));}
else if(x >= '6'+3 && x <= '9'+3){fw.write((char)(x-3));}
else {fw.write((char)(x));}
}
fw.close();
}
private javax.swing.JButton ComDecryBtn;
private javax.swing.JButton ComEncryBtn;
private javax.swing.JButton SimDecryBtn;
private javax.swing.JButton SimEncryBtn;
private javax.swing.JButton filePathBtn;
private javax.swing.JTextField filePathTxt;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton savePathBtn;
private javax.swing.JTextField savePathTxt;
}