zoukankan      html  css  js  c++  java
  • MD5加密

    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>
    

      

  • 相关阅读:
    C#小常识集锦(一)《更锋利的CSharp代码》读书笔记
    QueryBuilder 优雅的Linq To SQL动态查询(转)
    了解 NoSQL 的必读资料
    微软报表
    linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
    LINQ体验(17)——LINQ to SQL语句之动态查询
    细数那些运行在微软平台上的NoSQL数据库
    5道经典的程序题
    打造自己的LINQ Provider(中):IQueryable和IQueryProvider (转 李会军)
    Expression Examples :Report Builder
  • 原文地址:https://www.cnblogs.com/therunningfish/p/6322010.html
Copyright © 2011-2022 走看看