Java实现MD5散列

生成MD5值
String str = "12345";
MessageDigest digest =MessageDigest.getInstance("MD5");
byte[] bs = digest.digest(str.getBytes());
BytesPrint("未经过散列的S:",bs);
String bb = "";
for(int i =0;i<bs.length;i++)
{
    bb += byteHEX(bs[i]);
}
System.out.println(bb);

String aa = new String(bs, "iso-8859-1");//直接转化成字符串,省地
打印出来  
 82 7c cb 0e ea 8a 70 6c 4c 34 a1 68 91 f8 4e 7b 
static public void BytesPrint(String string, byte[] bs)
{
    System.out.print(string);
    for (int i=0;i<bs.length;i++)
    {
        System.out.printf("%02x ",bs[i]);
    }
    System.out.println("");
}
生成字符串
public static String byteHEX(byte ib)
{
    char[] Digit ={ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    char[] ob = new char[2];
    ob[0] = Digit[(ib >>> 4) & 0X0F];
    ob[1] = Digit[ib & 0X0F];
    String s = new String(ob);
    return s;
}

参考:




原文地址:https://www.cnblogs.com/MarmaladeCat/p/3dbfb8890866d20beaffe2c96825e6f9.html