Java实现对称加密DES和AES的示例代码
作者:绘绘~
这篇文章主要介绍了如何使用Java实现采用对称密码算法的应用软件,所用算法包括DES算法和AES算法,文中的示例代码讲解详细,感兴趣的可以了解一下
实验内容和要求
采用Java实现采用对称密码算法的应用软件,所用算法包括DES算法和AES算法。要求该软件具有图形用户界面,能生成密钥,以及对字符串和文件进行加解密
参考代码
// 文件名: 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("对称加密算法实现"); 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(); } }
实现效果:
大概就是这样
以上就是Java实现对称加密DES和AES的示例代码的详细内容,更多关于Java对称加密DES AES的资料请关注脚本之家其它相关文章!