MD5加密

下面这个是工具类

package com.zgq.xl.content;



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

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

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

public class Encrypt {
	private static final Key key;
	private static final String DESede = "DESede";

	static {
		KeyGenerator keyGen = null;
		try {
			keyGen = KeyGenerator.getInstance(DESede);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		key = keyGen.generateKey();
	}

	public static String get(Object o) {
		if (o == null)
			return "";
		String str = o.toString();
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		str = str.replaceAll("=", "");

		return str;
	}

	public static String getNoEqual(Object o) {
		if (o == null) {
			return "";
		}

		String str = o.toString();
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		str = str.replaceAll("script", "");
		return str;
	}

	public static String get(Object o, boolean isReturnNull) {
		if ((o == null) && (!isReturnNull))
			return "";
		if ((o == null) && (isReturnNull))
			return null;
		String str = o.toString().toLowerCase();
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		str = str.replaceAll("script", "");
		str = str.replaceAll("=", "");

		return str;
	}

	/* 锟斤拷锟斤拷锟街凤拷 */
	public static String doStrByMD5(String str) {
		if (str == null)
			return null;

		MessageDigest mgd = null;
		try {
			mgd = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		mgd.update(str.getBytes());

		return byte2hex(mgd.digest());
	}

	/* 锟皆硷拷锟斤拷锟街凤拷锟斤拷斜冉锟? */
	public static boolean equalsByMD5(String strByMD5, String str) {
		if (strByMD5 == null)
			return false;
		if (str == null)
			return false;

		MessageDigest mgd = null;
		try {
			mgd = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		mgd.update(str.getBytes());

		return strByMD5.equals(byte2hex(mgd.digest()));
	}

	private static String byte2hex(byte[] b) {
		if (b == null)
			return "";
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}

	public static String getEncrypt(String str) {
		if (str == null)
			return null;

		byte[] encriptText = (byte[]) null;
		try {
			Cipher cipher = Cipher.getInstance(DESede);
			cipher.init(1, key);
			encriptText = cipher.doFinal(str.getBytes());
		} catch (Exception e) {
			System.out.println("Encript fail.");
		}

		return new BASE64Encoder().encode(encriptText);
	}

	public static String getDecrypt(String str) {
		if (str == null)
			return null;

		byte[] decriptText = (byte[]) null;
		try {
			decriptText = new BASE64Decoder().decodeBuffer(str);
			Cipher cipher = Cipher.getInstance(DESede);
			cipher.init(2, key);
			decriptText = cipher.doFinal(decriptText);
		} catch (Exception e) {
			System.out.println("Decript fail.");
		}

		return new String(decriptText);
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String md5Str = doStrByMD5("07978199092");
		System.out.println(md5Str);

	}
}

再写一个普的Java程序调用它

public static void main(String[] args) throws UnsupportedEncodingException {
		String md5Str = doStrByMD5("07978199092");
		System.out.println(md5Str);
		//结果为
		02498045D1D4571D34AB6D2836FAB846
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值