MD5 encryption in Android

MD5 is an irreversible encryption algorithm, that is, it cannot be decrypted. It is mainly used to encrypt user passwords on the client side. No matter what the original string is, the MD5 encrypted string is a 32-bit hexadecimal string. MD5 is easier to crack, so we generally use the salting method for multi-layer encryption.

MD5 encryption algorithm tools are as follows.

public class MD5Util {

    public static String encrypt(String raw) {
        String md5Str = raw;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5"); // 创建一个MD5算法对象
            md.update(raw.getBytes()); // 给算法对象加载待加密的原始数据
            byte[] encryContext = md.digest(); // 调用digest方法完成哈希计算
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < encryContext.length; offset++) {
                i = encryContext[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i)); // 把字节数组逐位转换为十六进制数
            }
            md5Str = buf.toString(); // 拼装加密字符串
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5Str.toUpperCase(); // 输出大写的加密串
    }

}

The multi-layer encryption algorithm is like this. Suppose our encrypted string is String a; we have three salts, namely String x, String y, and String z. We first splice the a string with x, and perform MD5 encryption; add y to the encrypted string, and then perform MD5 encryption; finally, splice z on the second encrypted string, and perform one more time. MD5 encryption. In this way, even if it is deciphered once, the other party does not know what our x, y, and z are, and the three values ​​are applied at the same time, the possible situation will increase geometrically, so the degree of safety will be greatly improved. Similarly, we can use the method of dynamic additional transmission, let x, y, z be provided by the backend, which can better increase the degree of security. This method can also be used for some decryptable encryption algorithms mentioned later. I won't repeat it later. You can refer to the content here for application.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115028800