码迷,mamicode.com
首页 > 其他好文 > 详细

MD5加密

时间:2017-01-20 10:53:29      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:etc   ack   throws   getc   exce   test   sign   错误   int   

MD5加密

package test.test;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;

/**
 * MD5签名处理核心文件
 * */

public class MD5 {

    /**
     * 签名字符串
     *
     * @param text
     *            需要签名的字符串
     * @param key
     *            密钥
     *            编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String charset) throws Exception {
        text = text + key;
        System.out.println("签名字符串:"+text);
        return DigestUtils.md5Hex(getContentBytes(text, charset));
    }

    /**
     * 签名字符串
     *
     * @param text
     *            需要签名的字符串
     * @param key
     *            密钥
     * @param charset
     *            编码格式
     * @return 签名结果
     * @deprecated 无替代方法
     */
    public static String sign(String text, PrivateKey key, String charset) throws Exception {
        throw new UnsupportedOperationException();
    }

    /**
     * 签名字符串
     *
     * @param text
     *            需要签名的字符串
     * @param sign
     *            签名结果
     * @param key
     *            密钥
     * @param charset
     *            编码格式
     * @return 签名结果
     */
    public static boolean verify(String text, String sign, String key, String charset)
                                                                                      throws Exception {
        text = text + key;
        System.out.println("签名字符串:"+text);
        String mysign = DigestUtils.md5Hex(getContentBytes(text, charset));
        if (mysign.equals(sign)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param content
     * @param charset
     * @return
     * @throws java.security.SignatureException
     * @throws java.io.UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }
    
    public static void main(String[] args){
    	try {
    		String md5_sign = MD5.sign("15152200001", "mobile", "UTF-8");
			System.out.println(md5_sign);
			System.out.println(MD5.verify("15152200001", md5_sign, "mobile", "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}

Jar包依赖

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>${commons-codec_version}</version>
</dependency>

  

MD5加密

标签:etc   ack   throws   getc   exce   test   sign   错误   int   

原文地址:http://www.cnblogs.com/therunningfish/p/6322010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!