今天用Java类里自带的编码解码做了 一个加密解密器。其实我没做什么就是做了个界面。
还有key是固定八个字符,我改成不是八个字符也可以了。
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Encrypt extends JFrame{ private static final long serialVersionUID = 1L; JLabel lb1 = new JLabel("原文:"); JTextArea txt = new JTextArea(8,35); JScrollPane jsTxt = new JScrollPane(txt); JLabel lb = new JLabel("KEY:"); JTextField pwd = new JTextField(10); JButton encry = new JButton("加密"); JButton decry = new JButton("解密"); JLabel lb2 = new JLabel("加/解密后:"); JTextArea showArea = new JTextArea(8,35); JScrollPane jsShowArea = new JScrollPane(showArea); public Encrypt(String title) throws HeadlessException { super(title); } public static void main(String[] args) { new Encrypt("Encrypt").start(); } public void start(){ JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JPanel downPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); BorderLayout layout = new BorderLayout(5,5); this.setLayout(layout); this.setSize(400, 395); topPanel.add(lb1); topPanel.add(jsTxt); this.add("North", topPanel); leftPanel.add(lb); leftPanel.add(pwd); this.add("West", leftPanel); rightPanel.add(encry); encry.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(txt.getText().length()==0){ JOptionPane.showMessageDialog(null, "加密内容不能为空", "提示", JOptionPane.INFORMATION_MESSAGE); return ; } showArea.setText(encrypt(txt.getText(),pwd.getText())); } }); rightPanel.add(decry); decry.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(txt.getText().length()==0){ JOptionPane.showMessageDialog(null, "解密内容不能为空", "提示", JOptionPane.INFORMATION_MESSAGE); return ; } showArea.setText(decrypt(txt.getText(),pwd.getText())); } }); this.add("East", rightPanel); downPanel.add(lb2); downPanel.add(jsShowArea); this.add("South", downPanel); this.setResizable(false); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 加密逻辑方法 * @param message * @param key * @return * @throws Exception */ private static byte[] encryptProcess(String message, String key) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return cipher.doFinal(message.getBytes("UTF-8")); } /** * 解密逻辑方法 * @param message * @param key * @return * @throws Exception */ private static String decryptProcess(String message,String key) throws Exception { byte[] bytesrc =convertHexString(message); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } /** * 16进制数组数转化 * @param ss * @return */ private static byte[] convertHexString(String ss) throws Exception { byte digest[] = new byte[ss.length() / 2]; for(int i = 0; i < digest.length; i++) { String byteString = ss.substring(2 * i, 2 * i + 2); int byteValue = Integer.parseInt(byteString, 16); digest[i] = (byte)byteValue; } return digest; } /** * 十六进制数转化 * @param b * @return * @throws Exception */ private static String toHexString(byte b[]) throws Exception { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < b.length; i++) { String plainText = Integer.toHexString(0xff & b[i]); if (plainText.length() < 2) plainText = "0" + plainText; hexString.append(plainText); } return hexString.toString(); } private static String fixKey(String key){ if(key.length()>8){ key = key.substring(0, 8); }else{ while(key.length()<8){ key+='0'; } } return key; } /** * 加密方法 */ public static String encrypt(String message,String key){ String enStr = null; if(key.length()!=8){ key = fixKey(key); } System.out.println(key+"......"+key.length()); try { String orignStr=java.net.URLEncoder.encode(message, "utf-8"); enStr=toHexString(encryptProcess(orignStr, key)); } catch (Exception e) { JOptionPane.showMessageDialog(null, "加密参数异常!", "错误", JOptionPane.OK_OPTION); } return enStr; } /** * 解密方法 */ public static String decrypt(String message,String key){ String decStr = null; if(key.length()!=8){ key = fixKey(key); } System.out.println(key+"......"+key.length()); try { decStr = java.net.URLDecoder.decode(decryptProcess(message,key), "utf-8") ; }catch (Exception e) { JOptionPane.showMessageDialog(null, "解密参数异常!", "错误", JOptionPane.OK_OPTION); } return decStr; } }
经过测试。确实可以加解密。
时间: 2024-10-13 09:15:17