private IEnumerable<EmbeddedResource> DecryptResources(string resName) { var res = Ctx.Assembly.ManifestModule.Resources.First(x => x.Name == resName) as EmbeddedResource; if (res == null) throw new Exception("Resource not found: " + resName); Stream resStream = new MemoryStream(res.GetResourceData()); var buff = new byte[resStream.Length]; resStream.Read(buff, 0, buff.Length); var num = (byte)DemutatedKeys["res"].DemutatedInts[0]; for (var i = 0; i < buff.Length; i++) { buff[i] = (byte)(buff[i] ^ num); num = (byte)((num * DemutatedKeys["res"].DemutatedInts[1]) % 256); } var s = new MemoryStream(buff); var decoder = new Lzma.LzmaDecoder(); var prop = new byte[5]; s.Read(prop, 0, 5); decoder.SetDecoderProperties(prop); long outSize = 0; for (int i = 0; i < 8; i++) { int v = s.ReadByte(); if (v < 0) throw (new Exception()); outSize |= ((long)(byte)v) << (8 * i); } var b = new byte[outSize]; var z = new MemoryStream(b, true); long compressedSize = s.Length - 13; decoder.Code(s, z, compressedSize, outSize); z.Position = 0; using (var rdr = new BinaryReader(z)) { buff = rdr.ReadBytes(rdr.ReadInt32()); var tmpAsm = AssemblyDef.Load(buff); foreach (var res2 in tmpAsm.ManifestModule.Resources.Where(r => r is EmbeddedResource)) yield return res2 as EmbeddedResource; } yield return null; }
static byte[] Decrypt(byte[] asm, int key0I) { byte[] dat; byte[] iv; byte[] key; using (var rdr = new BinaryReader(new MemoryStream(asm))) { dat = rdr.ReadBytes(rdr.ReadInt32()); iv = rdr.ReadBytes(rdr.ReadInt32()); key = rdr.ReadBytes(rdr.ReadInt32()); } var key0 = key0I; for (var j = 0; j < key.Length; j += 4) { key[j + 0] ^= (byte)((key0 & 0x000000ff) >> 0); key[j + 1] ^= (byte)((key0 & 0x0000ff00) >> 8); key[j + 2] ^= (byte)((key0 & 0x00ff0000) >> 16); key[j + 3] ^= (byte)((key0 & 0xff000000) >> 24); } var rijn = new RijndaelManaged(); using (var s = new CryptoStream(new MemoryStream(dat), rijn.CreateDecryptor(key, iv), CryptoStreamMode.Read)) { var l = new byte[4]; s.Read(l, 0, 4); var len = BitConverter.ToUInt32(l, 0); var decoder = new Lzma.LzmaDecoder(); var prop = new byte[5]; s.Read(prop, 0, 5); decoder.SetDecoderProperties(prop); long outSize = 0; for (var i = 0; i < 8; i++) { var v = s.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } var ret = new byte[outSize]; long compressedSize = len - 13; decoder.Code(s, new MemoryStream(ret, true), compressedSize, outSize); return ret; } }