md5工具使用方法 安卓md5校验工具apk( 二 )


/** * 将字符串转换为MD5 */ public class ParseMD5 { public static String parseStrToMd5L32(String str) { // 将字符串转换为32位小写MD5 String reStr = null; try { MessageDigest md5 = MessageDigest.getInstance(\\”MD5\\”); byte[] bytes = md5.digest(str.getBytes()); StringBuffer stringBuffer = new StringBuffer(); for (byte b : bytes) { int bt = b&0xff; if (bt < 16) { stringBuffer.append(0); } stringBuffer.append(Integer.toHexString(bt)); } reStr = stringBuffer.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return reStr; }
// 将字符串转换为32位大写的MD5public static String parseStrToMd5U32(String str) {String reStr = parseStrToMd5L32(str);if (reStr != null) {reStr = reStr.toUpperCase();}return resStr;}
// 将字符串转换为16位小写的MD5public static String parseStrToMd5L16(String str) {String reStr = paseStrToMd5L32(str);if (reStr != null) {reStr = reStr.subString(8, 24);}return reStr;}
// 将字符串转换为16位大写的MD5public static String parseStrToMd5U16(String str) {String reStr = parseStrToMd5L32(str);if (reStr != null) {reStr = reStr.toUpperCase().subString(8, 24);}return reStr;} }文本的MD5加密工具类Java中提供了自带的MessageDigest实现对文本的加密算法. 对文本进行加密的MD5加密工具类如下:
public class MD5Util {// 将文本转换为32位小写的MD5public static String textToMd5L32(String plainText) {String result = null;// 判断需要转换的文本是否为空if (StringUtils.isBlank(plainText)) {return null;}try {// 进行实例化和初始化MessageDigest md5 = MessageDigest.getInstance(\\”MD5\\”);// 得到一个操作系统默认的字节编码格式的字节数组byte[] byteInput = plainText.getBytes();// 对得到的字节数组进行处理md5.update(byteInput);// 进行Hash计算并得到返回结果byte[] btResult = md5.digest();// 得到进行Hash计算后数据的长度StringBuffer stringBuffer = new StringBuffer();for (byte b : btResult) {int bt = b&0xff;if (bt < 16) {stringBuffer.append(0);}stringBuffer.append(Integer.toHexString(bt));}reStr = stringBuffer.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return reStr;}
【md5工具使用方法 安卓md5校验工具apk】// 将文本转换为32位大写的MD5public static String textToMd5U32(String plainText) {if (StringUtils.isBlank(plainText)) {return null;}String result = textToMd5L32(plainText);result = result.toUpperCase();return result;}}