您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java怎么實(shí)現(xiàn)對(duì)稱(chēng)加密DES和AES”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java怎么實(shí)現(xiàn)對(duì)稱(chēng)加密DES和AES”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
采用Java實(shí)現(xiàn)采用對(duì)稱(chēng)密碼算法的應(yīng)用軟件,所用算法包括DES算法和AES算法。要求該軟件具有圖形用戶界面,能生成密鑰,以及對(duì)字符串和文件進(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("對(duì)稱(chēng)加密算法實(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)對(duì)稱(chēng)加密DES和AES”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。