Skip to content

Commit b0a6e2f

Browse files
author
shangkun
committed
Update xor encrypt function
1 parent dc731d4 commit b0a6e2f

File tree

8 files changed

+275
-32
lines changed

8 files changed

+275
-32
lines changed

README.md

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,88 @@
11
# SKGenerateModelTool
22

3-
- 支持通过URL链接或json字符串一键生成model文件
4-
- 支持自定义model父类、自定义model前缀、自定义文件名、自定义作者名
5-
- 支持OC / Swift
6-
- 支持自定义输出文件夹路径
7-
- 兼容YYModel / MJExtension / HandyJSON解析
8-
- 兼容服务端返回“id”字段
3+
4+
- #####支持通过URL链接或json字符串一键生成model文件
5+
- #####支持字符串加密(可设置不同的密钥,开发者可自行修改加密算法)
6+
- #####支持自定义model父类、自定义model前缀、自定义文件名、自定义作者名
7+
- #####支持OC / Swift
8+
- #####支持自定义输出文件夹路径
9+
- #####兼容YYModel / MJExtension / HandyJSON解析
10+
- #####兼容服务端返回“id”字段
11+
- #####支持类驼峰命名
912

1013
- Supports one-click generation of model files through URL links or json strings
14+
- Support string encryption (different keys can be set, developers can modify the encryption algorithm)
1115
- Support custom model parent class, custom model prefix, custom file name, custom author name
1216
- Support OC / Swift
1317
- Support custom output folder path
14-
- Compatible with YYModel / MJExtension / HandyJSON analysis
18+
- Compatible with YYModel / MJExtension analysis
1519
- Compatible server returns "id" field
20+
- Supports hump naming
21+
22+
![SKGenerateModelTool](https://upload-images.jianshu.io/upload_images/1129777-9c130b4ce345ddf9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
23+
24+
###### 生成Model
25+
![生成Model](https://upload-images.jianshu.io/upload_images/1129777-51a3b41012e11c96.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
26+
27+
###### 字符串加密示例
28+
![字符串加密示例](https://upload-images.jianshu.io/upload_images/1129777-1fc076f8a5f16768.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
29+
30+
加密后的内容添加到项目中(声明和定义可以分别放.h和.m),因为代码依赖SKEncryptString结构体,所以需要导入头文件**SKEncryptHeader.h**引用。
31+
32+
> 只需把此头文件(SKEncryptHeader.h)加入到项目,并在pch文件中导入该头文件即可使用;
33+
Just add this header file (SKEncryptHeader.h) to the project and import the header file in the pch file to use;
34+
35+
![SKEncryptHeader.h](https://upload-images.jianshu.io/upload_images/1129777-aa68b75c0934fc6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
36+
37+
加密后的数据申明:
38+
39+
```
40+
/** 需要加密的重要字符,比如AppKey、AppSecret、AppID、数据库密码等 */
41+
extern const SKEncryptString * const _3596508958;
42+
43+
/** Important characters that need to be encrypted, such as appkey, appsecret, appid, database password, etc */
44+
extern const SKEncryptString * const _4038772756;
45+
46+
/** appkeyhdfoashiodfowehfowefjqwehgpqegpifhwappkey */
47+
extern const SKEncryptString * const _3908173925;
48+
```
49+
50+
定义:
51+
```
52+
/** 需要加密的重要字符,比如AppKey、AppSecret、AppID、数据库密码等 */
53+
const SKEncryptString * const _3596508958 = &(SKEncryptString){
54+
.factor = (char)9,
55+
.value = (char[]){-125,-10,-22,-126,-52,-21,-113,-32,-54,-113,-59,-20,-115,-16,-18,-125,-19,-25,-126,-52,-21,-113,-57,-3,-115,-58,-52,-123,-42,-26,-116,-59,-2,-113,-52,-24,43,26,26,33,15,19,-119,-22,-21,43,26,26,57,15,9,24,15,30,-119,-22,-21,43,26,26,35,46,-119,-22,-21,-116,-1,-38,-116,-25,-60,-113,-48,-7,-113,-59,-20,-115,-54,-21,-115,-57,-29,0},
56+
.length = 83,
57+
};
58+
59+
/** Important characters that need to be encrypted, such as appkey, appsecret, appid, database password, etc */
60+
const SKEncryptString * const _4038772756 = &(SKEncryptString){
61+
.factor = (char)100,
62+
.value = (char[]){57,31,4,0,20,29,1,2,6,73,19,26,21,29,7,10,20,9,0,26,80,6,28,14,18,73,14,9,23,13,80,6,27,79,4,12,64,9,28,10,2,11,4,27,3,13,76,76,1,28,19,26,84,14,21,73,1,28,2,2,21,11,88,79,7,25,16,31,23,10,2,23,0,67,70,8,16,28,27,13,92,82,16,14,18,8,2,13,1,12,80,2,21,28,21,30,15,30,22,69,80,23,0,12,0},
63+
.length = 104,
64+
.key = (char[]){119,117,115,104,97,110,103,107,117,110,0},
65+
.kl = 10
66+
};
67+
68+
/** appkeyhdfoashiodfowehfowefjqwehgpqegpifhwappkey */
69+
const SKEncryptString * const _3908173925 = &(SKEncryptString){
70+
.factor = (char)50,
71+
.value = (char[]){4,20,23,14,1,30,13,0,1,10,5,20,13,13,8,1,2,8,18,1,15,3,11,16,0,2,13,20,19,2,13,3,23,20,1,0,21,13,1,13,19,6,21,20,12,0,29,0},
72+
.length = 47,
73+
.key = (char[]){98,99,96,0},
74+
.kl = 3
75+
};
76+
```
1677

17-
![photo1.png](https://upload-images.jianshu.io/upload_images/1129777-cf955d251053c767.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
78+
使用时
79+
```
80+
if let string = sk_OCString(_3596508958) {
81+
print("示例:解密后的数据为:\(string)")
82+
}
83+
if let string = sk_OCString(_4038772756) {
84+
print("The decrypted data is:\(string)")
85+
}
86+
```
1887

88+
Tip:本工具仅用到简单的XOR加密算法,开发者可自行下载项目进行加密算法修改,另外也可直接下载项目里的dmg文件进行安装使用。

SKGenerateModelTool.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
BC6E5D5224C0770800809D98 /* EncryptExampleModel.m in Sources */ = {isa = PBXBuildFile; fileRef = BC6E5D5124C0770800809D98 /* EncryptExampleModel.m */; };
1011
BC88CEA12467A8A600A4828B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC88CEA02467A8A600A4828B /* AppDelegate.swift */; };
1112
BC88CEA32467A8A600A4828B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC88CEA22467A8A600A4828B /* ViewController.swift */; };
1213
BC88CEA52467A8AA00A4828B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BC88CEA42467A8AA00A4828B /* Assets.xcassets */; };
@@ -17,6 +18,9 @@
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXFileReference section */
21+
BC6E5BD824C071EE00809D98 /* SKEncryptHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKEncryptHeader.h; sourceTree = "<group>"; };
22+
BC6E5D5024C0770800809D98 /* EncryptExampleModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EncryptExampleModel.h; sourceTree = "<group>"; };
23+
BC6E5D5124C0770800809D98 /* EncryptExampleModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EncryptExampleModel.m; sourceTree = "<group>"; };
2024
BC88CE9D2467A8A600A4828B /* SKGenerateModelTool.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SKGenerateModelTool.app; sourceTree = BUILT_PRODUCTS_DIR; };
2125
BC88CEA02467A8A600A4828B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2226
BC88CEA22467A8A600A4828B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -76,8 +80,11 @@
7680
BCB184B224B808AF00C2A5D3 /* SKEncryptString */ = {
7781
isa = PBXGroup;
7882
children = (
83+
BC6E5BD824C071EE00809D98 /* SKEncryptHeader.h */,
7984
BCB184B324B8090500C2A5D3 /* SKEncryptTool.swift */,
8085
BCB184B524B85CC700C2A5D3 /* SKGenerateModelTool-Bridging-Header.h */,
86+
BC6E5D5024C0770800809D98 /* EncryptExampleModel.h */,
87+
BC6E5D5124C0770800809D98 /* EncryptExampleModel.m */,
8188
);
8289
path = SKEncryptString;
8390
sourceTree = "<group>";
@@ -153,6 +160,7 @@
153160
isa = PBXSourcesBuildPhase;
154161
buildActionMask = 2147483647;
155162
files = (
163+
BC6E5D5224C0770800809D98 /* EncryptExampleModel.m in Sources */,
156164
BCB6929F24A2EC8F004AC91A /* EncryptionController.swift in Sources */,
157165
BC88D01E2467C2AA00A4828B /* SKCodeBuilder.swift in Sources */,
158166
BC88CEA32467A8A600A4828B /* ViewController.swift in Sources */,

SKGenerateModelTool/EncryptionController.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,20 @@ class EncryptionController: NSViewController {
1616
@IBOutlet var inputTextView: NSTextView!
1717
@IBOutlet var hTextView: NSTextView!
1818
@IBOutlet var mTextView: NSTextView!
19-
2019
@IBOutlet weak var useKeyBtn: NSButton!
2120
@IBOutlet weak var keyTF: NSTextField!
2221

2322
lazy var inputTextColor = NSColor.blue
2423
lazy var codeTextColor = NSColor(red: 215/255.0, green: 0/255.0 , blue: 143/255.0, alpha: 1.0)
2524

26-
2725
@IBAction func encryptString(_ sender: NSButton) {
2826
if let inputString = inputTextView.textStorage?.string {
29-
3027
let state = useKeyBtn.state
3128
UserDefaults.standard.set(state == .on , forKey: UseKeyBtnSelectStateCacheKey)
3229
let key = state == .on ? keyTF.stringValue : nil
3330
UserDefaults.standard.setValue(key, forKey: CipherContentCacheKey)
34-
3531
let tempStr = inputString.replacingOccurrences(of: " ", with: "")
3632
if tempStr.isBlank { return }
37-
3833
configJsonTextView(text: inputString, textView: inputTextView, color: inputTextColor)
3934
SKEncryptTool.encryptString(str: inputString, key: key) {[weak self] (hStr, mStr) in
4035
let hstring = hStr.substring(to: hStr.length)
@@ -46,7 +41,6 @@ class EncryptionController: NSViewController {
4641
}
4742

4843
@IBAction func copyCodeString(_ sender: NSButton) {
49-
5044
let tag = sender.tag
5145
var string = ""
5246
switch tag {
@@ -79,10 +73,17 @@ class EncryptionController: NSViewController {
7973
super.viewDidLoad()
8074

8175
useKeyBtn.state = UserDefaults.standard.bool(forKey: UseKeyBtnSelectStateCacheKey) ? .on : .off
82-
8376
if let key = UserDefaults.standard.string(forKey: CipherContentCacheKey) {
8477
keyTF.stringValue = key
8578
}
79+
80+
/////////////// 使用范例 (Usage Example) /////////////////
81+
82+
// if let string = sk_OCString(_3596508958) {
83+
// print("示例:解密后的数据为:\(string)")
84+
// }
85+
// if let string = sk_OCString(_4038772756) {
86+
// print("The decrypted data is:\(string)")
87+
// }
8688
}
87-
8889
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// EncryptExampleModel.h
3+
// SKGenerateModelTool
4+
//
5+
// Created by shangkun on 2020/7/16.
6+
// Copyright © 2020 wushangkun. All rights reserved.
7+
//
8+
9+
#import "SKEncryptHeader.h"
10+
11+
/////////////////////////////////////////////////////////////////////////////////////
12+
13+
///////////////////////// 以下为示例 ///////////////////////////////
14+
15+
/////////////////////////////////////////////////////////////////////////////////////
16+
17+
/** 需要加密的重要字符,比如AppKey、AppSecret、AppID、数据库密码等 */
18+
extern const SKEncryptString * const _3596508958;
19+
20+
/** Important characters that need to be encrypted, such as appkey, appsecret, appid, database password, etc */
21+
extern const SKEncryptString * const _4038772756;
22+
23+
/** appkeyhdfoashiodfowehfowefjqwehgpqegpifhwappkey */
24+
extern const SKEncryptString * const _3908173925;
25+
26+
/** Swift版:https://github.com/Xcoder1011/SKGenerateModelTool */
27+
extern const SKEncryptString * const _1242105574;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// EncryptExampleModel.m
3+
// SKGenerateModelTool
4+
//
5+
// Created by shangkun on 2020/7/16.
6+
// Copyright © 2020 wushangkun. All rights reserved.
7+
//
8+
9+
#import "EncryptExampleModel.h"
10+
11+
/** 需要加密的重要字符,比如AppKey、AppSecret、AppID、数据库密码等 */
12+
const SKEncryptString * const _3596508958 = &(SKEncryptString){
13+
.factor = (char)9,
14+
.value = (char[]){-125,-10,-22,-126,-52,-21,-113,-32,-54,-113,-59,-20,-115,-16,-18,-125,-19,-25,-126,-52,-21,-113,-57,-3,-115,-58,-52,-123,-42,-26,-116,-59,-2,-113,-52,-24,43,26,26,33,15,19,-119,-22,-21,43,26,26,57,15,9,24,15,30,-119,-22,-21,43,26,26,35,46,-119,-22,-21,-116,-1,-38,-116,-25,-60,-113,-48,-7,-113,-59,-20,-115,-54,-21,-115,-57,-29,0},
15+
.length = 83,
16+
};
17+
18+
/** Important characters that need to be encrypted, such as appkey, appsecret, appid, database password, etc */
19+
const SKEncryptString * const _4038772756 = &(SKEncryptString){
20+
.factor = (char)100,
21+
.value = (char[]){57,31,4,0,20,29,1,2,6,73,19,26,21,29,7,10,20,9,0,26,80,6,28,14,18,73,14,9,23,13,80,6,27,79,4,12,64,9,28,10,2,11,4,27,3,13,76,76,1,28,19,26,84,14,21,73,1,28,2,2,21,11,88,79,7,25,16,31,23,10,2,23,0,67,70,8,16,28,27,13,92,82,16,14,18,8,2,13,1,12,80,2,21,28,21,30,15,30,22,69,80,23,0,12,0},
22+
.length = 104,
23+
.key = (char[]){119,117,115,104,97,110,103,107,117,110,0},
24+
.kl = 10
25+
};
26+
27+
/** appkeyhdfoashiodfowehfowefjqwehgpqegpifhwappkey */
28+
const SKEncryptString * const _3908173925 = &(SKEncryptString){
29+
.factor = (char)50,
30+
.value = (char[]){4,20,23,14,1,30,13,0,1,10,5,20,13,13,8,1,2,8,18,1,15,3,11,16,0,2,13,20,19,2,13,3,23,20,1,0,21,13,1,13,19,6,21,20,12,0,29,0},
31+
.length = 47,
32+
.key = (char[]){98,99,96,0},
33+
.kl = 3
34+
};
35+
36+
/** Swift版:https://github.com/Xcoder1011/SKGenerateModelTool */
37+
const SKEncryptString * const _1242105574 = &(SKEncryptString){
38+
.factor = (char)24,
39+
.value = (char[]){40,12,18,29,15,-100,-14,-13,-108,-57,-31,19,15,15,11,8,65,84,84,28,18,15,19,14,25,85,24,20,22,84,35,24,20,31,30,9,74,75,74,74,84,40,48,60,30,21,30,9,26,15,30,54,20,31,30,23,47,20,20,23,0},
40+
.length = 60,
41+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// SKEncryptHeader.h
3+
// SKGenerateModelTool
4+
//
5+
// Created by shangkun on 2020/5/27.
6+
// Copyright © 2020 wushangkun. All rights reserved.
7+
//
8+
//
9+
10+
/////////////////////////////////////////////////////////////////////////////////////
11+
/// https://github.com/Xcoder1011/SKGenerateModelTool
12+
/////////////////////////////////////////////////////////////////////////////////////
13+
14+
/*
15+
16+
1. 只需把此头文件(SKEncryptHeader.h)加入到项目,并在pch文件中导入该头文件即可使用;
17+
1. Just add this header file (SKEncryptHeader.h) to the project and import the header file in the pch file to use;
18+
19+
2. 加密:使用SKGenerateModelTool进行数据加密,支持自定义密钥key;
20+
2. Encryption: Use SKGenerateModelTool for data encryption and support custom key;
21+
22+
3. 解密:(参考以下范例)
23+
3. Decrypt: (refer to the following example)
24+
25+
if let string = sk_OCString(_3596508958) {
26+
print("示例:解密后的数据为:\(string)")
27+
}
28+
29+
*/
30+
31+
/////////////////////////////////////////////////////////////////////////////////////
32+
///
33+
/////////////////////////////////////////////////////////////////////////////////////
34+
35+
36+
#ifndef SKEncryptHeader_h
37+
#define SKEncryptHeader_h
38+
39+
#ifdef __cplusplus
40+
extern "C" {
41+
#endif
42+
43+
typedef struct {
44+
char factor;
45+
char *value;
46+
int length;
47+
char *key;
48+
int kl;
49+
char decoded;
50+
}SKEncryptString;
51+
52+
/// retutn decrypted C string.
53+
/// @param data encrypted data
54+
static inline const char *sk_CString(const SKEncryptString *data) {
55+
if (data->decoded == 1) {
56+
return data->value;
57+
}
58+
int kl = data->kl;
59+
char a = 99;
60+
if (kl > 0) {
61+
char b = 100;
62+
for (int i = 0; i < kl; i++) {
63+
data->key[i] ^= (data->factor ^ b);
64+
}
65+
int cipherIndex = 0;
66+
for (int i = 0; i < data->length; i++) {
67+
cipherIndex = cipherIndex % kl;
68+
data->value[i] ^= (data->factor ^ a ^ data->key[cipherIndex]);
69+
cipherIndex++;
70+
}
71+
} else {
72+
for (int i = 0; i < data->length; i++) {
73+
data->value[i] ^= (data->factor ^ a);
74+
}
75+
}
76+
((SKEncryptString *)data)->decoded = 1;
77+
return data->value;
78+
}
79+
80+
81+
#ifdef __OBJC__
82+
#import <Foundation/Foundation.h>
83+
84+
/// retutn decrypted NSString.
85+
/// @param data encrypted data
86+
static inline NSString *sk_OCString(const SKEncryptString *data)
87+
{
88+
return [NSString stringWithUTF8String:sk_CString(data)];
89+
}
90+
#endif
91+
92+
#ifdef __cplusplus
93+
}
94+
#endif
95+
96+
#endif /* SKEncryptHeader_h */

0 commit comments

Comments
 (0)