2012-02-21 00:20:29 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-03-27 02:12:56 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
using Mono.MyStuff;
|
2012-04-10 22:32:15 +08:00
|
|
|
|
using de4dot.PE;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.MaxtoCode {
|
2012-02-21 18:51:19 +08:00
|
|
|
|
// Decrypts methods and resources
|
2012-02-21 00:20:29 +08:00
|
|
|
|
class FileDecrypter {
|
|
|
|
|
MainType mainType;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
PeImage peImage;
|
|
|
|
|
PeHeader peHeader;
|
|
|
|
|
McKey mcKey;
|
|
|
|
|
byte[] fileData;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
class PeHeader {
|
|
|
|
|
const int XOR_KEY = 0x7ABF931;
|
|
|
|
|
|
|
|
|
|
byte[] headerData;
|
2012-02-21 18:51:19 +08:00
|
|
|
|
uint rvaDispl1;
|
|
|
|
|
uint rvaDispl2;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
2012-02-22 19:14:15 +08:00
|
|
|
|
public PeHeader(MainType mainType, PeImage peImage) {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
headerData = getPeHeaderData(peImage);
|
|
|
|
|
|
2012-02-22 19:14:15 +08:00
|
|
|
|
if (!mainType.IsOld && peImage.readUInt32(0x2008) != 0x48) {
|
2012-02-21 18:51:19 +08:00
|
|
|
|
rvaDispl1 = readUInt32(0x0FB0) ^ XOR_KEY;
|
|
|
|
|
rvaDispl2 = readUInt32(0x0FB4) ^ XOR_KEY;
|
|
|
|
|
}
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
public uint getMcKeyRva() {
|
2012-02-21 18:51:19 +08:00
|
|
|
|
return getRva2(0x0FFC, XOR_KEY);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 18:51:19 +08:00
|
|
|
|
public uint getRva1(int offset, uint xorKey) {
|
|
|
|
|
return (readUInt32(offset) ^ xorKey) - rvaDispl1;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 18:51:19 +08:00
|
|
|
|
public uint getRva2(int offset, uint xorKey) {
|
|
|
|
|
return (readUInt32(offset) ^ xorKey) - rvaDispl2;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
public uint readUInt32(int offset) {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
return BitConverter.ToUInt32(headerData, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static byte[] getPeHeaderData(PeImage peImage) {
|
|
|
|
|
var data = new byte[0x1000];
|
|
|
|
|
|
|
|
|
|
var firstSection = peImage.Sections[0];
|
|
|
|
|
readTo(peImage, data, 0, 0, firstSection.pointerToRawData);
|
|
|
|
|
|
|
|
|
|
foreach (var section in peImage.Sections) {
|
|
|
|
|
if (section.virtualAddress >= data.Length)
|
|
|
|
|
continue;
|
|
|
|
|
int offset = (int)section.virtualAddress;
|
|
|
|
|
readTo(peImage, data, offset, section.pointerToRawData, section.sizeOfRawData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void readTo(PeImage peImage, byte[] data, int destOffset, uint imageOffset, uint maxLength) {
|
|
|
|
|
if (destOffset > data.Length)
|
|
|
|
|
return;
|
|
|
|
|
int len = Math.Min(data.Length - destOffset, (int)maxLength);
|
|
|
|
|
var newData = peImage.offsetReadBytes(imageOffset, len);
|
|
|
|
|
Array.Copy(newData, 0, data, destOffset, newData.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
class McKey {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
PeHeader peHeader;
|
|
|
|
|
byte[] data;
|
|
|
|
|
|
2012-02-21 18:51:19 +08:00
|
|
|
|
public byte this[int index] {
|
|
|
|
|
get { return data[index]; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
public McKey(PeImage peImage, PeHeader peHeader) {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
this.peHeader = peHeader;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
this.data = peImage.readBytes(peHeader.getMcKeyRva(), 0x2000);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte readByte(int offset) {
|
|
|
|
|
return data[offset];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint readUInt32(int offset) {
|
|
|
|
|
return BitConverter.ToUInt32(data, offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
enum EncryptionVersion {
|
|
|
|
|
Unknown,
|
|
|
|
|
V1,
|
|
|
|
|
V2,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
V3,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
}
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
class EncryptionInfo {
|
|
|
|
|
public uint MagicLo { get; set; }
|
|
|
|
|
public uint MagicHi { get; set; }
|
|
|
|
|
public EncryptionVersion Version { get; set; }
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
static EncryptionInfo[] encryptionInfos_Rva900h = new EncryptionInfo[] {
|
|
|
|
|
// PE header timestamp
|
2012-02-22 19:38:02 +08:00
|
|
|
|
// 462FA2D2 = Wed, 25 Apr 2007 18:49:54 (3.20)
|
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xA098B387,
|
|
|
|
|
MagicHi = 0x1E8EBCA3,
|
|
|
|
|
Version = EncryptionVersion.V1,
|
|
|
|
|
},
|
2012-02-22 19:14:15 +08:00
|
|
|
|
// 482384FB = Thu, 08 May 2008 22:55:55 (3.36)
|
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xAA98B387,
|
|
|
|
|
MagicHi = 0x1E8EECA3,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-22 19:14:15 +08:00
|
|
|
|
},
|
2012-02-21 16:26:05 +08:00
|
|
|
|
// 4A5EEC64 = Thu, 16 Jul 2009 09:01:24
|
2012-02-23 17:59:02 +08:00
|
|
|
|
// 4C6220EC = Wed, 11 Aug 2010 04:02:52
|
|
|
|
|
// 4C622357 = Wed, 11 Aug 2010 04:13:11
|
2012-02-21 16:26:05 +08:00
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xAA98B387,
|
|
|
|
|
MagicHi = 0x128EECA3,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
// 4C6E4605 = Fri, 20 Aug 2010 09:08:21
|
2012-02-23 17:59:02 +08:00
|
|
|
|
// 4D0E220D = Sun, 19 Dec 2010 15:17:33
|
|
|
|
|
// 4DC2FC75 = Thu, 05 May 2011 19:37:25
|
|
|
|
|
// 4DFA3D5D = Thu, 16 Jun 2011 17:29:01
|
2012-02-21 16:26:05 +08:00
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xAA98B387,
|
|
|
|
|
MagicHi = 0xF28EECA3,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
// 4DC2FE0C = Thu, 05 May 2011 19:44:12
|
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xAA98B387,
|
|
|
|
|
MagicHi = 0xF28EEAA3,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
// 4ED76740 = Thu, 01 Dec 2011 11:38:40
|
2012-02-23 17:59:02 +08:00
|
|
|
|
// 4EE1FAD1 = Fri, 09 Dec 2011 12:10:57
|
2012-02-21 16:26:05 +08:00
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0xAA983B87,
|
|
|
|
|
MagicHi = 0xF28EECA3,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V3,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
static EncryptionInfo[] encryptionInfos_McKey8C0h = new EncryptionInfo[] {
|
2012-02-22 19:38:02 +08:00
|
|
|
|
// 462FA2D2 = Wed, 25 Apr 2007 18:49:54 (3.20)
|
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0x6AA13B13,
|
|
|
|
|
MagicHi = 0xD72B991F,
|
|
|
|
|
Version = EncryptionVersion.V1,
|
|
|
|
|
},
|
2012-02-22 19:14:15 +08:00
|
|
|
|
// 482384FB = Thu, 08 May 2008 22:55:55 (3.36)
|
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0x6A713B13,
|
|
|
|
|
MagicHi = 0xD72B891F,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-22 19:14:15 +08:00
|
|
|
|
},
|
2012-02-21 16:26:05 +08:00
|
|
|
|
// 4A5EEC64 = Thu, 16 Jul 2009 09:01:24
|
2012-02-23 17:59:02 +08:00
|
|
|
|
// 4C6220EC = Wed, 11 Aug 2010 04:02:52
|
|
|
|
|
// 4C622357 = Wed, 11 Aug 2010 04:13:11
|
|
|
|
|
// 4C6E4605 = Fri, 20 Aug 2010 09:08:21
|
|
|
|
|
// 4D0E220D = Sun, 19 Dec 2010 15:17:33
|
|
|
|
|
// 4DC2FC75 = Thu, 05 May 2011 19:37:25
|
|
|
|
|
// 4DC2FE0C = Thu, 05 May 2011 19:44:12
|
|
|
|
|
// 4DFA3D5D = Thu, 16 Jun 2011 17:29:01
|
2012-02-21 16:26:05 +08:00
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0x6A713B13,
|
|
|
|
|
MagicHi = 0xD72B891F,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V2,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
// 4ED76740 = Thu, 01 Dec 2011 11:38:40
|
2012-02-23 17:59:02 +08:00
|
|
|
|
// 4EE1FAD1 = Fri, 09 Dec 2011 12:10:57
|
2012-02-21 16:26:05 +08:00
|
|
|
|
new EncryptionInfo {
|
|
|
|
|
MagicLo = 0x6A731B13,
|
|
|
|
|
MagicHi = 0xD72B891F,
|
2012-02-22 19:38:02 +08:00
|
|
|
|
Version = EncryptionVersion.V3,
|
2012-02-21 16:26:05 +08:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2012-02-21 00:20:29 +08:00
|
|
|
|
class MethodInfos {
|
2012-02-22 19:14:15 +08:00
|
|
|
|
MainType mainType;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
PeImage peImage;
|
|
|
|
|
PeHeader peHeader;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
McKey mcKey;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
uint structSize;
|
|
|
|
|
uint methodInfosOffset;
|
|
|
|
|
uint encryptedDataOffset;
|
|
|
|
|
uint xorKey;
|
|
|
|
|
Dictionary<uint, DecryptedMethodInfo> infos = new Dictionary<uint, DecryptedMethodInfo>();
|
2012-02-21 16:26:05 +08:00
|
|
|
|
IDecrypter decrypter;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
const int ENCRYPTED_DATA_INFO_SIZE = 0x13;
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
public class DecryptedMethodInfo {
|
|
|
|
|
public uint bodyRva;
|
|
|
|
|
public byte[] body;
|
|
|
|
|
|
|
|
|
|
public DecryptedMethodInfo(uint bodyRva, byte[] body) {
|
|
|
|
|
this.bodyRva = bodyRva;
|
|
|
|
|
this.body = body;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
public MethodInfos(MainType mainType, PeImage peImage, PeHeader peHeader, McKey mcKey) {
|
2012-02-22 19:14:15 +08:00
|
|
|
|
this.mainType = mainType;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
this.peImage = peImage;
|
|
|
|
|
this.peHeader = peHeader;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
this.mcKey = mcKey;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
structSize = getStructSize(mcKey);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
uint methodInfosRva = peHeader.getRva2(0x0FF8, mcKey.readUInt32(0x005A));
|
|
|
|
|
uint encryptedDataRva = peHeader.getRva2(0x0FF0, mcKey.readUInt32(0x0046));
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
methodInfosOffset = peImage.rvaToOffset(methodInfosRva);
|
|
|
|
|
encryptedDataOffset = peImage.rvaToOffset(encryptedDataRva);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
static uint getStructSize(McKey mcKey) {
|
|
|
|
|
uint magicLo = mcKey.readUInt32(0x8C0);
|
|
|
|
|
uint magicHi = mcKey.readUInt32(0x8C4);
|
|
|
|
|
foreach (var info in encryptionInfos_McKey8C0h) {
|
2012-02-22 19:14:15 +08:00
|
|
|
|
if (magicLo == info.MagicLo && magicHi == info.MagicHi)
|
2012-02-21 16:26:05 +08:00
|
|
|
|
return 0xC + 6 * ENCRYPTED_DATA_INFO_SIZE;
|
|
|
|
|
}
|
|
|
|
|
return 0xC + 3 * ENCRYPTED_DATA_INFO_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EncryptionVersion getVersion() {
|
|
|
|
|
uint m1lo = peHeader.readUInt32(0x900);
|
|
|
|
|
uint m1hi = peHeader.readUInt32(0x904);
|
2012-03-27 01:31:45 +08:00
|
|
|
|
uint m2lo = mcKey.readUInt32(0x8C0);
|
|
|
|
|
uint m2hi = mcKey.readUInt32(0x8C4);
|
2012-02-21 16:26:05 +08:00
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
foreach (var info in encryptionInfos_McKey8C0h) {
|
2012-02-21 16:26:05 +08:00
|
|
|
|
if (info.MagicLo == m2lo && info.MagicHi == m2hi)
|
|
|
|
|
return info.Version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var info in encryptionInfos_Rva900h) {
|
|
|
|
|
if (info.MagicLo == m1lo && info.MagicHi == m1hi)
|
|
|
|
|
return info.Version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.w("Could not detect MC version. Magic1: {0:X8} {1:X8}, Magic2: {2:X8} {3:X8}", m1lo, m1hi, m2lo, m2hi);
|
|
|
|
|
return EncryptionVersion.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 00:20:29 +08:00
|
|
|
|
public DecryptedMethodInfo lookup(uint bodyRva) {
|
|
|
|
|
DecryptedMethodInfo info;
|
|
|
|
|
infos.TryGetValue(bodyRva, out info);
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte readByte(uint offset) {
|
|
|
|
|
return peImage.offsetReadByte(methodInfosOffset + offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short readInt16(uint offset) {
|
|
|
|
|
return (short)peImage.offsetReadUInt16(methodInfosOffset + offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint readUInt32(uint offset) {
|
|
|
|
|
return peImage.offsetReadUInt32(methodInfosOffset + offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readInt32(uint offset) {
|
|
|
|
|
return (int)readUInt32(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short readEncryptedInt16(uint offset) {
|
|
|
|
|
return (short)(readInt16(offset) ^ xorKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readEncryptedInt32(uint offset) {
|
|
|
|
|
return (int)readEncryptedUInt32(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint readEncryptedUInt32(uint offset) {
|
|
|
|
|
return readUInt32(offset) ^ xorKey;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
interface IDecrypter {
|
|
|
|
|
byte[] decrypt(int type, byte[] encrypted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecrypterV1 : IDecrypter {
|
|
|
|
|
MethodInfos methodInfos;
|
|
|
|
|
|
|
|
|
|
public DecrypterV1(MethodInfos methodInfos) {
|
|
|
|
|
this.methodInfos = methodInfos;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-22 19:38:02 +08:00
|
|
|
|
public byte[] decrypt(int type, byte[] encrypted) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 1: return methodInfos.decrypt1(encrypted);
|
|
|
|
|
case 2: return methodInfos.decrypt4(encrypted);
|
|
|
|
|
case 3: return methodInfos.decrypt2(encrypted);
|
|
|
|
|
case 4: return methodInfos.decrypt3(encrypted);
|
|
|
|
|
case 5: return methodInfos.decrypt5(encrypted);
|
|
|
|
|
case 6: return methodInfos.decrypt6(encrypted);
|
|
|
|
|
case 7: return methodInfos.decrypt7(encrypted);
|
|
|
|
|
default: throw new ApplicationException(string.Format("Invalid encryption type: {0:X2}", type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecrypterV2 : IDecrypter {
|
|
|
|
|
MethodInfos methodInfos;
|
|
|
|
|
|
|
|
|
|
public DecrypterV2(MethodInfos methodInfos) {
|
|
|
|
|
this.methodInfos = methodInfos;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
public byte[] decrypt(int type, byte[] encrypted) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 1: return methodInfos.decrypt3(encrypted);
|
|
|
|
|
case 2: return methodInfos.decrypt2(encrypted);
|
|
|
|
|
case 3: return methodInfos.decrypt1(encrypted);
|
|
|
|
|
case 4: return methodInfos.decrypt4(encrypted);
|
|
|
|
|
case 5: return methodInfos.decrypt5(encrypted);
|
|
|
|
|
case 6: return methodInfos.decrypt6(encrypted);
|
|
|
|
|
case 7: return methodInfos.decrypt7(encrypted);
|
|
|
|
|
default: throw new ApplicationException(string.Format("Invalid encryption type: {0:X2}", type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-22 19:38:02 +08:00
|
|
|
|
class DecrypterV3 : IDecrypter {
|
2012-02-21 16:26:05 +08:00
|
|
|
|
MethodInfos methodInfos;
|
|
|
|
|
|
2012-02-22 19:38:02 +08:00
|
|
|
|
public DecrypterV3(MethodInfos methodInfos) {
|
2012-02-21 16:26:05 +08:00
|
|
|
|
this.methodInfos = methodInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt(int type, byte[] encrypted) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 1: return methodInfos.decrypt1(encrypted);
|
|
|
|
|
case 2: return methodInfos.decrypt2(encrypted);
|
|
|
|
|
case 3: return methodInfos.decrypt3(encrypted);
|
|
|
|
|
case 4: return methodInfos.decrypt4(encrypted);
|
|
|
|
|
case 5: return methodInfos.decrypt5(encrypted);
|
|
|
|
|
case 6: return methodInfos.decrypt6(encrypted);
|
|
|
|
|
case 7: return methodInfos.decrypt7(encrypted);
|
|
|
|
|
default: throw new ApplicationException(string.Format("Invalid encryption type: {0:X2}", type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initializeDecrypter() {
|
|
|
|
|
switch (getVersion()) {
|
|
|
|
|
case EncryptionVersion.V1:
|
|
|
|
|
decrypter = new DecrypterV1(this);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case EncryptionVersion.V2:
|
|
|
|
|
decrypter = new DecrypterV2(this);
|
|
|
|
|
break;
|
|
|
|
|
|
2012-02-22 19:38:02 +08:00
|
|
|
|
case EncryptionVersion.V3:
|
|
|
|
|
decrypter = new DecrypterV3(this);
|
|
|
|
|
break;
|
|
|
|
|
|
2012-02-21 16:26:05 +08:00
|
|
|
|
case EncryptionVersion.Unknown:
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown MC version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 00:20:29 +08:00
|
|
|
|
public void initializeInfos() {
|
2012-02-21 16:26:05 +08:00
|
|
|
|
initializeDecrypter();
|
|
|
|
|
|
2012-02-21 00:20:29 +08:00
|
|
|
|
int numMethods = readInt32(0) ^ readInt32(4);
|
|
|
|
|
if (numMethods < 0)
|
|
|
|
|
throw new ApplicationException("Invalid number of encrypted methods");
|
|
|
|
|
|
|
|
|
|
xorKey = (uint)numMethods;
|
2012-02-22 19:14:15 +08:00
|
|
|
|
uint rvaDispl = !mainType.IsOld && peImage.readUInt32(0x2008) != 0x48 ? 0x1000U : 0;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
int numEncryptedDataInfos = ((int)structSize - 0xC) / ENCRYPTED_DATA_INFO_SIZE;
|
|
|
|
|
var encryptedDataInfos = new byte[numEncryptedDataInfos][];
|
|
|
|
|
|
|
|
|
|
uint offset = 8;
|
|
|
|
|
for (int i = 0; i < numMethods; i++, offset += structSize) {
|
|
|
|
|
uint methodBodyRva = readEncryptedUInt32(offset) - rvaDispl;
|
|
|
|
|
uint totalSize = readEncryptedUInt32(offset + 4);
|
|
|
|
|
uint methodInstructionRva = readEncryptedUInt32(offset + 8) - rvaDispl;
|
|
|
|
|
|
|
|
|
|
var decryptedData = new byte[totalSize];
|
|
|
|
|
|
|
|
|
|
// Read the method body header and method body (instrs + exception handlers).
|
|
|
|
|
// The method body header is always in the first one. The instrs + ex handlers
|
|
|
|
|
// are always in the last 4, and evenly divided (each byte[] is totalLen / 4).
|
|
|
|
|
// The 2nd one is for the exceptions (or padding), but it may be null.
|
|
|
|
|
uint offset2 = offset + 0xC;
|
|
|
|
|
int exOffset = 0;
|
|
|
|
|
for (int j = 0; j < encryptedDataInfos.Length; j++, offset2 += ENCRYPTED_DATA_INFO_SIZE) {
|
|
|
|
|
// readByte(offset2); <-- index
|
|
|
|
|
int encryptionType = readEncryptedInt16(offset2 + 1);
|
|
|
|
|
uint dataOffset = readEncryptedUInt32(offset2 + 3);
|
|
|
|
|
uint encryptedSize = readEncryptedUInt32(offset2 + 7);
|
|
|
|
|
uint realSize = readEncryptedUInt32(offset2 + 11);
|
|
|
|
|
if (j == 1)
|
|
|
|
|
exOffset = readEncryptedInt32(offset2 + 15);
|
|
|
|
|
if (j == 1 && exOffset == 0)
|
|
|
|
|
encryptedDataInfos[j] = null;
|
|
|
|
|
else
|
|
|
|
|
encryptedDataInfos[j] = decrypt(encryptionType, dataOffset, encryptedSize, realSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int copyOffset = 0;
|
|
|
|
|
copyOffset = copyData(decryptedData, encryptedDataInfos[0], copyOffset);
|
|
|
|
|
for (int j = 2; j < encryptedDataInfos.Length; j++)
|
|
|
|
|
copyOffset = copyData(decryptedData, encryptedDataInfos[j], copyOffset);
|
|
|
|
|
copyData(decryptedData, encryptedDataInfos[1], exOffset); // Exceptions or padding
|
|
|
|
|
|
|
|
|
|
var info = new DecryptedMethodInfo(methodBodyRva, decryptedData);
|
|
|
|
|
infos[info.bodyRva] = info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int copyData(byte[] dest, byte[] source, int offset) {
|
|
|
|
|
if (source == null)
|
|
|
|
|
return offset;
|
|
|
|
|
Array.Copy(source, 0, dest, offset, source.Length);
|
|
|
|
|
return offset + source.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] readData(uint offset, int size) {
|
|
|
|
|
return peImage.offsetReadBytes(encryptedDataOffset + offset, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt(int type, uint dataOffset, uint encryptedSize, uint realSize) {
|
|
|
|
|
if (realSize == 0)
|
|
|
|
|
return null;
|
|
|
|
|
if (realSize > encryptedSize)
|
|
|
|
|
throw new ApplicationException("Invalid realSize");
|
|
|
|
|
|
|
|
|
|
var encrypted = readData(dataOffset, (int)encryptedSize);
|
2012-02-21 16:26:05 +08:00
|
|
|
|
var decrypted = decrypter.decrypt(type, encrypted);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
if (realSize > decrypted.Length)
|
|
|
|
|
throw new ApplicationException("Invalid decrypted length");
|
|
|
|
|
Array.Resize(ref decrypted, (int)realSize);
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt1(byte[] encrypted) {
|
|
|
|
|
var decrypted = new byte[encrypted.Length];
|
|
|
|
|
for (int i = 0; i < decrypted.Length; i++)
|
2012-03-27 01:31:45 +08:00
|
|
|
|
decrypted[i] = (byte)(encrypted[i] ^ mcKey.readByte(i % 0x2000));
|
2012-02-21 00:20:29 +08:00
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt2(byte[] encrypted) {
|
|
|
|
|
if ((encrypted.Length & 7) != 0)
|
|
|
|
|
throw new ApplicationException("Invalid encryption #2 length");
|
|
|
|
|
const int offset = 0x00FA;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
uint key4 = mcKey.readUInt32(offset + 4 * 4);
|
|
|
|
|
uint key5 = mcKey.readUInt32(offset + 5 * 4);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
byte[] decrypted = new byte[encrypted.Length & ~7];
|
|
|
|
|
var writer = new BinaryWriter(new MemoryStream(decrypted));
|
|
|
|
|
|
|
|
|
|
int loopCount = encrypted.Length / 8;
|
|
|
|
|
for (int i = 0; i < loopCount; i++) {
|
|
|
|
|
uint val0 = BitConverter.ToUInt32(encrypted, i * 8);
|
|
|
|
|
uint val1 = BitConverter.ToUInt32(encrypted, i * 8 + 4);
|
|
|
|
|
uint x = (val1 >> 26) + (val0 << 6);
|
|
|
|
|
uint y = (val0 >> 26) + (val1 << 6);
|
|
|
|
|
|
|
|
|
|
writer.Write(x ^ key4);
|
|
|
|
|
writer.Write(y ^ key5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static byte[] decrypt3Shifts = new byte[16] { 5, 11, 14, 21, 6, 20, 17, 29, 4, 10, 3, 2, 7, 1, 26, 18 };
|
|
|
|
|
byte[] decrypt3(byte[] encrypted) {
|
|
|
|
|
if ((encrypted.Length & 7) != 0)
|
2012-02-21 18:51:19 +08:00
|
|
|
|
throw new ApplicationException("Invalid encryption #3 length");
|
2012-02-21 00:20:29 +08:00
|
|
|
|
const int offset = 0x015E;
|
2012-03-27 01:31:45 +08:00
|
|
|
|
uint key0 = mcKey.readUInt32(offset + 0 * 4);
|
|
|
|
|
uint key3 = mcKey.readUInt32(offset + 3 * 4);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
byte[] decrypted = new byte[encrypted.Length & ~7];
|
|
|
|
|
var writer = new BinaryWriter(new MemoryStream(decrypted));
|
|
|
|
|
|
|
|
|
|
int loopCount = encrypted.Length / 8;
|
|
|
|
|
for (int i = 0; i < loopCount; i++) {
|
|
|
|
|
uint x = BitConverter.ToUInt32(encrypted, i * 8);
|
|
|
|
|
uint y = BitConverter.ToUInt32(encrypted, i * 8 + 4);
|
|
|
|
|
foreach (var shift in decrypt3Shifts) {
|
|
|
|
|
int shift1 = 32 - shift;
|
|
|
|
|
uint x1 = (y >> shift1) + (x << shift);
|
|
|
|
|
uint y1 = (x >> shift1) + (y << shift);
|
|
|
|
|
x = x1;
|
|
|
|
|
y = y1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.Write(x ^ key0);
|
|
|
|
|
writer.Write(y ^ key3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt4(byte[] encrypted) {
|
|
|
|
|
var decrypted = new byte[encrypted.Length / 3 * 2 + 1];
|
|
|
|
|
|
|
|
|
|
int count = encrypted.Length / 3;
|
|
|
|
|
int i = 0, j = 0, k = 0;
|
|
|
|
|
while (count-- > 0) {
|
2012-03-27 01:31:45 +08:00
|
|
|
|
byte k1 = mcKey.readByte(j + 1);
|
|
|
|
|
byte k2 = mcKey.readByte(j + 2);
|
|
|
|
|
byte k3 = mcKey.readByte(j + 3);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
decrypted[k++] = (byte)(((encrypted[i + 1] ^ k2) >> 4) | ((encrypted[i] ^ k1) & 0xF0));
|
|
|
|
|
decrypted[k++] = (byte)(((encrypted[i + 1] ^ k2) << 4) + ((encrypted[i + 2] ^ k3) & 0x0F));
|
|
|
|
|
i += 3;
|
|
|
|
|
j = (j + 4) % 0x2000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((encrypted.Length % 3) != 0)
|
2012-03-27 01:31:45 +08:00
|
|
|
|
decrypted[k] = (byte)(encrypted[i] ^ mcKey.readByte(j));
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt5(byte[] encrypted) {
|
|
|
|
|
throw new NotImplementedException("Encryption type #5 not implemented yet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt6(byte[] encrypted) {
|
|
|
|
|
throw new NotImplementedException("Encryption type #6 not implemented yet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt7(byte[] encrypted) {
|
|
|
|
|
throw new NotImplementedException("Encryption type #7 not implemented yet");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileDecrypter(MainType mainType) {
|
|
|
|
|
this.mainType = mainType;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 05:57:55 +08:00
|
|
|
|
public bool decrypt(byte[] fileData, ref DumpedMethods dumpedMethods) {
|
2012-03-27 01:31:45 +08:00
|
|
|
|
peImage = new PeImage(fileData);
|
|
|
|
|
peHeader = new PeHeader(mainType, peImage);
|
|
|
|
|
mcKey = new McKey(peImage, peHeader);
|
|
|
|
|
this.fileData = fileData;
|
2012-02-21 18:51:19 +08:00
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
dumpedMethods = decryptMethods();
|
2012-02-21 18:51:19 +08:00
|
|
|
|
if (dumpedMethods == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
decryptResources();
|
2012-03-27 02:12:56 +08:00
|
|
|
|
decryptStrings();
|
2012-02-21 18:51:19 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
DumpedMethods decryptMethods() {
|
2012-02-27 05:57:55 +08:00
|
|
|
|
var dumpedMethods = new DumpedMethods();
|
2012-02-21 18:51:19 +08:00
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
var methodInfos = new MethodInfos(mainType, peImage, peHeader, mcKey);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
methodInfos.initializeInfos();
|
|
|
|
|
|
|
|
|
|
var metadataTables = peImage.Cor20Header.createMetadataTables();
|
|
|
|
|
var methodDef = metadataTables.getMetadataType(MetadataIndex.iMethodDef);
|
|
|
|
|
uint methodDefOffset = methodDef.fileOffset;
|
|
|
|
|
for (int i = 0; i < methodDef.rows; i++, methodDefOffset += methodDef.totalSize) {
|
|
|
|
|
uint bodyRva = peImage.offsetReadUInt32(methodDefOffset);
|
|
|
|
|
if (bodyRva == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var info = methodInfos.lookup(bodyRva);
|
|
|
|
|
if (info == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
uint bodyOffset = peImage.rvaToOffset(bodyRva);
|
|
|
|
|
ushort magic = peImage.offsetReadUInt16(bodyOffset);
|
|
|
|
|
if (magic != 0xFFF3)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var dm = new DumpedMethod();
|
|
|
|
|
dm.token = (uint)(0x06000001 + i);
|
|
|
|
|
dm.mdImplFlags = peImage.offsetReadUInt16(methodDefOffset + (uint)methodDef.fields[1].offset);
|
|
|
|
|
dm.mdFlags = peImage.offsetReadUInt16(methodDefOffset + (uint)methodDef.fields[2].offset);
|
|
|
|
|
dm.mdName = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[3].offset, methodDef.fields[3].size);
|
|
|
|
|
dm.mdSignature = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[4].offset, methodDef.fields[4].size);
|
|
|
|
|
dm.mdParamList = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[5].offset, methodDef.fields[5].size);
|
|
|
|
|
|
|
|
|
|
var reader = new BinaryReader(new MemoryStream(info.body));
|
|
|
|
|
byte b = reader.ReadByte();
|
|
|
|
|
if ((b & 3) == 2) {
|
|
|
|
|
dm.mhFlags = 2;
|
|
|
|
|
dm.mhMaxStack = 8;
|
|
|
|
|
dm.mhCodeSize = (uint)(b >> 2);
|
|
|
|
|
dm.mhLocalVarSigTok = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
reader.BaseStream.Position--;
|
|
|
|
|
dm.mhFlags = reader.ReadUInt16();
|
|
|
|
|
dm.mhMaxStack = reader.ReadUInt16();
|
|
|
|
|
dm.mhCodeSize = reader.ReadUInt32();
|
|
|
|
|
dm.mhLocalVarSigTok = reader.ReadUInt32();
|
|
|
|
|
uint codeOffset = (uint)(dm.mhFlags >> 12) * 4;
|
|
|
|
|
reader.BaseStream.Position += codeOffset - 12;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dm.code = reader.ReadBytes((int)dm.mhCodeSize);
|
|
|
|
|
if ((dm.mhFlags & 8) != 0) {
|
|
|
|
|
reader.BaseStream.Position = (reader.BaseStream.Position + 3) & ~3;
|
|
|
|
|
dm.extraSections = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 05:57:55 +08:00
|
|
|
|
dumpedMethods.add(dm);
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 18:51:19 +08:00
|
|
|
|
return dumpedMethods;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 01:31:45 +08:00
|
|
|
|
void decryptResources() {
|
|
|
|
|
uint resourceRva = peHeader.getRva1(0x0E10, mcKey.readUInt32(0x00A0));
|
|
|
|
|
uint resourceSize = peHeader.readUInt32(0x0E14) ^ mcKey.readUInt32(0x00AA);
|
2012-02-21 18:51:19 +08:00
|
|
|
|
if (resourceRva == 0 || resourceSize == 0)
|
|
|
|
|
return;
|
|
|
|
|
if (resourceRva != peImage.Cor20Header.resources.virtualAddress ||
|
|
|
|
|
resourceSize != peImage.Cor20Header.resources.size) {
|
|
|
|
|
Log.w("Invalid resource RVA and size found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.v("Decrypting resources @ RVA {0:X8}, {1} bytes", resourceRva, resourceSize);
|
|
|
|
|
|
|
|
|
|
int resourceOffset = (int)peImage.rvaToOffset(resourceRva);
|
|
|
|
|
for (int i = 0; i < resourceSize; i++)
|
2012-03-27 01:31:45 +08:00
|
|
|
|
fileData[resourceOffset + i] ^= mcKey[i % 0x2000];
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
2012-03-27 02:12:56 +08:00
|
|
|
|
|
|
|
|
|
void decryptStrings() {
|
|
|
|
|
uint usHeapRva = peHeader.getRva1(0x0E00, mcKey.readUInt32(0x0078));
|
|
|
|
|
uint usHeapSize = peHeader.readUInt32(0x0E04) ^ mcKey.readUInt32(0x0082);
|
|
|
|
|
if (usHeapRva == 0 || usHeapSize == 0)
|
|
|
|
|
return;
|
|
|
|
|
var usHeap = peImage.Cor20Header.metadata.getStream("#US");
|
|
|
|
|
if (usHeap == null ||
|
|
|
|
|
peImage.rvaToOffset(usHeapRva) != usHeap.fileOffset ||
|
2012-03-27 21:23:27 +08:00
|
|
|
|
usHeapSize != usHeap.Length) {
|
2012-03-27 02:12:56 +08:00
|
|
|
|
Log.w("Invalid #US heap RVA and size found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.v("Decrypting strings @ RVA {0:X8}, {1} bytes", usHeapRva, usHeapSize);
|
|
|
|
|
Log.indent();
|
|
|
|
|
|
|
|
|
|
int mcKeyOffset = 0;
|
|
|
|
|
int usHeapOffset = (int)peImage.rvaToOffset(usHeapRva);
|
|
|
|
|
int usHeapEnd = usHeapOffset + (int)usHeapSize;
|
|
|
|
|
usHeapOffset++;
|
|
|
|
|
while (usHeapOffset < usHeapEnd) {
|
|
|
|
|
if (fileData[usHeapOffset] == 0 || fileData[usHeapOffset] == 1) {
|
|
|
|
|
usHeapOffset++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int usHeapOffsetOrig = usHeapOffset;
|
2012-03-27 08:24:53 +08:00
|
|
|
|
int stringDataLength = DeobUtils.readVariableLengthInt32(fileData, ref usHeapOffset);
|
2012-03-27 02:12:56 +08:00
|
|
|
|
int usHeapOffsetString = usHeapOffset;
|
2012-03-27 08:24:53 +08:00
|
|
|
|
int encryptedLength = stringDataLength - (usHeapOffset - usHeapOffsetOrig == 1 ? 1 : 2);
|
|
|
|
|
for (int i = 0; i < encryptedLength; i++) {
|
2012-03-27 02:12:56 +08:00
|
|
|
|
byte k = mcKey.readByte(mcKeyOffset++ % 0x2000);
|
|
|
|
|
fileData[usHeapOffset] = rolb((byte)(fileData[usHeapOffset] ^ k), 3);
|
|
|
|
|
usHeapOffset++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2012-03-27 08:24:53 +08:00
|
|
|
|
Log.v("Decrypted string: {0}", Utils.toCsharpString(Encoding.Unicode.GetString(fileData, usHeapOffsetString, stringDataLength - 1)));
|
2012-03-27 02:12:56 +08:00
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
Log.v("Could not decrypt string at offset {0:X8}", usHeapOffsetOrig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usHeapOffset++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.deIndent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte rolb(byte b, int n) {
|
|
|
|
|
return (byte)((b << n) | (b >> (8 - n)));
|
|
|
|
|
}
|
2012-02-21 00:20:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|