采用java实现DES对称加密算法,要求具有图形用户界面,密钥的长度是8的倍数,可以生成密钥,以及对字符串进行加解密,给出完整的源代码,其中注释或者提示部分用中文,给出使用说明
时间: 2023-11-12 08:07:40 浏览: 256
java实现DES对称加密
以下是使用Java实现DES对称加密算法的完整源代码,包括图形用户界面和加解密功能:
```java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DESGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField keyTextField;
private JTextArea inputTextArea;
private JTextArea outputTextArea;
private SecretKey key;
private Cipher encryptCipher;
private Cipher decryptCipher;
public DESGUI() {
super("DES加解密工具");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JPanel inputPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel outputPanel = new JPanel();
keyTextField = new JTextField(32);
inputTextArea = new JTextArea(10, 40);
outputTextArea = new JTextArea(10, 40);
outputTextArea.setEditable(false);
JButton generateButton = new JButton("生成密钥");
JButton encryptButton = new JButton("加密");
JButton decryptButton = new JButton("解密");
generateButton.addActionListener(this);
encryptButton.addActionListener(this);
decryptButton.addActionListener(this);
inputPanel.add(keyTextField);
inputPanel.add(inputTextArea);
buttonPanel.add(generateButton);
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
outputPanel.add(outputTextArea);
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(outputPanel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new DESGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getActionCommand().equals("生成密钥")) {
generateKey();
} else if (e.getActionCommand().equals("加密")) {
encrypt();
} else if (e.getActionCommand().equals("解密")) {
decrypt();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
key = keyGenerator.generateKey();
keyTextField.setText(bytesToHex(key.getEncoded()));
}
private void encrypt() throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException {
if (key == null) {
generateKey();
}
if (encryptCipher == null) {
encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
}
byte[] input = inputTextArea.getText().getBytes();
byte[] output = encryptCipher.doFinal(input);
outputTextArea.setText(bytesToHex(output));
}
private void decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException {
if (key == null) {
generateKey();
}
if (decryptCipher == null) {
decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
byte[] input = hexToBytes(inputTextArea.getText());
byte[] output = decryptCipher.doFinal(input);
outputTextArea.setText(new String(output));
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
private byte[] hexToBytes(String hex) {
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return bytes;
}
}
```
使用说明:
1. 运行程序,会弹出一个图形用户界面。
2. 在“密钥”文本框中输入密钥,或者点击“生成密钥”按钮随机生成密钥。
3. 在“输入”文本框中输入要加密或解密的字符串。
4. 点击“加密”按钮进行加密,或者点击“解密”按钮进行解密。
5. 加密或解密后的结果会显示在“输出”文本框中,同时也会以十六进制格式显示在“输入”文本框中,方便复制粘贴。
6. 可以重复进行加密或解密操作,不需要重新输入密钥。
7. 点击窗口右上角的关闭按钮可以退出程序。
阅读全文