Java实现对称加密DES和AES的示例代码

 更新时间:2023年04月06日 09:34:57   作者:绘绘~  
这篇文章主要介绍了如何使用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的资料请关注脚本之家其它相关文章!

相关文章

  • 基于Spring开发之自定义标签及其解析

    基于Spring开发之自定义标签及其解析

    Spring框架是现在Java最流行的开源框架之一,需要实现一些自定义的标签,主要是方便使用我们框架的人能够快速、简单进行配置,有兴趣的可以了解一下。
    2017-04-04
  • Spring Boot conditional注解用法详解

    Spring Boot conditional注解用法详解

    这篇文章主要介绍了Spring Boot conditional注解用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java并发程序刺客之假共享的原理及复现

    Java并发程序刺客之假共享的原理及复现

    前段时间在各种社交平台“雪糕刺客”这个词比较火,而在并发程序中也有一个刺客,那就是假共享。本文将通过示例详细讲解假共享的原理及复现,需要的可以参考一下
    2022-08-08
  • Java并发编程示例(七):守护线程的创建和运行

    Java并发编程示例(七):守护线程的创建和运行

    这篇文章主要介绍了Java并发编程示例(七):守护线程的创建和运行,在本节示例中,我们将创建两个线程,一个是普通线程,向队列中写入事件,另外一个是守护线程,清除队列中的事件,需要的朋友可以参考下
    2014-12-12
  • 使用注解开发SpringMVC详细配置教程

    使用注解开发SpringMVC详细配置教程

    这篇文章主要介绍了使用注解开发SpringMVC详细配置教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • java编程实现根据EXCEL列名求其索引的方法

    java编程实现根据EXCEL列名求其索引的方法

    这篇文章主要介绍了java编程实现根据EXCEL列名求其索引的方法,涉及Java元素遍历与数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • struts2中通过json传值解决乱码问题的实现方法

    struts2中通过json传值解决乱码问题的实现方法

    这篇文章主要介绍了struts2中通过json传值解决乱码问题的实现方法,涉及js编码及java解码的相关操作技巧,需要的朋友可以参考下
    2016-06-06
  • 浅谈Java中的LinkedHashSet哈希链表

    浅谈Java中的LinkedHashSet哈希链表

    这篇文章主要介绍了浅谈Java中的LinkedHashSet哈希链表,LinkedHashSet 是 Java 中的一个集合类,它是 HashSet 的子类,并实现了 Set 接口,与 HashSet 不同的是,LinkedHashSet 保留了元素插入的顺序,并且具有 HashSet 的快速查找特性,需要的朋友可以参考下
    2023-09-09
  • gateway与spring-boot-starter-web冲突问题的解决

    gateway与spring-boot-starter-web冲突问题的解决

    这篇文章主要介绍了gateway与spring-boot-starter-web冲突问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 详解Java Selenium中的键盘控制操作

    详解Java Selenium中的键盘控制操作

    这篇文章主要为大家介绍了如何使用java代码利用Selenium 控制浏览器中需要用到的键盘操作。文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-01-01

最新评论