您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。