MD5的anroid代码,Android写MD5方法源码参考.
java语言写的MD5获取方法
获取字符串的MD5值源码下载参考:
/** * @ClassName OSS * @Description OSS * @Author TT * @Date 2021-06-16 16:42 * @Version 1.0 */ package util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Md5 { public static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; //将字符串转化为位 public static String toHexString(byte[] b) { StringBuilder stringBuilder = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { stringBuilder.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]); stringBuilder.append(HEX_DIGITS[b[i] & 0x0f]); } return stringBuilder.toString(); } public static String md5Hex(String string) { try { MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(string.getBytes()); byte messageDigest[] = digest.digest(); return toHexString(messageDigest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } /** * @param str MD5,返回小写的正常md5值 * @return */ public static String md5(String str) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] bytes = digest.digest(str.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : bytes) { int c = b & 0xff; //负数转换成正数 String result = Integer.toHexString(c); //把十进制的数转换成十六进制的书 if(result.length()<2){ sb.append(0); //让十六进制全部都是两位数 } sb.append(result); } return sb.toString(); //返回加密后的密文 } catch (Exception ex) { ex.printStackTrace(); return ""; } } }