今天贴两个简单的方法,需要的时候不用自己再写,
public static string md5(string plaintext){ messagedigest m; try { m = messagedigest.getinstance("md5"); m.reset(); m.update(plaintext.getbytes()); byte[] digest = m.digest(); biginteger bigint = new biginteger(1,digest); string hashtext = bigint.tostring(16); // now we need to zero pad it if you actually want the full 32 chars. while(hashtext.length() < 32 ){ hashtext = "0"+hashtext; } return hashtext; } catch (nosuchalgorithmexception e) { e.printstacktrace(); return system.currenttimemillis() + ""; } } public static string randomcreator(int num){ random random = new random(); int i=random.nextint(); i=math.abs(i); string tem=string.valueof(i); while(tem.length()<num){ tem+="0"; } return tem.substring(0,num); }