最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

采用Java實(shí)現(xiàn)采用對稱密碼算法的應(yīng)用軟件,所用算法包括DES算法和AES算法。要求該軟件具有圖形用戶界面,能生成密鑰,以及對字符串和文件進(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("對稱加密算法實(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)對稱加密DES和AES的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java對稱加密DES AES的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于Spring開發(fā)之自定義標(biāo)簽及其解析

    基于Spring開發(fā)之自定義標(biāo)簽及其解析

    Spring框架是現(xiàn)在Java最流行的開源框架之一,需要實(shí)現(xiàn)一些自定義的標(biāo)簽,主要是方便使用我們框架的人能夠快速、簡單進(jìn)行配置,有興趣的可以了解一下。
    2017-04-04
  • Spring Boot conditional注解用法詳解

    Spring Boot conditional注解用法詳解

    這篇文章主要介紹了Spring Boot conditional注解用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java并發(fā)程序刺客之假共享的原理及復(fù)現(xiàn)

    Java并發(fā)程序刺客之假共享的原理及復(fù)現(xiàn)

    前段時間在各種社交平臺“雪糕刺客”這個詞比較火,而在并發(fā)程序中也有一個刺客,那就是假共享。本文將通過示例詳細(xì)講解假共享的原理及復(fù)現(xiàn),需要的可以參考一下
    2022-08-08
  • Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行

    Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行

    這篇文章主要介紹了Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行,在本節(jié)示例中,我們將創(chuàng)建兩個線程,一個是普通線程,向隊(duì)列中寫入事件,另外一個是守護(hù)線程,清除隊(duì)列中的事件,需要的朋友可以參考下
    2014-12-12
  • 使用注解開發(fā)SpringMVC詳細(xì)配置教程

    使用注解開發(fā)SpringMVC詳細(xì)配置教程

    這篇文章主要介紹了使用注解開發(fā)SpringMVC詳細(xì)配置教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法

    java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法

    這篇文章主要介紹了java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法,涉及Java元素遍歷與數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • struts2中通過json傳值解決亂碼問題的實(shí)現(xiàn)方法

    struts2中通過json傳值解決亂碼問題的實(shí)現(xiàn)方法

    這篇文章主要介紹了struts2中通過json傳值解決亂碼問題的實(shí)現(xiàn)方法,涉及js編碼及java解碼的相關(guān)操作技巧,需要的朋友可以參考下
    2016-06-06
  • 淺談Java中的LinkedHashSet哈希鏈表

    淺談Java中的LinkedHashSet哈希鏈表

    這篇文章主要介紹了淺談Java中的LinkedHashSet哈希鏈表,LinkedHashSet 是 Java 中的一個集合類,它是 HashSet 的子類,并實(shí)現(xiàn)了 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 控制瀏覽器中需要用到的鍵盤操作。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01

最新評論

女性| 德格县| 杂多县| 疏附县| 宜章县| 锡林浩特市| 晋宁县| 社旗县| 武定县| 兰溪市| 楚雄市| 镶黄旗| 威信县| 香河县| 攀枝花市| 仪征市| 定边县| 舟曲县| 平南县| 柳州市| 天门市| 兰考县| 磐石市| 集安市| 阿合奇县| 巴彦县| 盘锦市| 沅江市| 德兴市| 和平区| 隆昌县| 台安县| 乡宁县| 丁青县| 繁昌县| 额尔古纳市| 泸西县| 龙海市| 屏东市| 黔西县| 集安市|