本帖最后由 kanon 于 2015-4-23 11:55 编辑
//ApkCheck.h
[C++] 纯文本查看 复制代码
/*******************************************************************************
该方法是校验class.dex和AndroidManifest.xml
只需在你要进行校验的地方进行判断即可,如下:
if(!ApkCheck::AndroidFileCheck())
{
//返回为flase时,证明校验失败,说明有修改行为,给个提示,然后退出游戏
CCLog("apk check false!");
CCDirector::sharedDirector()->end();
}
class.dex和AndroidManifest.xml的MD5要自己从APK解压后,自己计算出来(可使用MD5工具),并填入ApkCheck.cpp中对应校验的地方即可
*******************************************************************************/
#ifndef UTIL_APKCHECK_H_
#define UTIL_APKCHECK_H_
#include "cocos2d.h"
class ApkCheck
{
public:
static bool AndroidFileCheck();
};
#endif // UTIL_APKCHECK_H_
//ApkCheck.cpp
[C++] 纯文本查看 复制代码 #include "ApkCheck.h"
#include "YMD5.h"
#include "cocos2d.h"
#include "platform/CCFileUtils.h"
#include "config.h"
USING_NS_CC;
/**包含所需的文件,只有在Android平台下需要,所以用条件编译语句括起来。*/
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "jni.h"
#include "platform/android/jni/JniHelper.h"
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#endif
//需要使用者按自己的情况修改下面的类名之类的,不要全部直接复制。
bool ApkCheck::AndroidFileCheck(){
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
unsigned long file_size;
const char *apk_path = getApkPath();
//校验dex文件
unsigned char *dexbuffer = CCFileUtils::sharedFileUtils()->getFileDataFromZip(apk_path,
"classes.dex",
&file_size);
YCMD5 dexcmd5;
const char *dexmd5_result=dexcmd5.GetFileMD5Values_buffer(dexbuffer,file_size);
CCLOG("vvvvvvvvvvvvvvdex %s",dexmd5_result);
int cmpdex_result=strcmp(dexmd5_result, config::m_sClassDexMD5);//我这里是放在config::m_sClassDexMD5,用的时候改为你觉得方便的地方。一个是classes.dex的md5值,一个是AndroidMainifest.xml文件的md5值。
delete []dexbuffer;
//校验manifest文件
unsigned char *manifestbuffer =CCFileUtils::sharedFileUtils()->getFileDataFromZip(apk_path,
"AndroidManifest.xml",
&file_size);
YCMD5 manifestcmd5;
const char *manifestmd5_result=manifestcmd5.GetFileMD5Values_buffer(manifestbuffer,file_size);
CCLOG("vvvvvvvvvvvvvvmani %s",manifestmd5_result);
int cmpmanifest_result=strcmp(manifestmd5_result, config::m_sManifestMD5);//AndroidMainifest.xml文件的md5值。
delete []manifestbuffer;
CCLog("dex =%s ,saved dex =%s",dexmd5_result,config::m_sClassDexMD5);
CCLog("manifest.xml =%s ,saved anifest.xml =%s",manifestmd5_result,config::m_sManifestMD5);
if(cmpdex_result==0&&cmpmanifest_result==0)
return true;
else
return false;
#else
CCLog("if not android ,fileCheck return true");
return true;
#endif
}
//config.h
[C++] 纯文本查看 复制代码 // 游戏全局配置文件
#ifndef COMMON_CONFIG_H_
#define COMMON_CONFIG_H_
namespace config {
const char *const m_sClassDexMD5 = "647096e34265eb887aa8f6f87546bea4";
const char *const m_sManifestMD5 = "03792a306889de8a0816865ad075a653";
}
#endif // COMMON_CONFIG_H_
//YMD5.h
[C++] 纯文本查看 复制代码 #ifndef MD5_H
#define MD5_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef unsigned char TYBYTE;
typedef unsigned int TYUINT;
class YCMD5
{
public:
YCMD5(void);
char* GetFileMD5Values(const char* pFileName);
char* GetFileMD5Values_buffer(const unsigned char* pFileBuffer, unsigned long nBufferLen);
char* GetStringMD5Values(const char* pStr);
private:
void Init(void);
void UpDate(const TYBYTE* input, TYUINT length);
void Transform(const TYBYTE* block);
void DeCode(const TYBYTE* input, TYUINT* output, TYUINT length);
void EnCode(const TYUINT* input, TYBYTE* output, TYUINT length);
void SetMD5Code(const TYBYTE* input, TYUINT length);
const TYBYTE* Digest();
private:
TYUINT m_state[4]; // state (ABCD)
TYUINT m_count[2]; // number of bits, modulo 2^64 (low-order word first)
TYBYTE m_buffer[64]; // input buffer
TYBYTE m_digest[16]; // message digest
char m_MD5Val[33]; // 保存 MD5 值
};
#endif // MD5_H YMD5.cpp//
[C++] 纯文本查看 复制代码 #include "YMD5.h"
#include <fstream>
using std::ifstream;
//////////////////////////////////////////////////////////////////////////
// 定义一个 4x4 矩阵
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
//////////////////////////////////////////////////////////////////////////
// F, G, H and I are basic CMD5 functions
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
//////////////////////////////////////////////////////////////////////////
// ROTATE_LEFT rotates x left n bits
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
//////////////////////////////////////////////////////////////////////////
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4
// Rotation is separate from addition to prevent recomputation
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
//////////////////////////////////////////////////////////////////////////
//
const int BUFFER_SIZE = 1024;
const TYBYTE PADDING[64] = { 0x80 };
const char HEX[16] =
{
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
//////////////////////////////////////////////////////////////////////////
// 默认构造函数
YCMD5::YCMD5(void)
{
}
//////////////////////////////////////////////////////////////////////////
// 重置
void YCMD5::Init()
{
m_count[0] = 0;
m_count[1] = 0;
m_state[0] = 0x67452301;
m_state[1] = 0xefcdab89;
m_state[2] = 0x98badcfe;
m_state[3] = 0x10325476;
memset(m_MD5Val, 0, 33);
}
//////////////////////////////////////////////////////////////////////////
//
void YCMD5::UpDate(const TYBYTE* input, TYUINT length)
{
TYUINT i, index, partLen;
/* Compute number of bytes mod 64 */
index = (TYUINT)((m_count[0] >> 3) & 0x3f);
/* update number of bits */
if ((m_count[0] += ((TYUINT)length << 3)) < ((TYUINT)length << 3))
{
++m_count[1];
}
m_count[1] += ((TYUINT)length >> 29);
partLen = 64 - index;
/* transform as many times as possible. */
if (length >= partLen)
{
memcpy(&m_buffer[index], input, partLen);
Transform(m_buffer);
for (i = partLen; i + 63 < length; i += 64)
{
Transform(&input[i]);
}
index = 0;
}
else
{
i = 0;
}
/* Buffer remaining input */
memcpy(&m_buffer[index], &input[i], length - i);
}
//////////////////////////////////////////////////////////////////////////
//
void YCMD5::Transform(const TYBYTE block[64])
{
TYUINT a = m_state[0];
TYUINT b = m_state[1];
TYUINT c = m_state[2];
TYUINT d = m_state[3];
TYUINT x[16] = {0};
DeCode(block, x, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
m_state[0] += a;
m_state[1] += b;
m_state[2] += c;
m_state[3] += d;
}
//////////////////////////////////////////////////////////////////////////
//
void YCMD5::DeCode(const TYBYTE* input, TYUINT* output, TYUINT length)
{
for (TYUINT i = 0, j = 0; j < length; ++i, j += 4)
{
output[i] = ((TYUINT)input[j])
| (((TYUINT)input[j + 1]) << 8)
| (((TYUINT)input[j + 2]) << 16)
| (((TYUINT)input[j + 3]) << 24);
}
}
//////////////////////////////////////////////////////////////////////////
//
void YCMD5::EnCode(const TYUINT* input, TYBYTE* output, TYUINT length)
{
for (size_t i = 0, j = 0; j < length; ++i, j += 4)
{
output[j] = (TYBYTE)(input[i] & 0xff);
output[j + 1] = (TYBYTE)((input[i] >> 8) & 0xff);
output[j + 2] = (TYBYTE)((input[i] >> 16) & 0xff);
output[j + 3] = (TYBYTE)((input[i] >> 24) & 0xff);
}
}
//////////////////////////////////////////////////////////////////////////
// 计算 MD5 值
void YCMD5::SetMD5Code(const TYBYTE* input, TYUINT length)
{
int t, a, b;
for (size_t i = 0; i < length; ++i)
{
t = input[i];
a = t / 16;
b = t % 16;
m_MD5Val[2 * i] = HEX[a];
m_MD5Val[2 * i + 1] = HEX[b];
}
}
//////////////////////////////////////////////////////////////////////////
// 获取文件的 MD5 值
char* YCMD5::GetFileMD5Values(const char* pFileName)
{
Init();
ifstream file(pFileName);
if (!file)
{
/* 加载文件失败,返回空串 */
return "";
}
std::streamsize length;
char buffer[BUFFER_SIZE];
while (!file.eof())
{
memset(buffer, 0, BUFFER_SIZE);
file.read(buffer, BUFFER_SIZE);
length = file.gcount();
if (length > 0)
{
UpDate((const TYBYTE*)buffer, length);
}
}
file.close();
SetMD5Code(Digest(), 16);
return m_MD5Val;
}
//////////////////////////////////////////////////////////////////////////
// 获取文件的 MD5 值
char* YCMD5::GetFileMD5Values_buffer(const unsigned char* pFileBuffer, unsigned long nBufferLen)
{
Init();
if (pFileBuffer == NULL || nBufferLen <= 0)
{
return "";
}
char buffer[BUFFER_SIZE];
int length = 0;
while (nBufferLen > 0)
{
if (nBufferLen > BUFFER_SIZE)
{
length = BUFFER_SIZE;
}
else
{
length = nBufferLen;
}
memcpy(buffer, pFileBuffer, length);
pFileBuffer += length;
nBufferLen -= length;
if (length > 0)
{
UpDate((const TYBYTE*)buffer, length);
}
}
SetMD5Code(Digest(), 16);
return m_MD5Val;
}
//////////////////////////////////////////////////////////////////////////
// 获取字符串的 MD5 值
char* YCMD5::GetStringMD5Values(const char* pStr)
{
Init();
UpDate((const TYBYTE*)pStr, (TYUINT)strlen(pStr));
SetMD5Code(Digest(), 16);
return m_MD5Val;
}
//////////////////////////////////////////////////////////////////////////
//
const TYBYTE* YCMD5::Digest()
{
TYBYTE bits[8];
TYUINT oldState[4];
TYUINT oldCount[2];
TYUINT index;
TYUINT padLen;
/* Save current state and count. */
memcpy(oldState, m_state, 16);
memcpy(oldCount, m_count, 8);
/* Save number of bits */
EnCode(m_count, bits, 8);
/* Pad out to 56 mod 64. */
index = (TYUINT)((m_count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
UpDate(PADDING, padLen);
/* Append length (before padding) */
UpDate(bits, 8);
/* Store state in digest */
EnCode(m_state, m_digest, 16);
/* Restore current state and count. */
memcpy(m_state, oldState, 16);
memcpy(m_count, oldCount, 8);
return m_digest;
}
|