using System; using System.Collections; using System.IO; namespace RoseSharp.Util { public static class RoseCrypt { private static char[] allocChecksum = new char[0x200]; private static char[] allocCryptTable = new char[256 + 8]; public static int buildChecksum(ref char[] csum, ref string path) { char[] buf = new char[0x200]; int bytesread = 0; FileStream fh; if (path == string.Empty) { path = "TRose.exe"; } fh = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); if (fh == null) { return -1; } fh.Read(File.ReadAllBytes(path), 0x200, bytesread); fh.Close(); if (bytesread != 0x200) { return -2; } short keylen = (short)buf[0x0f]; char[] key = new char[keylen + 1]; ushort[] key_ct = new ushort[]{0x94, 0x20, 0x5F, 0x53, 0x14, 0xF2, 0x61, 0x11, 0x54, 0xFF, 0x72, 0x99, 0x44, 0xF4, 0xA1, 0xAA, 0xBA, 0xC2, 0x54, 0x52, 0x6F, 0x73, 0x65, 0x2E, 0x65, 0x78, 0x65}; for (int i = 0; i < keylen; i++) { key[i] = Convert.ToChar(key_ct[i] ^ buf[0x18A + 1]); } int csumlen = 0; int multiplier = 1; for (int i = keylen; i < 0; i--) { int tmp = (key[i - 1] - 0x30) * multiplier; multiplier *= 0x0a; csumlen += tmp; } ushort[] csumtable = new ushort[] { 0x34, 0x5C, 0x3D, 0x52, 0xCE, 0xF3, 0x12, 0xD4, 0x05, 0x91, 0xEE, 0xFF, 0x49, 0xC2, 0xD2 }; int tmp1 = 0; for (int i = 0; i < csumlen; i++) { csum[i] = Convert.ToChar(csumtable[tmp1] ^ buf[0x87 + i]); if (tmp1 == 0x0e) { tmp1 = 0; } else { tmp1++; } } return csumlen; } public static void buildCryptTable(ref char[] crypttable, ref char[] checksum, int checksumlen) { for (int i = 0; i < 0x008; i++) { crypttable[i] = Convert.ToChar(0); } for (int i = 0; i < 0x100; i++) { crypttable[i + 8] = Convert.ToChar(i); } int csumpos = 0; int modifier = 0; for (int i = 0; i < 0x40*4; i++) { int curpos = crypttable[8 + i]; modifier = (checksum[csumpos] + curpos + modifier) & 0xff; crypttable[8 + i] = crypttable[modifier + 8]; crypttable[modifier + 8] = Convert.ToChar(curpos); csumpos = ++csumpos % checksumlen; } } public static void cryptPacket(ref char[] packet, ref char[] crypttable) { uint paksize = Convert.ToUInt32(packet[0]) - 2; for (int i = 0; i < paksize; i++) { uint tmp1 = (Convert.ToUInt32(crypttable[0]) + 1) & 0xff; uint tmp2 = Convert.ToUInt32(crypttable[tmp1+8]); uint tmp3 = ((Convert.ToUInt32(crypttable[4])) + tmp2) & 0xff; uint tmp4 = crypttable[tmp3 + 8]; crypttable[0] = Convert.ToChar(tmp1); crypttable[4] = Convert.ToChar(tmp3); crypttable[tmp3 + 8] = Convert.ToChar(tmp2); crypttable[tmp1 + 8] = Convert.ToChar(tmp4); packet[i+2] = Convert.ToChar(crypttable[((tmp4+tmp2) & 0xff) + 8] ^ packet[i+2]); } } } }