码迷,mamicode.com
首页 > 编程语言 > 详细

java编写Base64密码器

时间:2015-12-14 23:02:52      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

Base64加密算法,应用广泛,尤其是在电子邮件传输上,有很大的用途

用JAVA编写的程序代码如下

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class Base64Frame extends JFrame {

    private JPanel contentPane;
    private JButton btnNewButton;
    private JTextArea textArea;
    private JButton btnNewButton_1;
    private JTextArea textArea_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Base64Frame frame = new Base64Frame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Base64Frame() {
        setTitle("Base64密码器");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 505, 405);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        textArea = new JTextArea();
        textArea.setBounds(14, 13, 471, 136);
        contentPane.add(textArea);
        
        textArea_1 = new JTextArea();
        textArea_1.setBounds(14, 206, 471, 151);
        contentPane.add(textArea_1);
        
        btnNewButton = new JButton("编码");
        btnNewButton.setBounds(14, 166, 113, 27);
        contentPane.add(btnNewButton);
        
        btnNewButton_1 = new JButton("解码");
        btnNewButton_1.setBounds(372, 162, 113, 27);
        contentPane.add(btnNewButton_1);
        btnNewButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea_1.setText(Base64Util.encryptBase64(textArea.getText().getBytes()));
                
            }
        });
        btnNewButton_1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    textArea_1.setText(Base64Util.decryptBase64(textArea.getText()));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                
            }
        });
    }
}
import java.io.IOException;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;


public class Base64Util {
    
    public static String encryptBase64(byte[] data){
        return new BASE64Encoder().encode(data);//编码算法
    }
    
    public static String decryptBase64(String data) throws IOException{
        byte[] resultBytes = new BASE64Decoder().decodeBuffer(data);//data代表密文
        return new String(resultBytes);//返回字节数组的字符串形式
    }

}

其中的Base64Util工具类由极客学院提供;

其中需要sun.misc.BASE64Decode.jar  JDK开发包的支持;可以去JDK的官网下下载,也可以点击此链接下载

http://yunpan.cn/c3XmzH5Tmcg6b  访问密码 f450

java编写Base64密码器

标签:

原文地址:http://www.cnblogs.com/qq1402876003/p/5046527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!