• java编写Base64密码器


    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

  • 相关阅读:
    状压DP【p1879】[USACO06NOV]玉米田Corn Fields
    Tarjan缩点+Spfa最长路【p3627】[APIO2009] 抢掠计划
    Tarjan缩点【p1726】上白泽慧音
    分层图【p4822】[BJWC2012]冻结
    Tarjan缩点+LCA【p2783】有机化学之神偶尔会做作弊
    线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle
    better-scroll踩坑合集
    在浏览器上安装 Vue Devtools工具
    无法执行vue初始化命令
    vue-cli创建第一个项目(用git bash解决上下键移动选择问题)
  • 原文地址:https://www.cnblogs.com/qq1402876003/p/5046527.html
Copyright © 2020-2023  润新知