使用jframe编写一个base64加密解密工具

该工具可以使用exe4j来打包成exe工具(如何打包自己百度)

先上截图功能

运行main方法后,会弹出如下窗口

输入密文

 

然后点击解密,在点格式化

 

 

 

 

代码分享

 

package tools;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.codec.binary.Base64;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * TODO(用一句话描述这个类做什么用)。
 * 
 * <pre>
 * 构建组:toJar
 * 作者:chenrd
 * 邮箱:1658219123@qq.com
 * 日期:2019-3-7-下午3:06:58
 * 版权:个人所有
 * </pre>
 * 
 * Base64 加密解密工具
 */
public class ChenrdBase64 extends JFrame {
    private static final long serialVersionUID = 1L;
    Base64 base64 = new Base64();
    JPanel desktop = new JPanel();
    JPanel jfr1 = new JPanel();
    JPanel btnAllPanel = new JPanel();

    JButton incodeBtn = new JButton("加密");
    JButton decodeBtn = new JButton("解密");
    JButton formatBtn = new JButton("格式化");

    JTextArea inputMsg = new JTextArea("", 30, 50);

    public ChenrdBase64() {
        super("base64-chenrd");
        this.add(desktop);
        this.setBounds(0, 0, 900, 600);
        this.setLocationRelativeTo(null);
        // 关闭该窗口的时候不用关闭主窗口,所以要注释下行代码
        // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 按平台默认的方式把添加的窗口有序的排列,不然子窗口挡住了父窗口
        this.setLocationByPlatform(true);
        this.setVisible(true);
        init();
    }

    public void init() {
        inputMsg.setLineWrap(true);// 激活自动换行功能
        inputMsg.setWrapStyleWord(true);// 激活断行不断字功能
        JScrollPane js = new JScrollPane(inputMsg);

        jfr1.setLayout(new BorderLayout());
        jfr1.add(js, BorderLayout.NORTH);

        // 设置组件垂直排列
        // btnAllPanel.setLayout(new BoxLayout(btnAllPanel, BoxLayout.X_AXIS));
        btnAllPanel.add(incodeBtn);
        btnAllPanel.add(decodeBtn);
        btnAllPanel.add(formatBtn);

        jfr1.add(btnAllPanel, BorderLayout.SOUTH);
        desktop.add(jfr1, 0);

        // 换行
        inputMsg.setSelectedTextColor(Color.BLUE);
        // 激活自动换行功能
        inputMsg.setLineWrap(true);
        // 激活断行不断字功能
        inputMsg.setWrapStyleWord(true);
        // 给按钮添加点击事件
        incodeBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    String msg = inputMsg.getText();
                    inputMsg.setText(new String(Base64.encodeBase64(msg.getBytes("utf-8"))));
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
            }
        });
        // 给按钮添加点击事件
        decodeBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String msg = inputMsg.getText();
                try {
                    inputMsg.setText(new String(base64.decode(msg.getBytes("utf-8"))));
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }

            }
        });
        // 给按钮添加点击事件
        formatBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 格式化输出格式(使用dom4j格式化)
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("utf-8");
                StringWriter writer = new StringWriter();
                // 格式化输出流
                XMLWriter xmlWriter = new XMLWriter(writer, format);
                // 将document写入到输出流
                try {
                    String text = inputMsg.getText();
                    Document doc = DocumentHelper.parseText(text);
                    xmlWriter.write(doc);
                    inputMsg.setText(writer.toString());
                    System.out.println(writer.toString());
                    xmlWriter.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        ChenrdBase64 my64 = new ChenrdBase64();
        my64.validate();// 刷新所有子组件
    }
}

 

posted @ 2019-03-07 15:36  爱跳舞的程序员  阅读(436)  评论(0编辑  收藏  举报