|
| 1 | +/* |
| 2 | + * by uranusq https://github.com/NL0bP/aaa_emulator |
| 3 | + * |
| 4 | + * by NLObP: метод шифрации для ВСЕХ |
| 5 | + */ |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Security.Cryptography; |
| 11 | + |
| 12 | +namespace AAEmu.Commons.Cryptography |
| 13 | +{ |
| 14 | + public class DecryptCs |
| 15 | + { |
| 16 | + public static byte[] Decode(byte[] data, uint xorKey, byte[] aesKey, byte[] iv, uint num) |
| 17 | + { |
| 18 | + //------------------------------ |
| 19 | + //здесь распаковка пакетов от клиента 0005 |
| 20 | + //для дешифрации следующих пакетов iv = шифрованный предыдущий пакет |
| 21 | + byte[] plaintext; |
| 22 | + byte[] ciphertext; |
| 23 | + var data5 = new byte[data.Length]; |
| 24 | + var mIv = new byte[16]; |
| 25 | + var cnt = new int[16]; |
| 26 | + Buffer.BlockCopy(iv, 0, mIv, 0, 16); // сохраним |
| 27 | + //подбираем наилучшее смещение для правильной дешифровки пакета |
| 28 | + for (var offset = 0; offset < 16; offset++) |
| 29 | + { |
| 30 | + Buffer.BlockCopy(data, 0, data5, 0, data.Length); //получим тело пакета |
| 31 | + Buffer.BlockCopy(mIv, 0, iv, 0, 16); // восстановим IV |
| 32 | + ciphertext = DecodeXor(data5, xorKey, offset); |
| 33 | + plaintext = DecodeAes(ciphertext, aesKey, iv); |
| 34 | + cnt[offset] = CountZero(plaintext); |
| 35 | + } |
| 36 | + //окончательный вывод |
| 37 | + Buffer.BlockCopy(data, 0, data5, 0, data.Length); //получим тело пакета |
| 38 | + Buffer.BlockCopy(mIv, 0, iv, 0, 16); // восстановим IV |
| 39 | + var offs = IndexMaxCountZero(cnt); |
| 40 | + if (num == 0) { Seq = 0; } |
| 41 | + ciphertext = DecodeXor(data, xorKey, offs); |
| 42 | + plaintext = DecodeAes(ciphertext, aesKey, iv); |
| 43 | + return plaintext; |
| 44 | + } |
| 45 | + |
| 46 | + //-------------------------------------------------------------------------------------- |
| 47 | + private static int CountZero(byte[] pck) |
| 48 | + { |
| 49 | + var countZero = 0; |
| 50 | + for (var i = 2; i < pck.Length; i++) |
| 51 | + { |
| 52 | + if (pck[i] == 0) |
| 53 | + { |
| 54 | + countZero++; |
| 55 | + } |
| 56 | + } |
| 57 | + return countZero; |
| 58 | + } |
| 59 | + //-------------------------------------------------------------------------------------- |
| 60 | + private static int IndexMaxCountZero(int[] countZero) |
| 61 | + { |
| 62 | + var index = 0; |
| 63 | + var n = 0; |
| 64 | + for (var i = 0; i < countZero.Length; i++) |
| 65 | + { |
| 66 | + if (countZero[i] <= n) |
| 67 | + { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + n = countZero[i]; |
| 72 | + index = i; |
| 73 | + } |
| 74 | + return index; |
| 75 | + } |
| 76 | + //-------------------------------------------------------------------------------------- |
| 77 | + /// <summary> |
| 78 | + /// toClientEncr help function |
| 79 | + /// </summary> |
| 80 | + /// <param name="key"></param> |
| 81 | + /// <returns></returns> |
| 82 | + private static byte Add(ref uint cry) |
| 83 | + { |
| 84 | + cry += 0x2FCBD5; |
| 85 | + var n = (byte)(cry >> 0x10); |
| 86 | + n = (byte)(n & 0x0F7); |
| 87 | + return (byte)(n == 0 ? 0x0FE : n); |
| 88 | + } |
| 89 | + //-------------------------------------------------------------------------------------- |
| 90 | + internal static uint XorKey; |
| 91 | + internal static byte Seq; |
| 92 | + //-------------------------------------------------------------------------------------- |
| 93 | + /// <summary> |
| 94 | + /// decXor packet |
| 95 | + /// </summary> |
| 96 | + /// <param name="bodyPacket">packet data, starting after message-key byte</param> |
| 97 | + /// <param name="msgKey">unique key for each message</param> |
| 98 | + /// <param name="xorKey">xor key </param> |
| 99 | + /// <param name="offset">xor decryption can start from some offset)</param> |
| 100 | + /// <returns>xor decrypted packet</returns> |
| 101 | + //-------------------------------------------------------------------------------------- |
| 102 | + private static byte[] DeXor(byte[] bodyPacket, uint xorKey, uint msgKey, int offset = 0) |
| 103 | + { |
| 104 | + var length = bodyPacket.Length; |
| 105 | + var array = new byte[length]; |
| 106 | + |
| 107 | + XorKey = xorKey * xorKey & 0xffffffff; |
| 108 | + var mul = XorKey * msgKey; |
| 109 | + |
| 110 | + var cry = (0x75a024a4 ^ mul) ^ 0xC3903b6a; // 3.0.3.0 archerage.to |
| 111 | + var n = 4 * (length / 4); |
| 112 | + for (var i = n - 1 - offset; i >= 0; i--) |
| 113 | + { |
| 114 | + array[i] = (byte)(bodyPacket[i] ^ (uint)Add(ref cry)); |
| 115 | + } |
| 116 | + |
| 117 | + for (var i = n - offset; i < length; i++) |
| 118 | + { |
| 119 | + array[i] = (byte)(bodyPacket[i] ^ (uint)Add(ref cry)); |
| 120 | + } |
| 121 | + |
| 122 | + return array; |
| 123 | + } |
| 124 | + //-------------------------------------------------------------------------------------- |
| 125 | + /// <summary> |
| 126 | + /// DecodeXor: расшифровка пакета от клиента XOR ключом |
| 127 | + /// </summary> |
| 128 | + /// <param name="bodyPacket">тело пакета начиная сразу за 0005</param> |
| 129 | + /// <param name="xorKey"></param> |
| 130 | + /// <param name="offset"></param> |
| 131 | + /// <returns></returns> |
| 132 | + //-------------------------------------------------------------------------------------- |
| 133 | + public static byte[] DecodeXor(byte[] bodyPacket, uint xorKey, int offset) |
| 134 | + { |
| 135 | + var map = new Dictionary<int, int> |
| 136 | + { |
| 137 | + {0x30, 0x01}, {0x31, 0x02}, {0x32, 0x03}, {0x33, 0x04}, {0x34, 0x05}, {0x35, 0x06}, {0x36, 0x07}, {0x37, 0x08}, |
| 138 | + {0x38, 0x09}, {0x39, 0x0a}, {0x3a, 0x0b}, {0x3b, 0x0c}, {0x3c, 0x0d}, {0x3d, 0x0e}, {0x3e, 0x0f}, {0x3f, 0x10} |
| 139 | + }; |
| 140 | + var length = bodyPacket.Length; |
| 141 | + var mBodyPacket = new byte[length - 3]; |
| 142 | + Buffer.BlockCopy(bodyPacket, 3, mBodyPacket, 0, length - 3); |
| 143 | + var packet = new byte[mBodyPacket.Length]; |
| 144 | + var msgKey = (uint)(bodyPacket.Length / 16 - 1) << 4; |
| 145 | + msgKey += (uint)map[bodyPacket[2]]; |
| 146 | + packet = DeXor(mBodyPacket, xorKey, msgKey, offset); |
| 147 | + return packet; |
| 148 | + } |
| 149 | + //-------------------------------------------------------------------------------------- |
| 150 | + private const int Size = 16; |
| 151 | + //-------------------------------------------------------------------------------------- |
| 152 | + private static RijndaelManaged CryptAes(byte[] aesKey, byte[] iv) |
| 153 | + { |
| 154 | + var rm = new RijndaelManaged |
| 155 | + { |
| 156 | + KeySize = 128, |
| 157 | + BlockSize = 128, |
| 158 | + Padding = PaddingMode.None, |
| 159 | + Mode = CipherMode.CBC, |
| 160 | + Key = aesKey, |
| 161 | + IV = iv |
| 162 | + }; |
| 163 | + return rm; |
| 164 | + } |
| 165 | + //-------------------------------------------------------------------------------------- |
| 166 | + /// <summary> |
| 167 | + /// DecodeAes: расшифровка пакета от клиента AES ключом |
| 168 | + /// </summary> |
| 169 | + /// <param name="cipherData"></param> |
| 170 | + /// <param name="aesKey"></param> |
| 171 | + /// <param name="iv"></param> |
| 172 | + /// <returns></returns> |
| 173 | + //-------------------------------------------------------------------------------------- |
| 174 | + public static byte[] DecodeAes(byte[] cipherData, byte[] aesKey, byte[] iv) |
| 175 | + { |
| 176 | + var mIv = new byte[16]; |
| 177 | + Buffer.BlockCopy(iv, 0, mIv, 0, Size); |
| 178 | + var len = cipherData.Length / Size; |
| 179 | + //Save last 16 bytes in IV |
| 180 | + Buffer.BlockCopy(cipherData, (len - 1) * Size, iv, 0, Size); |
| 181 | + // Create a MemoryStream that is going to accept the decrypted bytes |
| 182 | + using (var memoryStream = new MemoryStream()) |
| 183 | + { |
| 184 | + // Create a symmetric algorithm. |
| 185 | + // We are going to use RijndaelRijndael because it is strong and available on all platforms. |
| 186 | + // You can use other algorithms, to do so substitute the next line with something like |
| 187 | + // TripleDES alg = TripleDES.Create(); |
| 188 | + using (var alg = CryptAes(aesKey, mIv)) |
| 189 | + { |
| 190 | + // Create a CryptoStream through which we are going to be pumping our data. |
| 191 | + // CryptoStreamMode.Write means that we are going to be writing data to the stream |
| 192 | + // and the output will be written in the MemoryStream we have provided. |
| 193 | + using (var cs = new CryptoStream(memoryStream, alg.CreateDecryptor(), CryptoStreamMode.Write)) |
| 194 | + { |
| 195 | + // Write the data and make it do the decryption |
| 196 | + cs.Write(cipherData, 0, cipherData.Length); |
| 197 | + |
| 198 | + // Close the crypto stream (or do FlushFinalBlock). |
| 199 | + // This will tell it that we have done our decryption and there is no more data coming in, |
| 200 | + // and it is now a good time to remove the padding and finalize the decryption process. |
| 201 | + cs.FlushFinalBlock(); |
| 202 | + cs.Close(); |
| 203 | + } |
| 204 | + } |
| 205 | + // Now get the decrypted data from the MemoryStream. |
| 206 | + // Some people make a mistake of using GetBuffer() here, which is not the right way. |
| 207 | + var decryptedData = memoryStream.ToArray(); |
| 208 | + return decryptedData; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments