Home

Awesome

MJCodeObfuscation

一个用于代码混淆和字符串加密的Mac小Demo

代码混淆

将需要混淆的类名、方法名生成随机字符串的宏定义

@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

#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

字符串加密(方式1)

仅仅是将字符串进行了一个简单的异或处理(开发者可以自行制定加密算法)

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
};

#import "MJEncryptString.h"

NSString *str1 = mj_OCString(_761622619);
const char *str2 = mj_CString(_761622619);
NSLog(@"%@ %s", str1, str2);

// 打印结果如下
小码哥mj123go 小码哥mj123go

字符串加密(方式2)

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "MJEncryptStringData.h"

#endif /* PrefixHeader_pch */
#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