Java實(shí)現(xiàn)對稱加密DES和AES的示例代碼
實(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)文章
利用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)建,文章通過代碼示例給大家介紹的非常詳細(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)方法,結(jié)合實(shí)例形式分析了雙向循環(huán)隊(duì)列的原理與數(shù)組實(shí)現(xiàn)技巧,并附帶說明了該算法的用途,需要的朋友可以參考下2016-08-08
SpringBoot使用knife4j進(jìn)行在線接口調(diào)試
這篇文章主要介紹了SpringBoot使用knife4j進(jìn)行在線接口調(diào)試,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類詳解
這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05

