MD5加密的使用-java的使用

博客介绍了MD5加密的特点,即只能单向加密且每次加密结果为32位。还提及了两种加密工具方法,一种是MD5Utils且被推荐,另一种是MD5和SHA1加密工具方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MD5加密只能单向加密,不能解密。每次都是32位的结果

第一种:MD5Utils 推荐

public class MD5Utils {

public static String getMD5_16bits(String str) {
    return getMD5_32bits(str).substring(8, 24);
}

public static String getMD5_32bits(String str) {
    if(str == null || str.equals("")){
        throw new RuntimeException("md5加密内容不能为空");
    }
    MessageDigest md=null;
    try {
        md = MessageDigest.getInstance("MD5");
        byte[] md5Bytes = md.digest(str.getBytes());
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        str = hexValue.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return str;
}

}

第二种: MD5和SHA1加密工具方法

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * 数据加密工具类型,主要提供了MD5和SHA1加密工具方法
 *
 * @author Lu Jianliang
 *
 */
public class EncryptUtil {
	public static final String KEY_SHA = "SHA";

	public static final String KEY_MD5 = "MD5";

	public static final String KEY_SHA1 = "SHA-1";

	private static final String KEY_SHA256 = "SHA-256";
	
	private static final String KEY_SHA512 = "SHA-512";

/**
 * BASE64解码
 *
 * @param key
 *            -需要解码的密码字符串
 * @return
 * @throws Exception
 */
public static byte[] decryptBASE64(String key) throws Exception {
	return (new BASE64Decoder()).decodeBuffer(key);
}

/**
 * BASE64编码
 *
 * @param key
 *            -需要编码的字节数组
 * @return
 * @throws Exception
 */
public static String encryptBASE64(byte[] key) throws Exception {
	return (new BASE64Encoder()).encodeBuffer(key);
}

/**
 * MD5加密
 *
 * @param data
 *            - 需要加密的字节数组
 * @return
 * @throws Exception
 */
public static byte[] encryptMD5(byte[] data) throws Exception {

	MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
	md5.update(data);

	return md5.digest();

}

/**
 * SHA加密
 *
 * @param data
 *            -需要加密的字节数组
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA(String shaType,byte[] data) throws Exception {

	MessageDigest sha = MessageDigest.getInstance(shaType);
	sha.update(data);

	return sha.digest();

}

/**
 * SHA1加密
 *
 * @param data
 *            -需要加密的字节数组
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA1(byte[] data) throws Exception {

	return encryptSHA(KEY_SHA1, data);

}

/**
 * SHA256加密
 *
 * @param data
 *            -需要加密的字节数组
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA256(byte[] data) throws Exception {

	return encryptSHA(KEY_SHA256, data);

}

/**
 * SHA512加密
 *
 * @param data
 *            -需要加密的字节数组
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA512(byte[] data) throws Exception {

	return encryptSHA(KEY_SHA512, data);

}

/**
 * MD5加密
 * 
 * @param content
 *            -待加密明文字符串
 * @param charset
 *            -待加密明文字符串采用的字符编码集
 * @return -返回MD5加密后的字符串
 */
public static String encryptMD5(String content, String charset) {
	try {
		byte[] b = encryptMD5(content.getBytes(charset));
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < b.length; i++) {
			String shaHex = Integer.toHexString(b[i] & 0xFF);
			if (shaHex.length() < 2) {
				buffer.append(0);
			}
			buffer.append(shaHex);
		}
		return buffer.toString();
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "";

}

/**
 * MD5加密算法,该方法采用UTF-8编码集将明文进行转换,如果明文字符串为其他字符编码,<br>
 * 请采用<code>encryptMD5(String content,String charset)</code>方法
 * 
 * @param content
 *            -待加密明文字符串
 * @return -返回加密后密文字符串
 */
public static String encryptMD5(String content) {
	return encryptMD5(content, "UTF-8");
}

/**
 * SHA加密算法
 * 
 * @param content -待加密明文
 * @param charset -明文所采用字符编码集
 * @return
 */
public static String encryptSHA(String shaType,String content, String charset) {
	try {
		byte[] b = encryptSHA(shaType,content.getBytes(charset));
		StringBuffer buffer = new StringBuffer();
		// 字节数组转换为 十六进制 数
		for (int i = 0; i < b.length; i++) {
			String shaHex = Integer.toHexString(b[i] & 0xFF);
			if (shaHex.length() < 2) {
				buffer.append(0);
			}
			buffer.append(shaHex);
		}
		return buffer.toString();
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "";
}

/**
 * SHA加密算法
 * @param content -待加密明文字符串
 * @return
 */
public static String encryptSHA(String shaType,String content){
	return encryptSHA(shaType,content,"UTF-8");
}

/**
 * SHA1加密算法
 * @param content -待加密明文字符串
 * @param charset -明文所采用字符编码集
 * @return
 */
public static String encryptSHA1(String content,String charset){
	
	return encryptSHA(KEY_SHA1, content,charset);
}


/**
 * SHA1加密算法
 * @param content -待加密明文字符串
 * @return
 */
public static String encryptSHA1(String content){
	return encryptSHA1(content,"UTF-8");
}

/**
 * SHA256加密算法
 * @param content -待加密明文字符串
 * @param charset -明文所采用字符编码集
 * @return
 */
public static String encryptSHA256(String content,String charset){
	
	return encryptSHA(KEY_SHA256, content,charset);
}


/**
 * SHA256加密算法
 * @param content -待加密明文字符串
 * @return
 */
public static String encryptSHA256(String content){
	return encryptSHA256(content,"UTF-8");
}

/**
 * SHA512加密算法
 * @param content -待加密明文字符串
 * @param charset -明文所采用字符编码集
 * @return
 */
public static String encryptSHA512(String content,String charset){
	
	return encryptSHA(KEY_SHA512, content,charset);
}


/**
 * SHA512加密算法
 * @param content -待加密明文字符串
 * @return
 */
public static String encryptSHA512(String content){
	return encryptSHA512(content,"UTF-8");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卧虎3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值