MD5加密

MD5加密介绍

      MD5加密即Message-Digest Algorithm 5(信息-摘要算法),是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式,由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest于1991年开发,经MD2、MD3和MD4发展而来。   ---摘自搜狗百科

代码实现:本实现代码采用面向对象编程

主方法:

public class MD5 {
    private static final String HEX_NUMS_STR="0123456789ABCDEF";
    private static  final int SALT_LENGHT=12;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要加密的数");
        String s = scanner.next();
        String md5Str = getMD5Str(s);
        System.out.println(md5Str);
    }

下面的方法都在同一个类中,一下不在声明,大家可以直接粘贴直接使用

/**
 * 将16进制字符串转化为数组
 * @param hex
 * @return
 */
public static byte[]hexStringToByte(String hex){
    int len =(hex.length()/2);
    byte[]result = new byte[len];
    char[]hexChars = hex.toCharArray();
    for (int i = 0;i<len;i++){
        int pos = i*2;
        result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4
                | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));
    }
    return result;
}
/**
 *将指定byte数组转化成16进制字符串
 * @param b
 * @return
 */
public static  String byteToHexString(byte[]b){
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i]& 0xFF);
        if (hex.length()==1){
            hex='0'+hex;
        }
        hexString.append(hex.toUpperCase());
    }
    return hexString.toString();
}
/**
 * 验证口令是否正确
 * @param password
 * @param passwordDb
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static boolean validPassword(String password,String passwordDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    byte[]pwdlnDn = hexStringToByte(passwordDb);
    byte[]salt = new byte[SALT_LENGHT];
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(salt);
    md.update(password.getBytes("UTF-8"));
    byte[] digest = md.digest();
    byte[]digestInDb = new byte[pwdlnDn.length-SALT_LENGHT];
    System.arraycopy(pwdlnDn,SALT_LENGHT,digestInDb,0,digestInDb.length);
    if (Arrays.equals(digest,digestInDb)){
        return true;
    }else {
        return false;
    }
}
/**
 * 获得加密后的16进制形式
 * @param password
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static String getEncryptedPwd(String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    byte[] pwd = null;
    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[SALT_LENGHT];
    random.nextBytes(salt);

    MessageDigest md = null;
    md = MessageDigest.getInstance("MD5");
    md.update(salt);
    md.update(password.getBytes("UTF-8"));
    byte[] digest = md.digest();

    pwd = new byte[digest.length + SALT_LENGHT];
    System.arraycopy(salt, 0, pwd, 0, SALT_LENGHT);
    System.arraycopy(digest, 0, pwd, SALT_LENGHT, digest.length);
    return byteToHexString(pwd);
}
/**
 * 输出显示加密后的字符串以及编码字符的设置
 * @param str 要加密的字符串
 * @return
 */
public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;

    try {
        messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.reset();

        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException caught!");
        System.exit(-1);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }

    return md5StrBuff.toString();
}

看下运算结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

double_lifly

点喜欢就是最好的打赏!!

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

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

打赏作者

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

抵扣说明:

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

余额充值