Python到C的解释

2024-05-16 17:52:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在把一个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


Tags: the代码from脚本stringbyteoperationlower
3条回答

您提供的代码段不是有效的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)));
}

编辑:为deobfousate\u字符串添加默认参数

谢谢大家的回复,最后我抓到了一个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;
        }
    }

以上就是我最后的结局

函数deobfuscate_string接受一个iterable pnr、一个字符串obfuscated和一个默认情况下为substract的operation。你知道吗

  • 对于字符串obfuscated中的每个字符c
  • 它适用于 运算符(默认情况下为减法)指定字符的值 pnr中的下一个元素。你知道吗
  • 然后它使用& 0xff来确保结果在255范围内
  • 然后把所有的东西组合成一条线。你知道吗

所以,它只是通过旋转一组已知的旋转来加密输入。你知道吗

注意:代码是无效的操作不能用这种方式,我只是解释一下这里的目标。你知道吗

相关问题 更多 >