一个用于代码混淆和字符串加密的Mac小Demo
libclang
解析扫描源代码的语法树,搜索出所有的类名、方法名、字符串MJCodeObfuscation/Classes/Tool/MJClangTool.m
,比较简单,不复杂将需要混淆的类名、方法名生成随机字符串的宏定义
@interface MJPerson : NSObject
- (void)mj_run;
- (void)mj_setupName:(NSString *)name mj_no:(int)no mj_age:(int)age;
@end
@implementation MJPerson
- (void)mj_run {
NSLog(@"%s", __func__);
}
- (void)mj_setupName:(NSString *)name mj_no:(int)no mj_age:(int)age {
NSLog(@"%s - %@ %d %d", __func__, name, no, age);
}
@end
MJ
、mj_
)搜索出需要混淆的类名、方法名#define mj_run OmWJoTZfCqoPshvr
#define MJPerson egnjoOFDrFiQVRgr
#define mj_setupName HrZLzcgSoPhwMBwW
#define mj_age reXYcdSKKEUSMalJ
#define mj_no mHEQViTuoOvRtMuB
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "MJCodeObfuscation.h"
#endif /* PrefixHeader_pch */
MJPerson *person = [[MJPerson alloc] init];
[person mj_run];
[person mj_setupName:@"jack" mj_no:20 mj_age:21];
// 打印结果
-[egnjoOFDrFiQVRgr OmWJoTZfCqoPshvr]
-[egnjoOFDrFiQVRgr HrZLzcgSoPhwMBwW:mHEQViTuoOvRtMuB:reXYcdSKKEUSMalJ:] - jack 20 21
仅仅是将字符串进行了一个简单的异或处理(开发者可以自行制定加密算法)
NSString *str1 = @"小码哥mj123go";
const char *str2 = "小码哥mj123go";
NSLog(@"%@ %s", str1, str2);
/* 小码哥mj123go */
extern const MJEncryptStringData * const _761622619;
/* 小码哥mj123go */
const MJEncryptStringData * const _761622619 = &(MJEncryptStringData){
.factor = (char)-100,
.value = (char []){121,44,19,123,60,29,121,15,57,-15,-10,-83,-82,-81,-5,-13,0},
.length = 16
};
MJEncryptStringData
结构,所以需要将MJEncryptString
目录的内容加入到项目中#import "MJEncryptString.h"
NSString *str1 = mj_OCString(_761622619);
const char *str2 = mj_CString(_761622619);
NSLog(@"%@ %s", str1, str2);
// 打印结果如下
小码哥mj123go 小码哥mj123go
MJEncryptString
目录
MJEncryptStringData.h
(便于整个项目中共享使用加密的字符串)#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "MJEncryptStringData.h"
#endif /* PrefixHeader_pch */
MJEncryptStringData.h
文件内容如下所示
"%@ %s"
、"小码哥mj123go"
字符串都进行了加密#ifndef MJEncryptStringData_h
#define MJEncryptStringData_h
#include "MJEncryptString.h"
/* %@ %s */
extern const MJEncryptStringData * const _1302706645;
/* 小码哥mj123go */
extern const MJEncryptStringData * const _761622619;
#endif
NSString *str1 = mj_OCString(_761622619);
const char *str2 = mj_CString(_761622619);
NSLog(@"%@ %s", str1, str2);
// 打印结果如下
小码哥mj123go 小码哥mj123go