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

MD5 加密

时间:2017-10-17 21:40:33      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:mes   密码加密   数据   字符串   string   ati   update   derby   algorithm   

数据加密:

  我们在做关于私人信息时,我们总要使用到加密,特别是密码加密。如果我们的系统被黑客攻破了,他可以看见我们的全部信息。如果我们使用加密技术,即使他攻破了也无法拿到我们的正真信息,因为我们使用了加密技术,加密后的密码是不可逆。

  我的加密代码如下:

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

/**
 * 这是实现 MD5 数据加密的算法
 * 
 * @author TongZhou
 *
 */
public class EncodeByMd5 {
    /**
     * 
     * @param str 传入的参数
     * @return 返回加密后的字符串
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String EncoderByMd5(String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        char hexDigits[] = {
                ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘
        };
        try {
            byte[] btInput = string.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
}

 

MD5 加密

标签:mes   密码加密   数据   字符串   string   ati   update   derby   algorithm   

原文地址:http://www.cnblogs.com/gzbit-zxx/p/7598835.html

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