日期:2014-05-19  浏览次数:20729 次

struts2 项目 注册用户时 MD5加密
谁有struts2 项目 注册用户时的 MD5加密代码示例 action 和jsp页面的写法

------解决方案--------------------
jsp不用。只需要把传入过来的pass加密就行了。很简单的,给你提供个方法,直接套用就行
Java code
public final static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

------解决方案--------------------

import java.security.MessageDigest;

import sun.misc.BASE64Encoder;

public class MessageDigestUtil {
public static String digestByMD5(String str) throws Exception{
//采用MD5加密算法,将任意长度字符串加密
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bts = md.digest(str.getBytes());
//采用Base64算法,将加密后的字节变成字符串
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bts);
}

public static void main(String[] args) throws Exception{
System.out.println(digestByMD5("abc"));
System.out.println(digestByMD5("1231233223"));
System.out.println(digestByMD5("a"));
}


}

直接把前台获得的密码在保存的时候调用一下就行。。。比如
// 密码加密
String pwd = MessageDigestUtil.digestByMD5(password);
user.setPassword(pwd);
XXXService.save(user);


------解决方案--------------------
如果用的mysql 可一直直接用自带函数入:
insert into user(name,password) values('zhangsan',MD5('123456'));
------解决方案--------------------
Java code

package com.cissst.dsan.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    // MD5加密算法,对密码进行加密
    public static String MD5Password(String oldstr) {

        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };

        byte[] oldbytes = oldstr.getBytes();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");// 获取对象
            md.update(oldbytes);// 初始化对象
            byte[] newBytes = md.digest();// 运行加密算法
            char[] newStr = new char[32];
            for (int i = 0; i < 16; i++) {
                byte temp = newBytes[i];
                newStr[2 * i] = hexDigits[temp >>> 4 & 0xf];
                newStr[2 * i + 1] = hexDigits[temp & 0xf];

            }
            return new String(newStr);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }

    }
    public static void main(String[] args){
        System.out.print(MD5Password("123456"));
        if(MD5Password("123456").equals(MD5Password("123456"))){
            System.out.println("相等");
            
        }
    }
}