Encryption algorithm of MD5 digest ciphertext generated by mixed random salt

Posted by sealMail on Sun, 03 Nov 2019 18:01:27 +0100

Encryption steps:
  • Write the extractSalt() method, which generates and returns a random salt value
  • Write the method to generate MD5 summary: getMd5Hex(String
    str), the parameter is String, and 32-bit hex String is returned. This method needs to import jar package: commons-codec-1.10.jar
  • Write password generation method: generateKeywordText(String pwd,String
    salt), in the generateKeywordText() method, get the string hexs returned by getMd5Hex(salt+pwd), create a new 48 bit char array cs, through the for loop, respectively intercept the single character of hexs and salt custom rule position, and then give cs one by one, convert cs to the string os,os is the original password
Decryption step
  • Write the method verify(String pass,String origin);
  • In the verify() method, create digestChar character array [32] and saltChar character array [16] respectively, use the for loop to reverse intercept a single character from the origin according to the previously customized position rule, assign digestChar [] and saltChar [], and convert saltChar [] to the string salt
  • Finally, the hexString returned by getMd5Hex(salt+pass) is compared with new String(digestChar)
Java code
  /**
     * Extraction of salt
     *
     * @return
     */
    public String extractSalt() {
        Random random = new Random();
        StringBuilder builder = new StringBuilder(16);
        builder.append(random.nextInt(99999999));

        int length = builder.length();

        if (length < 16) {
            for (int i = 0; i < 16 - length; i++) {
                int n = random.nextInt(9);
                builder.append(n + "");
            }
        }

        return builder.toString();
    }

    /**
     * Get MD5 digest in hexadecimal string
     *
     * @param src
     * @return
     */
    private String getMd5Hex(String src) {
        MessageDigest md5 = null;

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        byte[] bs = md5.digest(src.getBytes());

        byte[] encode = new Hex().encode(bs);

        return new String(encode);

    }

    /**
     * Substitute the original password submitted by the page, generate and return the ciphertext
     * 
     * @param pwd raw materials
     * @param salt
     * @return
     */
    public String generateKeywordText(String pwd, String salt) {
        // Sprinkle salt and stir evenly in MD5hex method
        String hex = getMd5Hex(salt + pwd);

        char[] cs = new char[48];
        // Re encryption
        for (int i = 0; i < 48; i += 3) {
            cs[i] = hex.charAt(i / 3 * 2);
            cs[i + 1] = salt.charAt(i / 3);
            cs[i + 2] = hex.charAt(i / 3 * 2 + 1);
        }
        
        return new String(cs);

    }

    /**
     * Check whether the salt is consistent with the original and decrypt it in reverse
     *
     * @param password
     *            Password submitted
     * @param text
     *            original text
     * @return
     */
    public boolean verify(String password, String text) {
        char[] digestStr = new char[32];
        char[] saltStr = new char[16];

        for (int i = 0; i < 48; i += 3) {
            digestStr[i / 3 * 2] = text.charAt(i);
            digestStr[i / 3 * 2 + 1] = text.charAt(i + 2);

            saltStr[i / 3] = text.charAt(i + 1);
        }

        String salt = new String(saltStr);
        
        boolean b = getMd5Hex(salt + password).equals(new String(digestStr));
        
        return b;
        
    }

Topics: Java codec