欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)對(duì)稱(chēng)加密DES和AES的示例代碼

 更新時(shí)間:2023年04月06日 09:34:57   作者:繪繪~  
這篇文章主要介紹了如何使用Java實(shí)現(xiàn)采用對(duì)稱(chēng)密碼算法的應(yīng)用軟件,所用算法包括DES算法和AES算法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

實(shí)驗(yàn)內(nèi)容和要求

采用Java實(shí)現(xiàn)采用對(duì)稱(chēng)密碼算法的應(yīng)用軟件,所用算法包括DES算法和AES算法。要求該軟件具有圖形用戶(hù)界面,能生成密鑰,以及對(duì)字符串和文件進(jìn)行加解密

參考代碼

// 文件名: 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("對(duì)稱(chēng)加密算法實(shí)現(xiàn)");
        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();
    }
}

實(shí)現(xiàn)效果:

大概就是這樣

以上就是Java實(shí)現(xiàn)對(duì)稱(chēng)加密DES和AES的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java對(duì)稱(chēng)加密DES AES的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 初識(shí)Spring boot監(jiān)控

    初識(shí)Spring boot監(jiān)控

    這篇文章主要介紹了spring boot監(jiān)控的相關(guān)知識(shí),文中給大家介紹了查看監(jiān)控?cái)?shù)據(jù),數(shù)據(jù)可視化的相關(guān)知識(shí),需要的朋友可以參考下
    2018-03-03
  • 基于Java 談回調(diào)函數(shù)

    基于Java 談回調(diào)函數(shù)

    回調(diào)函數(shù)就是一個(gè)通過(guò)函數(shù)指針調(diào)用的函數(shù)。如果你把函數(shù)的指針(地址)作為參數(shù)傳遞給另一個(gè)函數(shù),當(dāng)這個(gè)指針被用來(lái)調(diào)用其所 指向的函數(shù)時(shí),我們就說(shuō)這是回調(diào)函數(shù)
    2017-05-05
  • Java多線(xiàn)程synchronized同步方法詳解

    Java多線(xiàn)程synchronized同步方法詳解

    這篇文章主要介紹了Java多線(xiàn)程synchronized同步方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 詳解Java中String類(lèi)的各種用法

    詳解Java中String類(lèi)的各種用法

    Java中定義了String和StringBuffer兩個(gè)類(lèi)來(lái)封裝對(duì)字符串的各種操作,存放于java.lang包中,是Java語(yǔ)言的核心類(lèi),提供了字符串的比較、查找、截取、大小寫(xiě)轉(zhuǎn)換等操作,無(wú)需導(dǎo)入即可直接使用它們。讓我們來(lái)詳細(xì)了解它吧
    2021-11-11
  • 利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情

    利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情

    這篇文章主要介紹了利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情,根據(jù)概率將獎(jiǎng)品劃分區(qū)間,每個(gè)區(qū)間代表一個(gè)獎(jiǎng)品,然后抽取???隨機(jī)數(shù)??,反查落在那個(gè)區(qū)間上,即為所抽取的獎(jiǎng)品,需要的朋友可以參考一下
    2022-07-07
  • SpringBoot整合ZXing實(shí)現(xiàn)二維碼和條形碼的創(chuàng)建

    SpringBoot整合ZXing實(shí)現(xiàn)二維碼和條形碼的創(chuàng)建

    如今我們?cè)絹?lái)越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實(shí)現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊(duì)列的數(shù)組實(shí)現(xiàn)方法

    java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊(duì)列的數(shù)組實(shí)現(xiàn)方法

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之雙向循環(huán)隊(duì)列的數(shù)組實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了雙向循環(huán)隊(duì)列的原理與數(shù)組實(shí)現(xiàn)技巧,并附帶說(shuō)明了該算法的用途,需要的朋友可以參考下
    2016-08-08
  • SpringBoot使用knife4j進(jìn)行在線(xiàn)接口調(diào)試

    SpringBoot使用knife4j進(jìn)行在線(xiàn)接口調(diào)試

    這篇文章主要介紹了SpringBoot使用knife4j進(jìn)行在線(xiàn)接口調(diào)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 解析Java程序中對(duì)象內(nèi)存的分配和控制的基本方法

    解析Java程序中對(duì)象內(nèi)存的分配和控制的基本方法

    這篇文章主要介紹了解析Java程序中對(duì)象內(nèi)存的分配和控制的基本方法,包括計(jì)算對(duì)象的內(nèi)存占用的方法,要的朋友可以參考下
    2016-04-04
  • mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)詳解

    mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)詳解

    這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05

最新評(píng)論