Liny_@NotePad

沉迷ACG中

MD5加密小程序

YOYO posted @ 2008年9月11日 09:15 in 【Java SE】 with tags md5 加密 实例 , 4484 阅读

【介绍】

调用了百度的MD5加密算法,实现对指定明文进行简单MD5加密的小程序~

【运行截图】



【程式代码】

TestMD5.java

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. /**
  7. * MD5加密小程序
  8. * @author Linyq.
  9. * @author YOYO.1989x.net
  10. * @version 1.0
  11. */
  12. public class TestMD5 extends JFrame {
  13.        
  14.         /**
  15.          * 明文输入框
  16.          */
  17.         private JTextField encodeText = new JTextField(20);
  18.         /**
  19.          * 密文显示框
  20.          */
  21.         private JTextField decodeText = new JTextField(20);
  22.        
  23.         /**
  24.          * 16位加密按钮
  25.          */
  26.         private JButton btn16md5 = new JButton("16位MD5加密");
  27.         /**
  28.          * 32位加密按钮
  29.          */
  30.         private JButton btn32md5 = new JButton("32位MD5加密");
  31.  
  32.         /**
  33.          * 入口方法
  34.          * @param args
  35.          */
  36.         public static void main(String[] args) {
  37.                 new TestMD5();
  38.         }
  39.        
  40.         /**
  41.          * 构造方法:创建默认程式窗体
  42.          */
  43.         public TestMD5(){
  44.                 this.setSize(320,150);
  45.                 this.setTitle("MD5加密小程序");
  46.                 this.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
  47.                
  48.                 this.add(new JLabel("明文:"));
  49.                 this.add(encodeText);
  50.                
  51.                 btn16md5.addActionListener(new EncodeBtn());
  52.                 btn32md5.addActionListener(new EncodeBtn());
  53.                
  54.                 this.add(btn16md5);
  55.                 this.add(btn32md5);
  56.                
  57.                 this.add(new JLabel("密文:"));
  58.                 decodeText.setEditable(false);
  59.                 this.add(decodeText);
  60.                
  61.                 this.setVisible(true);
  62.                
  63.         }
  64.        
  65.         /**
  66.          * 加密按钮触发事件
  67.          * @author Linyq.
  68.          * @author YOYO.1989x.net
  69.          * @version 1.0
  70.          */
  71.         private class EncodeBtn implements ActionListener{
  72.  
  73.                 /**
  74.                  * 重写事件
  75.                  */
  76.                 public void actionPerformed(ActionEvent e) {
  77.                         if(e.getSource() == btn16md5){
  78.                                 //      显示16位加密结果
  79.                                 decodeText.setText(new Md5().encode16(encodeText.getText()));
  80.                         }
  81.                         if(e.getSource() == btn32md5){
  82.                                 //      显示32位加密结果
  83.                                 decodeText.setText(new Md5().encode32(encodeText.getText()));
  84.                         }
  85.                 }
  86.                
  87.         }
  88.  
  89. }

Md5.java

  1. import java.security.MessageDigest;
  2. import java.security.NoSuchAlgorithmException;
  3.  
  4. /**
  5. * MD5加密类
  6. * @author Linyq.
  7. * @author YOYO.1989x.net
  8. * @version 1.0
  9. */
  10. public class Md5 {
  11.                
  12.         /**
  13.          * 16位加密方法
  14.          * @param plainText 明文
  15.          * @return 16位加密的密文
  16.          */
  17.         public String encode16(String plainText){
  18.                 return encode(plainText).substring(8,24);
  19.         }
  20.                
  21.         /**
  22.          * 32位加密方法
  23.          * @param plainText 明文
  24.          * @return 32位加密的密文
  25.          */
  26.         public String encode32(String plainText){
  27.                 return encode(plainText);
  28.         }
  29.        
  30.         /**
  31.          * MD5加密方法(实际上就是32位加密 - - )
  32.          * @param plainText 明文
  33.          * @return 密文
  34.          */
  35.         public String encode(String plainText){
  36.                 try {
  37.                         MessageDigest md = MessageDigest.getInstance("MD5");
  38.                         md.update(plainText.getBytes());
  39.                         byte b[] = md.digest();
  40.                
  41.                         int i;
  42.        
  43.                         StringBuffer buf = new StringBuffer("");
  44.                         for (int offset = 0; offset < b.length; offset++) {
  45.                                 i = b[offset];
  46.                                 if(i<0) i+= 256;
  47.                                 if(i<16)
  48.                                 buf.append("0");
  49.                                 buf.append(Integer.toHexString(i));
  50.                         }
  51.        
  52.                         return buf.toString();
  53.        
  54.                         } catch (NoSuchAlgorithmException e) {
  55.                         return null;
  56.                 }
  57.         }
  58.        
  59. }

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter