Java實現(xiàn)對稱加密DES和AES的示例代碼
實驗內(nèi)容和要求
采用Java實現(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("對稱加密算法實現(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();
}
}
實現(xiàn)效果:

大概就是這樣
以上就是Java實現(xiàn)對稱加密DES和AES的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java對稱加密DES AES的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Spring開發(fā)之自定義標(biāo)簽及其解析
Spring框架是現(xiàn)在Java最流行的開源框架之一,需要實現(xiàn)一些自定義的標(biāo)簽,主要是方便使用我們框架的人能夠快速、簡單進(jìn)行配置,有興趣的可以了解一下。2017-04-04
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)行,在本節(jié)示例中,我們將創(chuàng)建兩個線程,一個是普通線程,向隊列中寫入事件,另外一個是守護(hù)線程,清除隊列中的事件,需要的朋友可以參考下2014-12-12
java編程實現(xiàn)根據(jù)EXCEL列名求其索引的方法
這篇文章主要介紹了java編程實現(xiàn)根據(jù)EXCEL列名求其索引的方法,涉及Java元素遍歷與數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
struts2中通過json傳值解決亂碼問題的實現(xiàn)方法
這篇文章主要介紹了struts2中通過json傳值解決亂碼問題的實現(xiàn)方法,涉及js編碼及java解碼的相關(guān)操作技巧,需要的朋友可以參考下2016-06-06
gateway與spring-boot-starter-web沖突問題的解決
這篇文章主要介紹了gateway與spring-boot-starter-web沖突問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

