public class TestMd5Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String password = "ranfeng";
byte[] by = password.getBytes();
String str = toMd5(by);
System.out.println("str md5 " + str);
}
private static String toMd5(byte[] bytes) {
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(bytes);
return toHexString(algorithm.digest(), "");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private static String toHexString(byte[] bytes, String separator) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b);
if(hex.length()==1){
hexString.append('0');
}
hexString.append(hex).append(separator);
}
return hexString.toString();
}
}
md5加密
最新推荐文章于 2022-05-07 08:25:31 发布