2015-09-16 46 views
-2

我一直在轉換一個Python腳本到C#,我99%有,但我無法理解下面這段代碼的Python到C#解釋

# 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

+0

無效Python代碼,很遺憾。我可以看到它應該做什麼,但Python不能像那樣工作。 –

+0

嗨丹尼爾,這是從github .py文件複製,鏈接包含在原始文章中。這段代碼應該做什麼?如果你不會介意 – LoanRanger

+0

'operation'被定義爲'INT .__ sub__',所以它應該是等同於'ORD(C) - pnr.next()'。這是否更清晰?正如我所說,這在Python中不起作用;也許作者習慣了Scala之類的東西。 –

回答

0

謝謝大家對張貼的反應,我最終抓住一個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; 
     } 
    } 

上面就是我結束了

2

中找到您所提供的片段是不是一個有效的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

+0

而且由於每次調用這個函數都沒有提供明確的'operation'參數,所以總是應用相減的默認值。 ('qcompress_prefix = deobfuscate_string(pnr,contents [12:16])''和'zlib_stream = deobfuscate_string(pnr,contents [16:])') ' –

0

功能deobfuscate_string需要一個迭代pnr,字符串obfuscatedoperation是默認。減去。

  • 對於字符串obfuscated
  • 中的每個字符c它應用 操作(默認。減去),以字符的值與在 PNR的下一個元素。
  • 然後它使用& 0xff確保結果在255範圍內
  • 然後,每件事物都以字符串組合。

因此,它只是通過旋轉已知旋轉集中的每個字符來加密輸入。

注意:該代碼是不是因爲操作不能被用這樣的方式,我只在這裏解釋的目標有效。