我有兩個十六進制數字,其中,爲了試驗OTP的目的,我試圖與C#異或。不幸的是,這兩個數字均爲數百位數字 - 顯然太大而無法存儲在int
或long
中。我如何存儲/異或他們?現在,我以如下方式存儲爲BigInteger
:C#按位異或兩個非常大的十六進制數字
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
'byte []'有什麼問題。順便說一句:'哪個產生不了預期的結果?你的輸入是什麼?什麼是預期的輸出? – Eser
if((b1^b2)^ b2)== b1那麼它的工作正常 – maxpovver
也加上實際結果請 – maxpovver