Java对字符串进行md5加密

public static String md5(String source){
        //1.判断 source是否有效
        if(source == null || source.length() == 0){
            throw new RuntimeException("请输入正确的字符串!!");
        }
        try {
            //指明算法的类型
            String algorithm = "md5";
            //获取MessageDigest对象
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            //获取明文字符串对应的字节数组
            byte[] input = source.getBytes();
            //执行加密
            byte[] output = messageDigest.digest(input);
            //创建BigInteger对象
            int signum = 1;
            BigInteger bigInteger = new BigInteger(signum, output);
            //按照16进制将bigInteger的值转为字符串
            int radix = 16;
            String encode = bigInteger.toString(radix).toUpperCase();
            return encode;

        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

你可能感兴趣的:(java,java)