参考资料:

    /**

    • Created by Miracle Luna on 2019/11/18 */ public class Md5Util {

      /**

      • 将数据进行 MD5 32加密,并以16进制字符串格式输出
      • @param data
      • @return */ public static String encrypt32(String data) { StringBuilder sb = new StringBuilder(); try {

        1. MessageDigest md = MessageDigest.getInstance("md5");
        2. byte[] md5 = md.digest(data.getBytes(StandardCharsets.UTF_8));
        3. // 将字节数据转换为十六进制
        4. for (byte b : md5) {
        5. String bytes = Integer.toHexString(b & 0xff);
        6. if (bytes.length() == 1){
        7. sb.append(0);
        8. }
        9. sb.append(bytes);
        10. }

        } catch (NoSuchAlgorithmException e) {

        1. e.printStackTrace();

        } return sb.toString(); }

    1. /**
    2. * @Description:加密-16位小写
    3. * @author:liuyc
    4. * @time:2016年5月23日 上午11:15:33
    5. */
    6. public static String encrypt16(String encryptStr) {
    7. return encrypt32(encryptStr).substring(8, 24);
    8. }

    }

    1. ```java
    2. public static void main(String[] args) {
    3. String password = "password";
    4. String encrypt32 = encrypt32(password);
    5. System.out.println("MD5 32位: " + encrypt32);
    6. System.out.println("MD5 16位: " + encrypt32.substring(8, 24));
    7. }

    MD5 32位: 5f4dcc3b5aa765d61d8327deb882cf99 MD5 16位: 5aa765d61d8327de

    注:

    • 测试可行,但由于Java版本限制,实际上这些接口并不适配较低的Android版本,不建议在Android上用