2015-08-21 48 views
-2

我有兩個十六進制數字,其中,爲了試驗OTP的目的,我試圖與C#異或。不幸的是,這兩個數字均爲數百位數字 - 顯然太大而無法存儲在intlong中。我如何存儲/異或他們?現在,我以如下方式存儲爲BigIntegerC#按位異或兩個非常大的十六進制數字

public static string XOR(string string_1, string string_2){ 
      BigInteger b1 = BigInteger.Parse(string_1, System.Globalization.NumberStyles.AllowHexSpecifier); 
      BigInteger b2 = BigInteger.Parse(string_2, System.Globalization.NumberStyles.AllowHexSpecifier); 
      BigInteger retVal = b1^b2; 
      return retVal.ToString("X"); 
     } 

這不會產生預期結果。謝謝!

編輯:

輸入:

string_1 = 32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce 
string_2 = 71946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027 

預期結果(根據Python和this online XOR calculator):

32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5b83624730b208d83b237176b5a41e13d1a2c0080f55d6fb05e4fd9a6e8aff84a9eec74ec0e3115dd0808c011baa15b2c29edad06d6c319976fc7c7eb6a8727e79906c96397dd14594a17511e2ba018c3267935877b5c2c1750f28b2d5bf55faa6c2218c30e58f17542717ad6f8622dd0069a4886d20d3d657a80a869c8f6025399f914f23e5ccd3a999c271a50994c7db959c5c0b73334d15ba3754e9 
+5

'byte []'有什麼問題。順便說一句:'哪個產生不了預期的結果?你的輸入是什麼?什麼是預期的輸出? – Eser

+1

if((b1^b2)^ b2)== b1那麼它的工作正常 – maxpovver

+2

也加上實際結果請 – maxpovver

回答

5

似乎有人想不相信。

var string_1 = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce"; 
var string_2 = "71946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027"; 

//copied from https://xor.pw/? 
string expectedResult = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba3624730b208d83b237176b5a41e13d1a2c0080f55d6fb05e4fd9a6e8aff84a9eec74ec0e3115dd0808c011baa15b2c29edad06d6c319976fc7c7eb6a8727e79906c96397dd14594a17511e2ba018c3267935877b5c2c1750f28b2d5bf55faa6c2218c30e58f17542717ad6f8622dd0069a4886d20d3d657a80a869c8f6025399f914f23e5ccd3a999c271a50994c7db959c5c0b73334d15ba3754e9"; 

BigInteger b1 = BigInteger.Parse(string_1, NumberStyles.HexNumber); 
BigInteger b2 = BigInteger.Parse(string_2, NumberStyles.HexNumber); 
BigInteger retVal = b1^b2; 
var res = retVal.ToString("X").ToLower() == expectedResult; 

猜測res在回答。當然真實。 :)

+0

我不明白...這是爲我工作,但是當我做了同樣的事情作爲一個方法,得到發送兩個字符串,它停止工作......我從字面上複製並粘貼此代碼,然後使'string_1 '和'string_2'參數來代替... – KnightOfNi

+0

@KnightOfNi當你可以發佈一個可重現的問題時, – Eser

相關問題