基于AWT的DES加密和解密工具开发

数据加密标准DES加密算法是一种对称加密算法,DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES 使用 16 个循环,使用异或,置换,代换,移位操作四种基本运算。

近期做项目需要对数据库的配置文件进行加密,经过研究,特整理出如下的资料供大家参考


一、本文首先介绍DES加密算法的Java实现

package omms;


import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class DigestUtils
{
  private static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";

    /**

     *des加密算法程序

      **/
  public static String desEncode(String key, String data)
  {
    if (data == null)
      return "";
    try {
      DESKeySpec dks = new DESKeySpec(key.getBytes());
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");


      Key secretKey = keyFactory.generateSecret(dks);
      Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
      AlgorithmParameterSpec paramSpec = iv;
      cipher.init(1, secretKey, paramSpec);
      byte[] bytes = cipher.doFinal(data.getBytes());
      return byte2hex(bytes);
    } catch (Exception e) {
      e.printStackTrace();
    }return "";
  }


  public static String desDecode(String key, String data)
  {
    if (data == null)
      return "";
    try {
      DESKeySpec dks = new DESKeySpec(key.getBytes());
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");


      Key secretKey = keyFactory.generateSecret(dks);
      Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
      AlgorithmParameterSpec paramSpec = iv;
      cipher.init(2, secretKey, paramSpec);
      return new String(cipher.doFinal(hex2byte(data)));
    } catch (Exception e) {
      e.printStackTrace();
    }return "";
  }


  public static String byte2hex(byte[] b)
  {
    StringBuilder hs = new StringBuilder();


    for (int n = 0; (b != null) && (n < b.length); n++) {
      String stmp = Integer.toHexString(b[n] & 0xFF);
      if (stmp.length() == 1)
        hs.append('0');
      hs.append(stmp);
    }
    return hs.toString().toUpperCase();
  }


  public static byte[] hex2byte(String src)
  {
    byte[] res = new byte[src.length() / 2];
    char[] chs = src.toCharArray();
    int i = 0; for (int c = 0; i < chs.length; c++) {
      res[c] = (byte)Integer.parseInt(new String(chs, i, 2), 16);


      i += 2;
    }


    return res;
  }
}

二、加密和解密工具开发

先上图,这是基于awt做好之后的解密


下面是源码

package omms;


import java.awt.Button;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;


public class PasswordTools extends JFrame
{
  private static final long serialVersionUID = 1L;
  private JPanel contentPane;


  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          PasswordTools frame = new PasswordTools();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }


  public PasswordTools()
  {
    setTitle("密码加解密工具");
    setDefaultCloseOperation(3);
    setBounds(100, 100, 633, 456);
    this.contentPane = new JPanel();
    this.contentPane.setBackground(new Color(188, 232, 241));
    setContentPane(this.contentPane);
    this.contentPane.setSize(100, 200);


    JPanel jpanel = new JPanel();
    jpanel.setBackground(new Color(188, 232, 241));
    jpanel.setBounds(0, 0, 617, 418);
    JLabel label = new JLabel("明文", 2);
    label.setFont(new Font("宋体", 0, 12));
    label.setBounds(28, 23, 35, 21);
    jpanel.add(label);
    jpanel.setLayout(null);


    JLabel lable2 = new JLabel("密钥", 2);
    lable2.setFont(new Font("宋体", 0, 12));
    lable2.setBounds(28, 178, 35, 34);
    jpanel.add(lable2);
    this.contentPane.setLayout(null);
    this.contentPane.add(jpanel);
    Button encodeBtn = new Button("加密");
    encodeBtn.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    encodeBtn.setBounds(105, 223, 87, 37);
    encodeBtn.setBackground(new Color(185, 209, 234));
    jpanel.add(encodeBtn);
    Button decodeBtn = new Button("解密");
    decodeBtn.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    decodeBtn.setBounds(247, 223, 87, 37);
    decodeBtn.setBackground(new Color(185, 209, 234));
    jpanel.add(decodeBtn);
    Button clear = new Button("清除");
    clear.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    clear.setBounds(389, 223, 93, 37);
    clear.setBackground(new Color(185, 209, 234));
    jpanel.add(clear);
    JLabel lable3 = new JLabel("密文", 2);
    lable3.setFont(new Font("宋体", 0, 12));
    lable3.setBounds(28, 266, 35, 21);
    jpanel.add(lable3);


    JLabel label_1 = new JLabel("版权所有 @成立", 2);
    label_1.setFont(new Font("宋体", 0, 12));
    label_1.setBounds(466, 377, 124, 31);
    jpanel.add(label_1);


    TextArea key = new TextArea("", 20, 43, 3);
    key.setBounds(60, 184, 515, 30);
    jpanel.add(key);


    TextArea encryptPwd = new TextArea("", 20, 43, 3);
    encryptPwd.setBounds(28, 290, 546, 87);
    jpanel.add(encryptPwd);


    TextArea oriPwd = new TextArea("", 20, 43, 3);
    oriPwd.setBounds(28, 47, 546, 121);
    jpanel.add(oriPwd);


    clear.addActionListener(new ActionListener(oriPwd, encryptPwd)
    {
      public void actionPerformed(ActionEvent e) {
        this.val$oriPwd.setText("");
        this.val$encryptPwd.setText("");
      }
    });
    decodeBtn.addActionListener(new ActionListener(encryptPwd, key, oriPwd)
    {
      public void actionPerformed(ActionEvent e) {
        if (PasswordTools.this.checkDecodeData(this.val$encryptPwd, this.val$key)) {
          String encodeResult = DigestUtils.desDecode(this.val$key.getText(), this.val$encryptPwd.getText());
          this.val$oriPwd.setText(encodeResult);
        }
      }
    });
    encodeBtn.addActionListener(new ActionListener(oriPwd, key, encryptPwd)
    {
      public void actionPerformed(ActionEvent e) {
        if (PasswordTools.this.checkEncodeData(this.val$oriPwd, this.val$key)) {
          String encodeResult = DigestUtils.desEncode(this.val$key.getText(), this.val$oriPwd.getText());
          this.val$encryptPwd.setText(encodeResult);
        }
      }
    });
  }


  private boolean checkEncodeData(TextArea oriPwd, TextArea key)
  {
    boolean result = true;
    String error = "";
    if (oriPwd.getText().isEmpty()) {
      error = "密码明文不能为空!";
      result = false;
    } else if (key.getText().length() < 8) {
      error = "密钥长度不能小于8!";
      result = false;
    }
    if (!result) {
      JOptionPane.showMessageDialog(this.contentPane, error, "错误提示", 2);
    }
    return result;
  }


  private boolean checkDecodeData(TextArea encryptPwd, TextArea key)
  {
    boolean result = true;
    String error = "";
    if (encryptPwd.getText().isEmpty()) {
      error = "密文不能为空!";
      result = false;
    } else if (key.getText().length() < 8) {
      error = "密钥长度不能小于8!";
      result = false;
    }
    if (!result) {
      JOptionPane.showMessageDialog(this.contentPane, error, "错误提示", 2);
    }
    return result;
  }
}

下面是运行结果

基于AWT的DES加密和解密工具开发_第1张图片



你可能感兴趣的:(互联网,人工智能,机器学习)