Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Kaibou.Network.Security
- {
- using System;
- using Kaibou.Network.Sockets;
- public unsafe class AuthCryptography : ICipher
- {
- public class CryptCounter
- {
- public CryptCounter()
- {
- }
- public CryptCounter(ushort with)
- {
- m_Counter = with;
- }
- UInt16 m_Counter = 0;
- public byte Key2
- {
- get { return (byte)(m_Counter >> 8); }
- }
- public byte Key1
- {
- get { return (byte)(m_Counter & 0xFF); }
- }
- public void Increment()
- {
- m_Counter++;
- }
- }
- private CryptCounter _decryptCounter;
- private CryptCounter _encryptCounter;
- private static byte[] _cryptKey1;
- private static byte[] _cryptKey2;
- private static byte[] _cryptKey3;
- private static byte[] _cryptKey4;
- private static bool Decrypt2 = false;
- public static void PrepareAuthCryptography()
- {
- if (_cryptKey1 != null)
- {
- if (_cryptKey1.Length != 0)
- return;
- }
- _cryptKey1 = new byte[0x100];
- _cryptKey2 = new byte[0x100];
- byte i_key1 = 0x9D;
- byte i_key2 = 0x62;
- for (int i = 0; i < 0x100; i++)
- {
- _cryptKey1[i] = i_key1;
- _cryptKey2[i] = i_key2;
- i_key1 = (byte)((0x0F + (byte)(i_key1 * 0xFA)) * i_key1 + 0x13);
- i_key2 = (byte)((0x79 - (byte)(i_key2 * 0x5C)) * i_key2 + 0x6D);
- }
- }
- public AuthCryptography()
- {
- _encryptCounter = new CryptCounter();
- _decryptCounter = new CryptCounter();
- }
- public byte[] Encrypt(byte[] buffer, int length)
- {
- byte[] result = new byte[length];
- for (int i = 0; i < length; i++)
- {
- result[i] ^= (byte)(buffer[i] ^ 0xAB);
- result[i] = (byte)(result[i] >> 4 | result[i] << 4);
- result[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
- _encryptCounter.Increment();
- }
- return result;
- }
- public byte[] Decrypt(byte[] buffer, int length)
- {
- byte[] result = new byte[length];
- if (!Decrypt2)
- {
- for (int i = 0; i < length; i++)
- {
- result[i] ^= (byte)(buffer[i] ^ 0xAB);
- result[i] = (byte)(result[i] >> 4 | result[i] << 4);
- result[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
- _decryptCounter.Increment();
- }
- }
- else
- {
- for (int i = 0; i < length; i++)
- {
- result[i] ^= (byte)(buffer[i] ^ 0xAB);
- result[i] = (byte)(result[i] >> 4 | result[i] << 4);
- result[i] ^= (byte)(_cryptKey4[_decryptCounter.Key2] ^ _cryptKey3[_decryptCounter.Key1]);
- _decryptCounter.Increment();
- }
- }
- return result;
- }
- public void Decrypt(byte[] packet, byte[] buffer, int length, int position)
- {
- if (!Decrypt2)
- {
- for (int i = position; i < length + position; i++)
- {
- packet[i] ^= (byte)(buffer[i - position] ^ 0xAB);
- packet[i] = (byte)(packet[i] >> 4 | packet[i] << 4);
- packet[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
- _decryptCounter.Increment();
- }
- }
- else
- {
- for (int i = position; i < length + position; i++)
- {
- packet[i] ^= (byte)(buffer[i - position] ^ 0xAB);
- packet[i] = (byte)(packet[i] >> 4 | packet[i] << 4);
- packet[i] ^= (byte)(_cryptKey4[_decryptCounter.Key2] ^ _cryptKey3[_decryptCounter.Key1]);
- _decryptCounter.Increment();
- }
- }
- }
- public static void GenerateKeys(UInt32 CryptoKey, UInt32 AccountID)
- {
- UInt32 tmpkey1 = 0, tmpkey2 = 0;
- tmpkey1 = ((CryptoKey + AccountID) ^ (0x4321)) ^ CryptoKey;
- tmpkey2 = tmpkey1 * tmpkey1;
- _cryptKey3 = new byte[256];
- _cryptKey4 = new byte[256];
- for (int i = 0; i < 256; i++)
- {
- int right = ((3 - (i % 4)) * 8);
- int left = ((i % 4)) * 8 + right;
- _cryptKey3[i] = (byte)(_cryptKey1[i] ^ tmpkey1 << right >> left);
- _cryptKey4[i] = (byte)(_cryptKey2[i] ^ tmpkey2 << right >> left);
- }
- Decrypt2 = true;
- }
- public static void GenerateKeys2(byte[] InKey1, byte[] InKey2)
- {
- byte[] addKey1 = new byte[4];
- byte[] addKey2 = new byte[4];
- byte[] addResult = new byte[4];
- byte[] tempKey = new byte[4];
- long LMULer;
- _cryptKey3 = new byte[256];
- _cryptKey4 = new byte[256];
- for (int x = 0; x < 4; x++)
- {
- addKey1[x] = InKey1[3 - x];
- addKey2[x] = InKey2[3 - x];
- }
- uint Adder1;
- uint Adder2;
- uint Adder3;
- Adder1 = (uint)((addKey1[3] << 24) | (addKey1[2] << 16) | (addKey1[1] << 8) | (addKey1[0]));
- Adder2 = (uint)((addKey2[3] << 24) | (addKey2[2] << 16) | (addKey2[1] << 8) | (addKey2[0]));
- Adder3 = Adder1 + Adder2;
- addResult[0] = (byte)(Adder3 & 0xff);
- addResult[1] = (byte)((Adder3 >> 8) & 0xff);
- addResult[2] = (byte)((Adder3 >> 16) & 0xff);
- addResult[3] = (byte)((Adder3 >> 24) & 0xff);
- for (int b = 3; b >= 0; b--)
- {
- tempKey[3 - b] = addResult[b];
- }
- tempKey[2] = (byte)(tempKey[2] ^ (byte)0x43);
- tempKey[3] = (byte)(tempKey[3] ^ (byte)0x21);
- for (int b = 0; b < 4; b++)
- {
- tempKey[b] = (byte)(tempKey[b] ^ InKey1[b]);
- }
- for (int b = 0; b < 256; b++)
- {
- _cryptKey3[b] = (byte)(tempKey[3 - (b % 4)] ^ _cryptKey1[b]);
- }
- for (int x = 0; x < 4; x++)
- {
- addResult[x] = tempKey[3 - x];
- }
- Adder3 = (uint)((addResult[3] << 24) | (addResult[2] << 16) | (addResult[1] << 8) | (addResult[0]));
- LMULer = Adder3 * Adder3;
- LMULer = LMULer << 32;
- LMULer = LMULer >> 32;
- Adder3 = Convert.ToUInt32(LMULer & 0xffffffff);
- addResult[0] = (byte)(Adder3 & 0xff);
- addResult[1] = (byte)((Adder3 >> 8) & 0xff);
- addResult[2] = (byte)((Adder3 >> 16) & 0xff);
- addResult[3] = (byte)((Adder3 >> 24) & 0xff);
- for (int b = 3; b >= 0; b--)
- {
- tempKey[3 - b] = addResult[b];
- }
- for (int b = 0; b < 256; b++)
- {
- _cryptKey4[b] = Convert.ToByte(tempKey[3 - (b % 4)] ^ _cryptKey2[b]);
- }
- Decrypt2 = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement