温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java怎么实现对称加密DES和AES

发布时间:2023-05-06 11:11:20 来源:亿速云 阅读:131 作者:zzz 栏目:开发技术

本文小编为大家详细介绍“Java怎么实现对称加密DES和AES”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java怎么实现对称加密DES和AES”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实验内容和要求

采用Java实现采用对称密码算法的应用软件,所用算法包括DES算法和AES算法。要求该软件具有图形用户界面,能生成密钥,以及对字符串和文件进行加解密

参考代码

// 文件名: test01.java

import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class test01 extends JFrame implements ActionListener {
    private JFileChooser fileChooser = new JFileChooser();
    private JTextArea inputArea = new JTextArea(10, 40);
    private JTextArea outputArea = new JTextArea(10, 40);
    private JButton encryptButton = new JButton("加密");
    private JButton decryptButton = new JButton("解密");
    private JButton fileButton = new JButton("选择文件");
    private JComboBox<String> algorithmBox = new JComboBox<String>(new String[] {"DES", "AES"});
    private JLabel keyLabel = new JLabel("密钥:");
    private JTextField keyField = new JTextField(20);

    public test01() {
        super("对称加密算法实现");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        JPanel inputPanel = new JPanel();
        inputPanel.add(new JLabel("输入:"));
        inputPanel.add(new JScrollPane(inputArea));
        JPanel outputPanel = new JPanel();
        outputPanel.add(new JLabel("输出:"));
        outputPanel.add(new JScrollPane(outputArea));
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(encryptButton);
        buttonPanel.add(decryptButton);
        buttonPanel.add(fileButton);
        buttonPanel.add(algorithmBox);
        buttonPanel.add(keyLabel);
        buttonPanel.add(keyField);
        mainPanel.add(inputPanel);
        mainPanel.add(outputPanel);
        mainPanel.add(buttonPanel);
        encryptButton.addActionListener(this);
        decryptButton.addActionListener(this);
        fileButton.addActionListener(this);
        setContentPane(mainPanel);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == encryptButton) {
            encrypt();
        } else if (e.getSource() == decryptButton) {
            decrypt();
        } else if (e.getSource() == fileButton) {
            chooseFile();
        }
    }

    private void chooseFile() {
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                inputArea.setText("");
                String line = reader.readLine();
                while (line != null) {
                    inputArea.append(line);
                    line = reader.readLine();
                    if (line != null) {
                        inputArea.append("\n");
                    }
                }
                reader.close();
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "Error reading file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void encrypt() {
        try {
            String algorithm = (String) algorithmBox.getSelectedItem();
            String keyString = keyField.getText();
            byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);
            SecretKey key;
            if (algorithm.equals("DES")) {
                key = new SecretKeySpec(keyBytes, "DES");
            } else {
                key = new SecretKeySpec(keyBytes, "AES");
            }
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            String input = inputArea.getText();
            byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
            byte[] outputBytes = cipher.doFinal(inputBytes);
            String output = new String(outputBytes, StandardCharsets.UTF_8);
            outputArea.setText(output);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error encrypting: "
                    + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }


    private void decrypt() {
        try {
            String algorithm = (String) algorithmBox.getSelectedItem();
            String keyString = keyField.getText();
            byte[] keyBytes = keyString.getBytes();
            SecretKey key;
            if (algorithm.equals("DES")) {
                key = new SecretKeySpec(keyBytes, "DES");
            } else {
                key = new SecretKeySpec(keyBytes, "AES");
            }
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key);
            String input = inputArea.getText();
            byte[] inputBytes = input.getBytes();
            byte[] outputBytes = cipher.doFinal(inputBytes);
            String output = new String(outputBytes);
            outputArea.setText(output);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error decrypting: " +
                    e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

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

实现效果:

Java怎么实现对称加密DES和AES

读到这里,这篇“Java怎么实现对称加密DES和AES”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI