Python到c#的解释
本文关键字:解释 Python | 更新日期: 2023-09-27 18:13:09
我一直在转换Python脚本到c#,我有99%,但我有困难理解下面的代码
# The lower 8-bits from the Xorshift PNR are subtracted from byte
# values during extraction, and added to byte values on insertion.
# When calling deobfuscate_string() the whole string is processed.
def deobfuscate_string(pnr, obfuscated, operation=int.__sub__):
return ''.join([chr((ord(c) operation pnr.next()) & 0xff) for c in obfuscated])
你能解释一下上面的代码吗?operation pnr.next()
做什么?如果你能帮忙把这个方法转换成c#,那就更好了,但对上面的解释会更好。
完整的源代码可以在
找到https://raw.githubusercontent.com/sladen/pat/master/gar.py您提供的代码段不是有效的Python代码。不能在中缀操作符的位置写函数名。我想应该是这样的:
# The lower 8-bits from the Xorshift PNR are subtracted from byte
# values during extraction, and added to byte values on insertion.
# When calling deobfuscate_string() the whole string is processed.
def deobfuscate_string(pnr, obfuscated, operation=int.__sub__):
return ''.join([chr(operation(ord(c), pnr.next()) & 0xff) for c in obfuscated])
你看,这样它将在ord(c)
和pnr.next()
上执行operation
。这样转换到c#就很简单了,操作类型应该是Func<int, int, int>
。
这可能会给你一个想法:
public static T Next<T>(IEnumerator<T> en) {
en.MoveNext();
return en.Current;
}
public static string deobfuscate_string(IEnumerator<int> pnr, string obfuscated, Func<int, int, int> operation = null) {
if (operation == null) operation = (a, b) => a - b;
return string.Join("", from c in obfuscated select (char)operation((int)c, Next(pnr)));
}
编辑:添加默认参数deobfuscate_string
函数deobfuscate_string
接受一个可迭代对象pnr
、一个字符串obfuscated
和一个默认为减法的operation
。
- 对于字符串
obfuscated
中的每个字符 - 应用操作符(默认为减法)与字符的值 中的下一个元素
- 然后使用
& 0xff
确保结果在255 范围内 - 所有的东西都被组合成一个字符串。
c
所以,它只是通过旋转已知旋转集合中的每个字符来加密输入。
注意:代码是无效的,因为操作不能这样使用,我只是在这里解释目的。
感谢大家的回复,我最后找了一个Python调试器来调试。
private static byte[] deobfuscate_string(XORShift128 pnr, byte[] obfuscated)
{
byte[] deobfuscated = new byte[obfuscated.Length];
for (int i = 0; i < obfuscated.Length; i++)
{
byte b = Convert.ToByte((obfuscated[i] - pnr.next()) & 0xff);
deobfuscated[i] = b;
}
Array.Reverse(deobfuscated);
return deobfuscated;
}
private class XORShift128
{
private UInt32 x = 123456789;
private UInt32 y = 362436069;
private UInt32 z = 521288629;
private UInt32 w = 88675123;
public XORShift128(UInt32 x, UInt32 y)
{
this.x = x;
this.y = y;
}
public UInt32 next()
{
UInt32 t = (x ^ (x << 11)) & 0xffffffff;
x = y;
y = z;
z = w;
w = (w ^ (w >> 19) ^ (t ^ (t >> 8)));
return w;
}
}
上面的就是我最后写的