A-A+
某段java代码爬虫内置加密算法转python

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】
对方是一个agent,打开后发现全是加密的,找到加解密函数,如下:
package com.github.catvod.spider.merge;
import java.io.ByteArrayOutputStream;
/* loaded from: classes.dex */
public class cYh {
private static final String KEY = "gPAQWZ";
private static final String hexString = "0123456789ABCDEF";
public static String d(String str) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(str.length() / 2);
for (int i = 0; i < str.length(); i += 2) {
baos.write((hexString.indexOf(str.charAt(i)) << 4) | hexString.indexOf(str.charAt(i + 1)));
}
byte[] b = baos.toByteArray();
int len = b.length;
int keyLen = KEY.length();
for (int i2 = 0; i2 < len; i2++) {
b[i2] = (byte) (b[i2] ^ KEY.charAt(i2 % keyLen));
}
return new String(b);
}
}
转换成python代码如下:
#!/usr/bin/python
# Write Python 3 code in this online editor and run it.
class CYh:
KEY = "gPAQWZ"
hex_string = "0123456789ABCDEF"
@staticmethod
def d(s):
b = bytearray()
for i in range(0, len(s), 2):
b.append((CYh.hex_string.index(s[i]) << 4) | CYh.hex_string.index(s[i + 1]))
key_len = len(CYh.KEY)
for i in range(len(b)):
b[i] ^= ord(CYh.KEY[i % key_len])
return bytes(b).decode('utf-8')
# 解密字符串
encrypted_string = "243F3128062F08242435042E153135343023470B273E2537062407383236036D"
decrypted_string = CYh.d(encrypted_string)
print(decrypted_string)
布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏