java文件加密和解密的区别_压缩包解密软件

(85) 2024-07-19 21:01:03

Java文件加密和解密

本软件的制作原理是:文件的读写,拷贝。通过从一个文件中读取字节,在写入时加上一个偏移量来实现加密功能,解密则返过来,即减去一个相同的偏移量。

本软件的优点是:1.实现了大文件的加密,对视频,歌曲,文本文件等都是可以的。2.大文件加密的时候,加了线程处理,不会造成按钮按下去后,要到加密或解密完成时才能弹起,给用户造成不便。3.软件的基本健壮性已经实现,加密或解密完成时,会弹出一个提示窗口,若没弹出,则代表没有完成加密或解密。4,.软件是使用了2个下拉框来实现的,方便用户使用,输入的加密的码的范围是00~99,一共100个,即激活成功教程率为1%。5.本软件是把用户的加密的码写入了用户的文件中,待到用户解密时,就会获取那个加入的加密的码,与用户输入的解密的码相比较,这实现了多个文件的压缩。6.本软件新加了进度条,能反映加密过程。

本软件有待升级的地方:1.加密解密大文件是速度较慢,请耐心等待。2.加密或解密是针对原文件的,若是加密失败,原文件会被破坏,用户使用时,一定要先备份,先试一下加密和解密是否成功,若是成功了,再对原文件加密。

 

接下来附上源代码:

这是加密解密和进度条界面的代码:

package ctong17; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JTextField; public class FileLock { // private static int rate; private static JProgressBar jpr = new JProgressBar(); private static FileInputStream fis; private static JComboBox box,box1,box2,box3; private static File f,file,file1 ; private static JTextField jt1,jt3; /** * 文件加密界面 */ public static void init_ya(){ final JFrame jf_ya=new JFrame(); jf_ya.setTitle("文件加密系统"); jf_ya.setLocation(530,175); jf_ya.setResizable(false); jf_ya.setSize(300, 300); jf_ya.setIconImage(new ImageIcon("4.gif").getImage());//添加方框的图标 JPanel center = new JPanel(); center.setLayout(new GridLayout(2,3)); JPanel south = new JPanel(); JButton btn_ya = new JButton("开始加密(Enter)"); JButton btn_fanhui = new JButton("返回(Backspace)"); JButton btn_xuanze = new JButton("选择"); //这是下拉框 box =new JComboBox(); box.addItem("0"); box.addItem("1"); box.addItem("2"); box.addItem("3"); box.addItem("4"); box.addItem("5"); box.addItem("6"); box.addItem("7"); box.addItem("8"); box.addItem("9"); box1 =new JComboBox(); box1.addItem("0"); box1.addItem("1"); box1.addItem("2"); box1.addItem("3"); box1.addItem("4"); box1.addItem("5"); box1.addItem("6"); box1.addItem("7"); box1.addItem("8"); box1.addItem("9"); jt1 = new JTextField(); JLabel jl1 = new JLabel("选择加密文件:",JLabel.CENTER); JLabel jl2 = new JLabel("加密码:",JLabel.CENTER); JLabel jl3=new JLabel(new ImageIcon("6.gif")); jl3.setPreferredSize(new Dimension(30,180)); center.add(jl1); center.add(jt1); center.add(btn_xuanze); center.add(jl2); center.add(box); center.add(box1); south.add(btn_ya); south.add(btn_fanhui); jf_ya.add(jl3,BorderLayout.NORTH); jf_ya.add(center,BorderLayout.CENTER); jf_ya.add(south,BorderLayout.SOUTH); jf_ya.setVisible(true); //返回按钮 btn_fanhui.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jf_ya.setVisible(false); init_First(); } }); //选择文件按钮 btn_xuanze.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //打开文件选择器 JFileChooser jc = new JFileChooser(); jc.showOpenDialog(jf_ya); file = jc.getSelectedFile(); //获取文件的绝对路径 jt1.setText(file.getAbsolutePath()); } }); //加密按钮 btn_ya.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //这个文件是中转文件,用来缓冲的 // f = new File(file.toURI()); f= new File("1.txt"); //线程,处理加密文件过程 YaThread yt = new YaThread(jpr, file,f, jt1, box, box1); yt.start(); } }); } /* * 这是主的界面 */ public static void init_First(){ final JFrame jf_first = new JFrame("文件加密系统"); jf_first.setSize(300,300); jf_first.setLocation(530,175); jf_first.setResizable(false); jf_first.setDefaultCloseOperation(3); jf_first.setIconImage(new ImageIcon("4.gif").getImage());//添加方框的图标 JPanel pa = new JPanel(); pa.setLayout(new GridLayout(1,2)); JButton btn_ya = new JButton("加密"); JButton btn_jie = new JButton("解密"); JLabel jl3=new JLabel(new ImageIcon("1.gif")); jl3.setPreferredSize(new Dimension(30,240)); pa.add(btn_ya); pa.add(btn_jie); jf_first.add(jl3,BorderLayout.NORTH); jf_first.add(pa,BorderLayout.CENTER); jf_first.setVisible(true); //解密按钮 btn_jie.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { init_jie(); jf_first.setVisible(false); } }); //加密按钮 btn_ya.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { init_ya(); jf_first.setVisible(false); } }); } /* * 这是解密的界面 */ public static void init_jie(){ final JFrame jf_jie=new JFrame("文件解密系统"); jf_jie.setLocation(530,175); jf_jie.setResizable(false); jf_jie.setSize(300, 300); jf_jie.setIconImage(new ImageIcon("4.gif").getImage());//添加方框的图标 JPanel center = new JPanel(); //中间面板设置表格布局 center.setLayout(new GridLayout(2,3)); JPanel south = new JPanel(); JButton btn_jie = new JButton("开始解密(Enter)"); JButton btn_fanhui = new JButton("返回(Backspace)"); JButton btn_xuanze = new JButton("选择"); //这是下拉框 box2 =new JComboBox(); box2.addItem("0"); box2.addItem("1"); box2.addItem("2"); box2.addItem("3"); box2.addItem("4"); box2.addItem("5"); box2.addItem("6"); box2.addItem("7"); box2.addItem("8"); box2.addItem("9"); box3 =new JComboBox(); box3.addItem("0"); box3.addItem("1"); box3.addItem("2"); box3.addItem("3"); box3.addItem("4"); box3.addItem("5"); box3.addItem("6"); box3.addItem("7"); box3.addItem("8"); box3.addItem("9"); jt3 = new JTextField(6); JLabel jl1 = new JLabel("选择解密文件:",JLabel.CENTER); JLabel jl2 = new JLabel("解密码:",JLabel.CENTER); JLabel jl3=new JLabel(new ImageIcon("8.gif")); jl3.setPreferredSize(new Dimension(30,180)); center.add(jl1); center.add(jt3); center.add(btn_xuanze); center.add(jl2); center.add(box2); center.add(box3); south.add(btn_jie); south.add(btn_fanhui); jf_jie.add(jl3,BorderLayout.NORTH); jf_jie.add(center,BorderLayout.CENTER); jf_jie.add(south,BorderLayout.SOUTH); jf_jie.setVisible(true); //选择文件的按钮 btn_xuanze.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //文件选择器,选择解密文件 JFileChooser jc = new JFileChooser(); jc.showOpenDialog(jf_jie); file1 = jc.getSelectedFile(); //获取文件的绝对路径,显示 jt3.setText(file1.getAbsolutePath()); } }); //返回按钮 btn_fanhui.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jf_jie.setVisible(false); init_First(); } }); //解密按钮 btn_jie.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //线程,用来处理解密文件的过程 JieThread jt = new JieThread(jpr, fis, f, jt3, box2, box3,file1); jt.start(); } }); } // public FileLock(int rate){ // this.rate=rate; // } //进度条界面 public static void JProgress_Bar(){ JFrame jf = new JFrame("进度"); jf.setSize(300,90); jf.setLocation(530,435); jf.setAlwaysOnTop(true); jpr.setMinimum(0); jpr.setPreferredSize(new Dimension(200,30)); jpr.setBackground(Color.WHITE); jpr.setForeground(Color.BLUE); jpr.setStringPainted(true); jf.setLayout(new FlowLayout()); jf.add(jpr); jf.setVisible(true); } public static void main(String[] args) { init_First(); } } 

 以下分别是加密,解密的线程(用来处理加密 解密):

 

package ctong17; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JTextField; public class YaThread extends Thread{ private static JProgressBar jpr; private static int rate=0; private static int count=0,count1=0; private File f,file; private static JTextField jt1; private static FileInputStream fis; private static FileOutputStream fos; private static JComboBox box,box1; private static int t1,t2; //构造函数,把需要的数据传输过来 public YaThread(JProgressBar jpr,File file,File f,JTextField jt1,JComboBox box,JComboBox box1){ this.jpr=jpr; this.file=file; this.f=f; this.jt1=jt1; this.box=box; this.box1=box1; } public void run(){ if(jt1.getText().isEmpty()) JOptionPane.showMessageDialog(null, "请选择文件!"); else{ new FileLock().JProgress_Bar(); count = (int)file.length(); count1 = count; jpr.setMaximum(2*count1); jiami(jt1.getText(),f.getAbsolutePath()); copy(f.getAbsolutePath(),jt1.getText()); //删除缓冲的文件 f.delete(); JOptionPane.showMessageDialog(null, "加密完成!"); } } public static void jiami(String filename,String newname ){ try { final int t=count1; t1=Integer.parseInt(box.getSelectedItem().toString()); t2=Integer.parseInt(box1.getSelectedItem().toString()); fis =new FileInputStream(filename); fos =new FileOutputStream(newname); //文件拷贝过程 //把用户输入的加密的码先保存在文件中,以便在以后解密时获取其的加密的码,与用户输入的解密的码比较 fos.write(t1); fos.write(t2); int n =fis.read(); while (n !=-1){ //可以进行文件的加密和解密 fos.write(n+10*t1+t2); rate=t-(--count); jpr.setValue(rate); n=fis.read(); } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } //文件拷贝方法 public static void copy(String from,String to){ try { fis =new FileInputStream(from); fos =new FileOutputStream(to); //文件拷贝过程 int n =fis.read(); while (n !=-1){ //可以进行文件的加密和解密 fos.write(n); jpr.setValue(rate+(++count)); n=fis.read(); } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } 

 ************************************

 

package ctong17; import java.awt.HeadlessException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JTextField; public class JieThread extends Thread{ private static JProgressBar jpr; private static int rate=0; private static int count=0,count1=0; private static FileInputStream fis; private static FileOutputStream fos; private static JComboBox box2,box3; private static int t3,t4; private File f,file1; private static JTextField jt3; public JieThread(JProgressBar jpr,FileInputStream fis,File f,JTextField jt3,JComboBox box2,JComboBox box3,File file1){ this.jpr=jpr; this.fis=fis; this.f=f; this.jt3=jt3; this.box2=box2; this.box3=box3; this.file1=file1; } public void run(){ if(jt3.getText().isEmpty()) JOptionPane.showMessageDialog(null, "请选择文件!"); else{ try { fis = new FileInputStream(jt3.getText()); //从文件中读取加密的码,与解密的码比较 if(fis.read()==Integer.parseInt(box2.getSelectedItem().toString())&&fis.read()==Integer.parseInt(box3.getSelectedItem().toString())){ //创建缓冲文件,如果各位同志没有F盘请自改,之所以不改为C盘是因为一般的用户机子里的C盘不允许访问 new FileLock().JProgress_Bar(); f= new File("1.txt"); // f= new File(file1.toURI()); count = (int)file1.length(); count1 = count; jpr.setMaximum(2*count1-4); jiemi(jt3.getText(),f.getAbsolutePath()); copy(f.getAbsolutePath(),jt3.getText()); //删除缓冲文件 f.delete(); JOptionPane.showMessageDialog(null, "解密完成!"); }else JOptionPane.showMessageDialog(null, "解密码不正确,请重新输入"); } catch (NumberFormatException e1) { e1.printStackTrace(); } catch (HeadlessException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } } public static void jiemi(String filename,String newname){ try { final int t=count1; t3=Integer.parseInt(box2.getSelectedItem().toString()); t4=Integer.parseInt(box3.getSelectedItem().toString()); fis =new FileInputStream(filename); fos =new FileOutputStream(newname); //文件拷贝过程 //跳过2个字节,即一个字的大小 fis.skip(2); int n =fis.read(); while (n !=-1){ //可以进行文件的加密和解密 fos.write(n-10*t3-t4); rate=t-(--count); jpr.setValue(rate); n=fis.read(); } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public static void copy(String from,String to){ try { fis =new FileInputStream(from); fos =new FileOutputStream(to); //文件拷贝过程 int n =fis.read(); while (n !=-1){ //可以进行文件的加密和解密 fos.write(n); jpr.setValue(rate+(++count)); n=fis.read(); } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } 

 以下是操作的图片:

java文件加密和解密的区别_压缩包解密软件 (https://mushiming.com/)  第1张
 
 
java文件加密和解密的区别_压缩包解密软件 (https://mushiming.com/)  第2张
 
java文件加密和解密的区别_压缩包解密软件 (https://mushiming.com/)  第3张
 
 

 

 

文件加密的可执行程序已发,感兴趣的同学可以下载!欢迎留言,我会积极采纳你们的宝贵建议或意见。

THE END

发表回复