由于经常有人用本人电脑,一些重要的机密文件不免被人看见,所以自己写了小的加密文件数据的小工具。此加密工具采用的是最简单的加密方法:把数据分别异或得出加密后的文件;

操作步骤:

 

 

 

 

 

源代码:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.
event .ActionEvent;
import java.awt.
event .ActionListener;
import java.awt.
event .ItemEvent;
import java.awt.
event .ItemListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;



import javax.swing.
* ;

public   class  ToolsTakePassword extends JFrame  {
    
private static final long serialVersionUID = 1L;
    
private JFileChooser jf;
    
private JMenuBar jmb;
    
private JMenu file;
    
private JMenuItem open;
    
private JMenuItem save;
    
private JMenuItem exit;
    
private JTextArea text;
    
private JScrollPane jsp;
    
private JPanel p;
    
private JButton takepassword;
    
private JCheckBox override;
    
private JLabel intru;
    
private JLabel currentfile;
    
private JLabel currentfilename;
    
private Font ft;
    
private boolean flag = true;
    
private File filesave = null;
    
private File newfilesave = null;
    
private JTextField codenum;
    
private JProgressBar jbr;
    
private int code;
    
private JButton refresh;
    
private JLabel inputcode;
    
private JLabel temp0;
    
private JLabel temp1;


    
public ToolsTakePassword() {
        super(
"--文件加密器--    作者:sharpyuce.");
        jf 
= new JFileChooser();
        jmb 
= new JMenuBar();
        file 
= new JMenu("File");
        open 
= new JMenuItem("Open");
        save 
= new JMenuItem("Save");
        exit 
= new JMenuItem("Exit");
        text 
= new JTextArea();
        jsp 
= new JScrollPane(text);
        p 
= new JPanel();
        codenum 
= new JTextField(5);
        codenum.setText(
"50");
        refresh 
= new JButton("刷新");
        takepassword 
= new JButton("加 密");
        
override = new JCheckBox();
        intru 
= new JLabel("是否覆盖原文件");
        currentfile 
= new JLabel("当前文件:");
        currentfilename 
= new JLabel("");
        temp0 
= new JLabel("                 ");
        temp1 
= new JLabel("                ");
        inputcode 
= new JLabel("输入加密码(输入一个整数值):");
        
        
this.init();
        
this.setSize(800600);
        
this.setVisible(true);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    @SuppressWarnings(
"unused")
    
private void init() {
        open.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                text.setText(
"");
                
new Open().start();
            }

        }
);
        file.add(open);
        file.add(save);
        exit.addActionListener(
new ActionListener(){
            
public void actionPerformed(ActionEvent e) {
                System.exit(
0);
            }

            
        }
);
        file.add(exit);
        jmb.add(file);
        
this.add(jmb, BorderLayout.NORTH);
        
this.add(jsp, BorderLayout.CENTER);
        takepassword.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                
new TakePassWord().start();
            }

        }
);
        p.add(inputcode);
        p.add(codenum);
        p.add(temp0);
        refresh.addActionListener(
new ActionListener(){
            
public void actionPerformed(ActionEvent e) {
                text.setText(
"");
                
new Refresh().start();
            }
    
        }
);
        p.add(refresh);
        p.add(temp1);
        p.add(takepassword);
        
override.addItemListener(new ItemListener(){
            
public void itemStateChanged(ItemEvent e) {
                flag 
=!flag;
            }

            
        }
);
        p.add(
override);
        p.add(intru);
        p.add(currentfile);
        p.add(currentfilename);
        
this.add(p, BorderLayout.SOUTH);
        refresh.setFont(ft);
        takepassword.setFont(ft);
        intru.setFont(ft);
        file.setFont(ft);
        save.setFont(ft);
        open.setFont(ft);
        exit.setFont(ft);
        currentfile.setFont(ft);
        currentfilename.setFont(ft);
        inputcode.setFont(ft);
    }

    
    
class Open extends Thread{
        
public void run(){
            jf.showOpenDialog(
null);
            filesave 
= jf.getSelectedFile();
            currentfilename.setText(filesave.getAbsolutePath());
            InputStream fis 
= null;
            
byte[] buff = new byte[1024];
            
try {
                fis
=new FileInputStream(filesave);
                
while (fis.available() > 0{
                    
int len = fis.read(buff);
                    String s 
= new String(buff, 0, len);
                    text.append(s);
                }

            }
 catch (IOException e1) {
                e1.printStackTrace();
            }
 finally {
                
if (fis != null{
                    
try {
                        fis.close();
                    }
 catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }

        }

    }


    
class TakePassWord extends Thread{
        
public void run(){
            code 
= Integer.parseInt(codenum.getText().trim());
            String s 
= text.getText();
            String[] ss 
= s.split(" ");
            FileOutputStream fr 
= null;
            
if(flag){
                jf.showSaveDialog(
null);
                newfilesave 
= jf.getSelectedFile();
                currentfilename.setText(newfilesave.getAbsolutePath());
            }

            
try {
                
if (flag) {
                    fr 
= new FileOutputStream(newfilesave);
                    filesave 
= newfilesave;
                }
 else {
                    fr 
= new FileOutputStream(filesave);
                    currentfilename.setText(filesave.getAbsolutePath());
                }

                
for (String t : ss) {
                    
byte[] buff = t.getBytes();
                    String n 
= " ";
                    
for (int i = 0; i < buff.length; i++{
                        fr.write(buff[i] 
^ code);
                    }

                    fr.write(n.getBytes());
                }

            }
 catch (IOException e1) {
                e1.printStackTrace();
            }
 finally {
                
if (fr != null{
                    
try {
                        fr.close();
                    }
 catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }

        }

    }

    
    
class Refresh extends Thread{
        
public void run(){
            InputStream fis 
= null;
            
byte[] buff = new byte[1024];
            
try {
                fis
=new FileInputStream(filesave);
                
while (fis.available() > 0{
                    
int len = fis.read(buff);
                    String s 
= new String(buff, 0, len);
                    text.append(s);
                }

            }
 catch (IOException e1) {
                e1.printStackTrace();
            }
 finally {
                
if (fis != null{
                    
try {
                        fis.close();
                    }
 catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }

        }

    }

    
public static void main(String[] args) {
        
new ToolsTakePassword();
    }


}

 

如果想要生成EXE文件:先下载jsmooth,使用这个工具可 制作exe程序

Skeleton Chooser下拉框中选择Windowed Wrapper Skeleton Properties : 在launch java app in the exe process 选项打勾 4. 点击Executable, 这一步是设置生成的exe文件的属性,包括名字,图标,路径 5. 设置完成后,继续下一选项,Application.Main class : 主运行类,有main()方法的类 Embedded jar : 就是选择你要制作成EXE文件的jar 5. 其他的选项如果不需要可不必设置,最后单击project下的compile 就OK了 , 可以在装有jre的机器上直接运行。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐