安卓MD5加密

1.方法还是比较简单,通过MessageDigest.getInstance("MD5")得到一个MessageDigest对象,这个类是Java自带的一个加密类。然后通过调用.digest(byte[])得到了加密后的字节数组。 得到加密后的字节数组后,我们通常要把它们转换成16进制式的字符串。

  private String md5(String content){
        byte[]hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(content.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("NoSuchAlgorithmException",e);
        }

        StringBuilder hex = new StringBuilder(hash.length*2);
        for(byte b:hash){
            if((b & 0xFF)< 0x10){
                hex.append(0);
            }
            hex.append(Integer.toHexString(b & 0xff));
        }

        return hex.toString();
    }

猜你喜欢

转载自www.cnblogs.com/x2yblog/p/9932344.html