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); } }
public void DecryptResource(Stream stream, byte[] key) { var s = new MemoryStream(); // var asm = System.Reflection.Assembly.LoadFile(Ctx.Filename); //var x = asm.GetManifestResourceStream(Encoding.UTF8.GetString(BitConverter.GetBytes(Key0I))); var buff = new byte[stream.Length]; stream.Read(buff, 0, buff.Length); //var key = asm.Modules.ToList()[0].ResolveSignature(Key0D ^ Token1); var seed = BitConverter.ToUInt32(key, 12) * (uint)Key0I; var _m = (ushort)(seed >> 16); var _c = (ushort)(seed & 0xffff); var m = _c; var c = _m; for (var i = 0; i < buff.Length; i++) { buff[i] ^= (byte)((seed * m + c) % 0x100); m = (ushort)((seed * m + _m) % 0x10000); c = (ushort)((seed * c + _c) % 0x10000); } var str = new CryptoStream(new MemoryStream(buff), new RijndaelManaged().CreateDecryptor(key, MD5.Create().ComputeHash(key)), CryptoStreamMode.Read); { var dat = new byte[0x1000]; int read = str.Read(dat, 0, 0x1000); do { s.Write(dat, 0, read); read = str.Read(dat, 0, 0x1000); } while (read != 0); } str.Dispose(); s.Position = 0; 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]; long compressedSize = s.Length - 13; decoder.Code(s, new MemoryStream(b, true), compressedSize, outSize); ConstBuffer = b; }