Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

百度网盘的MD5是加密的,HashInfo被注释了,但网页有加密算法,而且该算法可逆,已尝试出解密算法,能否再加回来 #7466

Closed
4 tasks
narfa0215 opened this issue Nov 5, 2024 · 0 comments · May be fixed by #7469
Labels
enhancement New feature or request

Comments

@narfa0215
Copy link

narfa0215 commented Nov 5, 2024

Please make sure of the following things

  • I have read the documentation.
  • I'm sure there are no duplicate issues or discussions.
  • I'm sure this feature is not implemented.
  • I'm sure it's a reasonable and popular requirement.

Description of the feature / 需求描述

drivers/baidu_netdisk/types.go的注释说明是由于直接获取的MD5是错误的,HashInfo的信息被注释了,但网页有加密算法,且是可逆的,可否恢复该信息

Suggested solution / 实现思路

百度网盘的网页的加密算法如下:

encryptMd5 = function(e) {
    if (e & 32 != e.length)
        return e;
    for (var t = 0, n = e.length; t < n; t++)
        if (!(e[t] >= 0 && e[t] <= 9 || e[t] >= "a" && e[t] <= "f"))
            return e;
    for (var i = e.substr(8, 8) + e.substr(0, 8) + e.substr(24, 8) + e.substr(16, 8), r = "", o = 0, a = i.length; o < a; o++)
        r += (parseInt(i[o], 16) ^ 15 & o).toString(16);
    var s = String.fromCharCode("g".charCodeAt() + parseInt(r[9], 16));
    return r.substr(0, 9) + s + r.substr(10)
}

我转换成如下的java代码:

public static String encryptMd5(String input) {
    // 如果输入字符串长度不符合条件,则返回原字符串
    if ((input.length() & 32) != input.length()) {
        return input;
    }

    // 检查输入是否为有效的十六进制字符
    for (int index = 0; index < input.length(); index++) {
        char currentChar = input.charAt(index);
        if (!(('0' <= currentChar && currentChar <= '9') || ('a' <= currentChar && currentChar <= 'f'))) {
            return input;
        }
    }

    // 重组字符串:将输入的特定位置字符拼接成新字符串
    String rearranged = input.substring(8, 16) + input.substring(0, 8) + input.substring(24, 32) + input.substring(16, 24);
    StringBuilder encryptedString = new StringBuilder();

    // 对重组后的字符串执行 XOR 操作并转换为十六进制字符串
    for (int position = 0; position < rearranged.length(); position++) {
        int hexValue = Integer.parseInt(String.valueOf(rearranged.charAt(position)), 16) ^ (15 & position);
        encryptedString.append(Integer.toHexString(hexValue));
    }

    // 使用字符 'g' 的 Unicode 编码,基于加密结果的第 9 个字符生成新的字符
    char specialChar = (char) ('g' + Integer.parseInt(String.valueOf(encryptedString.charAt(9)), 16));

    // 返回最终加密字符串
    return encryptedString.substring(0, 9) + specialChar + encryptedString.substring(10);
}

经过我的测试,解密算法只需要稍微调整顺序,改一点就能解密,代码如下:

public static String decryptMd5(String encrypted) {
    // 加密后的字符串长度必须符合预期
    if ((encrypted.length() & 32) != encrypted.length()) {
        return encrypted;
    }

    // 逆向恢复第 9 个字符
    // encrypted.charAt(9) 是加密后的字符
    char specialChar = encrypted.charAt(9);
    int offset = specialChar - 'g'; // 计算偏移量
    StringBuilder decryptedString = new StringBuilder(encrypted);

    // 恢复加密字符串的第 9 位字符
    decryptedString.setCharAt(9, Integer.toHexString(offset).charAt(0));

    StringBuilder originalString = new StringBuilder();

    // 逆向 XOR 操作并恢复原字符
    for (int position = 0; position < decryptedString.length(); position++) {
        int hexValue = Integer.parseInt(String.valueOf(decryptedString.charAt(position)), 16) ^ (15 & position);
        originalString.append(Integer.toHexString(hexValue));
    }

    // 重组字符串:将输入的特定位置字符拼接成新字符串
    String rearranged = originalString.substring(8, 16) + originalString.substring(0, 8) + originalString.substring(24, 32) + originalString.substring(16, 24);

    // 恢复原始字符串
    return rearranged;
}

加密的流程是重组字符串->执行XOR->第9个字符原地加上103('g')
解密的流程是第9个字符原地减去103('g')->执行XOR->重组字符串
只修改了输入输出变量,并把加改为减,就完成了解密

Additional context / 附件

No response

@narfa0215 narfa0215 added the enhancement New feature or request label Nov 5, 2024
@narfa0215 narfa0215 changed the title 百度网盘的MD5是加密的,HashInfo被注释了,但网页有加密算法,而且该算法可逆,能否处理完再加回来 百度网盘的MD5是加密的,HashInfo被注释了,但网页有加密算法,而且该算法可逆,能否再加回来 Nov 5, 2024
@narfa0215 narfa0215 changed the title 百度网盘的MD5是加密的,HashInfo被注释了,但网页有加密算法,而且该算法可逆,能否再加回来 百度网盘的MD5是加密的,HashInfo被注释了,但网页有加密算法,而且该算法可逆,已尝试出解密算法,能否再加回来 Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant