首页>代码>java加密工具类>/1200475157562368.java
package com.common.util;

import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  
public class EncryptUtils {  
    /** 
     * Encrypt string using MD5 algorithm 
     */  
    public final static String encryptMD5(String source) {  
        if (source == null) {  
            source = "";  
        }  
        String result = "";  
        try {  
            result = encrypt(source, "MD5");  
        } catch (NoSuchAlgorithmException ex) {  
            // this should never happen  
            throw new RuntimeException(ex);  
        }  
        return result;  
    }
    /** 
     * Encrypt string using SHA algorithm
     */  
    public final static String encryptSHA(String source) {  
        if (source == null) {  
            source = "";  
        }  
        String result = "";
        try {  
            result = encrypt(source, "SHA");  
        } catch (NoSuchAlgorithmException ex) {  
            // this should never happen  
            throw new RuntimeException(ex);  
        }  
        return result;  
    }  
    /**
     * Encrypt string 
     */
    private final static String encrypt(String source, String algorithm)  
            throws NoSuchAlgorithmException {  
        byte[] resByteArray = encrypt(source.getBytes(), algorithm);  
        return toHexString(resByteArray);  
    }
    /** 
     * Encrypt byte array. 
     */  
    private final static byte[] encrypt(byte[] source, String algorithm)  
            throws NoSuchAlgorithmException {  
        MessageDigest md = MessageDigest.getInstance(algorithm);  
        md.reset();  
        md.update(source);  
        return md.digest();  
    }  
    /** 
     * Get hex string from byte array 
     */  
    private final static String toHexString(byte[] res) {  
        StringBuffer sb = new StringBuffer(res.length << 1);  
        for (int i = 0; i < res.length; i++) {  
            String digit = Integer.toHexString(0xFF & res[i]);  
            if (digit.length() == 1) {  
                digit = '0' + digit;  
            }  
            sb.append(digit);  
        }  
        return sb.toString().toUpperCase();  
    }  
    
    public static void main(String args[]){

//		System.out.println(EncryptUtils.encrypt("a", "MD5"));
		//md5加密看下面控制台输出
		System.out.println(EncryptUtils.encryptMD5("王伟我爱你"));
	}
}  
最近下载更多
3199625134  LV10 2022年4月23日
726837706  LV1 2021年2月2日
为什么要爬那个单杠  LV1 2019年12月8日
zw5097  LV23 2019年11月24日
a986527557  LV6 2019年11月19日
yongzheng132  LV17 2019年6月25日
jiajia91323  LV23 2019年4月22日
disoaa  LV3 2018年9月17日
yu3961520  LV8 2018年6月8日
小王吧  LV2 2017年11月21日
最近浏览更多
syd1988  LV7 3月14日
yymmdm  LV6 2022年8月10日
微微笑的笑笑  LV3 2022年6月14日
liys1234  LV9 2022年4月27日
3199625134  LV10 2022年4月23日
张世亮  LV1 2022年3月15日
微信网友_5855482984206336  LV1 2022年3月2日
xiaoluoaaa  LV8 2022年1月10日
阿远远  LV8 2021年12月6日
别碰我被窝  LV9 2021年10月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友