public static String Decrypt(Byte[] mapKey, Byte[] encrypted) { Sha1 sha1 = new Sha1(); Byte[] xorKey = new Byte[encrypted.Length + 19]; UInt32 xorKeyLength = 0; int counter = 0; while (xorKeyLength < encrypted.Length) { Byte[] counterStringBytes = Encoding.ASCII.GetBytes(counter.ToString()); sha1.Add(counterStringBytes, 0, counterStringBytes.Length); sha1.Add(mapKey, 0, mapKey.Length); sha1.Add(sharedServerSecret, 0, sharedServerSecret.Length); sha1.Add(counterStringBytes, 0, counterStringBytes.Length); UInt32[] hash = sha1.Finish(); xorKey.BigEndianSetUInt32(xorKeyLength + 0, hash[0]); xorKey.BigEndianSetUInt32(xorKeyLength + 4, hash[1]); xorKey.BigEndianSetUInt32(xorKeyLength + 8, hash[2]); xorKey.BigEndianSetUInt32(xorKeyLength + 12, hash[3]); xorKey.BigEndianSetUInt32(xorKeyLength + 16, hash[4]); xorKeyLength += 20; sha1.Reset(); counter++; } Char[] decrypted = new Char[encrypted.Length]; for (int i = 0; i < decrypted.Length; i++) { decrypted[i] = (Char)(xorKey[i] ^ encrypted[i]); } return(new String(decrypted)); }
public void TestBigEndianSerializationForLargeIntegerTypes() { Byte[] buffer = new Byte[256]; // UInt32 { Console.WriteLine("-----------------------------------------"); Console.WriteLine("Testing UInt32..."); Console.WriteLine("-----------------------------------------"); UInt32[] testValues = new UInt32[] { 0, 0x80, 0x800000, 0x80000000, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFF, 0xFFFFFFFF, 0x7F, 0x7FFFFF, 0x7FFFFFFF, 0x100, 0x80, 0xFF, 1, 2, 0x128EF92E, 0xFF00FF00, 0x12345678 }; for (UInt32 i = 0; i < testValues.Length; i++) { UInt32 testValue = testValues[i]; Console.WriteLine(testValue); Console.WriteLine(testValue); buffer.BigEndianSetUInt32(i, testValue); Assert.AreEqual((Byte)(testValue >> 24), buffer[i]); Assert.AreEqual((Byte)(testValue >> 16), buffer[i + 1]); Assert.AreEqual((Byte)(testValue >> 8), buffer[i + 2]); Assert.AreEqual((Byte)testValue, buffer[i + 3]); Assert.AreEqual(testValue, buffer.BigEndianReadUInt32(i)); } } // Int32 { Console.WriteLine("-----------------------------------------"); Console.WriteLine("Testing Int32..."); Console.WriteLine("-----------------------------------------"); Int32[] testValues = new Int32[] { 0, 0x80, 0x800000, -1, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFF, 0x7F, 0x7FFFFF, 0x7FFFFFFF, 0x100, 0x80, 0xFF, 1, 2, 0x128EF92E, 0x7F00FF00, 0x12345678 }; for (UInt32 i = 0; i < testValues.Length; i++) { Int32 testValue = testValues[i]; Console.WriteLine(testValue); buffer.BigEndianSetInt32(i, testValue); Assert.AreEqual((Byte)(testValue >> 24), buffer[i]); Assert.AreEqual((Byte)(testValue >> 16), buffer[i + 1]); Assert.AreEqual((Byte)(testValue >> 8), buffer[i + 2]); Assert.AreEqual((Byte)testValue, buffer[i + 3]); Assert.AreEqual(testValue, buffer.BigEndianReadInt32(i)); } for (UInt32 i = 0; i < testValues.Length; i++) { Int32 testValue = -testValues[i]; Console.WriteLine(testValue); buffer.BigEndianSetInt32(i, testValue); Assert.AreEqual((Byte)(testValue >> 24), buffer[i]); Assert.AreEqual((Byte)(testValue >> 16), buffer[i + 1]); Assert.AreEqual((Byte)(testValue >> 8), buffer[i + 2]); Assert.AreEqual((Byte)testValue, buffer[i + 3]); Assert.AreEqual(testValue, buffer.BigEndianReadInt32(i)); } } /* * // UInt64 * { * Console.WriteLine("-----------------------------------------"); * Console.WriteLine("Testing UInt64..."); * Console.WriteLine("-----------------------------------------"); * UInt64[] testValues = new UInt64[] { 0, 0x80, 0x800000, 0x80000000, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFF, 0xFFFFFFFF, * 0x7F, 0x7FFFFF, 0x7FFFFFFF, 0x100, 0x80, 0xFF, 1, 2, 0x128EF92E, 0xFF00FF00, 0x12345678 }; * for (int i = 0; i < testValues.Length; i++) * { * UInt64 testValue = testValues[i]; * Console.WriteLine(testValue); * Console.WriteLine(testValue); * buffer.BigEndianSetUInt64(i, testValue); * Assert.AreEqual((Byte)(testValue >> 24), buffer[i]); * Assert.AreEqual((Byte)(testValue >> 16), buffer[i + 1]); * Assert.AreEqual((Byte)(testValue >> 8), buffer[i + 2]); * Assert.AreEqual((Byte)testValue, buffer[i + 3]); * Assert.AreEqual(testValue, buffer.BigEndianReadUInt64(i)); * } * } */ }