(5)Java-MD5

作者: 菜six岁 | 来源:发表于2019-11-08 17:49 被阅读0次
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class MD5Encrypter {
    
        private static String[] hexMap = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                String raw = "1";
                byte[] bs = md5.digest(raw.getBytes());
                StringBuilder sBuilder = new StringBuilder();
                for (byte b : bs) {
                    sBuilder.append(byteToHex(b));
                }
                System.out.println(sBuilder.toString().toLowerCase());
            } catch (NoSuchAlgorithmException e) {
                // TODO: handle exception
            }
    
        }
    
        private static String byteToHex(byte b) {
            // return String.format("%02x", b);
            int i = b >= 0 ? b : b + 256;
            return hexMap[i >> 4] + hexMap[i % 16];
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:(5)Java-MD5

        本文链接:https://www.haomeiwen.com/subject/ozndbctx.html