备份CatanBuilding瘦身独立工程
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TDAESEncryptor.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptAlgorithm.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDAESEncryptor : NSObject <TDEncryptAlgorithm>
|
||||
|
||||
@property (nonatomic, copy, readonly) NSData *key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d1974cff31e245d4a80d7890c19ca79
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// TDAESEncryptor.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDAESEncryptor.h"
|
||||
#import <CommonCrypto/CommonCryptor.h>
|
||||
#import "TDLogging.h"
|
||||
|
||||
@interface TDAESEncryptor ()
|
||||
|
||||
@property (nonatomic, copy, readwrite) NSData *key;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDAESEncryptor
|
||||
|
||||
|
||||
- (NSData *)key {
|
||||
if (!_key) {
|
||||
NSUInteger length = 16;
|
||||
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
NSMutableString *randomString = [NSMutableString stringWithCapacity:length];
|
||||
for (NSUInteger i = 0; i < length; i++) {
|
||||
[randomString appendFormat: @"%C", [letters characterAtIndex:arc4random_uniform((uint32_t)[letters length])]];
|
||||
}
|
||||
_key = [randomString dataUsingEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
return _key;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)algorithm {
|
||||
return @"AES";
|
||||
}
|
||||
|
||||
|
||||
- (nullable NSString *)encryptData:(NSData *)obj {
|
||||
if (!obj) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (!self.key) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSData *data = obj;
|
||||
NSUInteger dataLength = [data length];
|
||||
size_t bufferSize = dataLength + kCCBlockSizeAES128;
|
||||
void *buffer = malloc(bufferSize);
|
||||
|
||||
size_t numBytesEncrypted = 0;
|
||||
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
|
||||
kCCAlgorithmAES128,
|
||||
kCCOptionPKCS7Padding | kCCOptionECBMode,
|
||||
[self.key bytes],
|
||||
kCCBlockSizeAES128,
|
||||
nil,
|
||||
[data bytes],
|
||||
dataLength,
|
||||
buffer,
|
||||
bufferSize,
|
||||
&numBytesEncrypted);
|
||||
if (cryptStatus == kCCSuccess) {
|
||||
|
||||
NSData *encryptData = [NSData dataWithBytes:buffer length:numBytesEncrypted];
|
||||
NSMutableData *ivEncryptData = [NSMutableData data];
|
||||
[ivEncryptData appendData:encryptData];
|
||||
|
||||
free(buffer);
|
||||
|
||||
NSData *base64EncodeData = [ivEncryptData base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
|
||||
NSString *encryptString = [[NSString alloc] initWithData:base64EncodeData encoding:NSUTF8StringEncoding];
|
||||
return encryptString;
|
||||
} else {
|
||||
free(buffer);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3356f39043a754358aac9d671c6355d0
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TDRSAEncryptor.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptAlgorithm.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDRSAEncryptor : NSObject <TDEncryptAlgorithm>
|
||||
|
||||
@property (nonatomic, copy) NSString *key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31a27833643c74455bef05a2933ee2eb
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,180 @@
|
||||
//
|
||||
// TDRSAEncryptor.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
@implementation TDRSAEncryptor
|
||||
|
||||
- (void)setKey:(NSString *)key {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
NSString *publicKeyCopy = [key copy];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\r" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\n" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\t" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||||
_key = publicKeyCopy;
|
||||
}
|
||||
|
||||
- (NSString *)algorithm {
|
||||
return @"RSA";
|
||||
}
|
||||
|
||||
- (NSString *)encryptData:(NSData *)data {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *asymmetricPublicKey = self.key;
|
||||
if (!asymmetricPublicKey) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
SecKeyRef keyRef = [self addPublicKey:asymmetricPublicKey];
|
||||
if (!keyRef) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
const uint8_t *srcbuf = (const uint8_t *)[data bytes];
|
||||
size_t srclen = (size_t)data.length;
|
||||
|
||||
size_t block_size = SecKeyGetBlockSize(keyRef) * sizeof(uint8_t);
|
||||
void *outbuf = malloc(block_size);
|
||||
size_t src_block_size = block_size - 11;
|
||||
|
||||
NSMutableData *ret = [[NSMutableData alloc] init];
|
||||
for(int idx=0; idx<srclen; idx+=src_block_size) {
|
||||
size_t data_len = srclen - idx;
|
||||
if (data_len > src_block_size) {
|
||||
data_len = src_block_size;
|
||||
}
|
||||
|
||||
size_t outlen = block_size;
|
||||
OSStatus status = noErr;
|
||||
|
||||
status = SecKeyEncrypt(keyRef,
|
||||
kSecPaddingPKCS1,
|
||||
srcbuf + idx,
|
||||
data_len,
|
||||
outbuf,
|
||||
&outlen
|
||||
);
|
||||
if (status != 0) {
|
||||
ret = nil;
|
||||
break;
|
||||
}else{
|
||||
[ret appendBytes:outbuf length:outlen];
|
||||
}
|
||||
}
|
||||
free(outbuf);
|
||||
CFRelease(keyRef);
|
||||
|
||||
return [ret base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
|
||||
}
|
||||
|
||||
#pragma mark – Private Methods
|
||||
- (SecKeyRef)addPublicKey:(NSString *)aymmetricPublicKey {
|
||||
NSString *key = [aymmetricPublicKey copy];
|
||||
|
||||
NSData *data = [[NSData alloc] initWithBase64EncodedString:key options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
||||
data = [self stripPublicKeyHeader:data];
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
//a tag to read/write keychain storage
|
||||
NSString *tag = @"RSAUtil_PubKey";
|
||||
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
|
||||
|
||||
// Delete any old lingering key with the same tag
|
||||
NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
|
||||
[publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
|
||||
[publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
|
||||
SecItemDelete((__bridge CFDictionaryRef)publicKey);
|
||||
|
||||
// Add persistent version of the key to system keychain
|
||||
[publicKey setObject:data forKey:(__bridge id)kSecValueData];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)
|
||||
kSecAttrKeyClass];
|
||||
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)
|
||||
kSecReturnPersistentRef];
|
||||
|
||||
CFTypeRef persistKey = nil;
|
||||
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
|
||||
if (persistKey != nil) {
|
||||
CFRelease(persistKey);
|
||||
}
|
||||
if ((status != noErr) && (status != errSecDuplicateItem)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
[publicKey removeObjectForKey:(__bridge id)kSecValueData];
|
||||
[publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
|
||||
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
|
||||
|
||||
// Now fetch the SecKeyRef version of the key
|
||||
SecKeyRef keyRef = nil;
|
||||
status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
|
||||
if (status != noErr) {
|
||||
return nil;
|
||||
}
|
||||
return keyRef;
|
||||
}
|
||||
|
||||
- (NSData *)stripPublicKeyHeader:(NSData *)d_key {
|
||||
// Skip ASN.1 public key header
|
||||
if (d_key == nil) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
unsigned long len = [d_key length];
|
||||
if (!len) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
unsigned char *c_key = (unsigned char *)[d_key bytes];
|
||||
unsigned int idx = 0;
|
||||
|
||||
if (c_key[idx++] != 0x30) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
if (c_key[idx] > 0x80) {
|
||||
idx += c_key[idx] - 0x80 + 1;
|
||||
} else {
|
||||
idx++;
|
||||
}
|
||||
|
||||
// PKCS #1 rsaEncryption szOID_RSA_RSA
|
||||
static unsigned char seqiod[] =
|
||||
{ 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
|
||||
0x01, 0x05, 0x00 };
|
||||
if (memcmp(&c_key[idx], seqiod, 15)) {
|
||||
return(nil);
|
||||
}
|
||||
idx += 15;
|
||||
|
||||
if (c_key[idx++] != 0x03) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
if (c_key[idx] > 0x80) {
|
||||
idx += c_key[idx] - 0x80 + 1;
|
||||
} else {
|
||||
idx++;
|
||||
}
|
||||
if (c_key[idx++] != '\0') {
|
||||
return(nil);
|
||||
}
|
||||
// Now make a new NSData from this buffer
|
||||
return([NSData dataWithBytes:&c_key[idx] length:len - idx]);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bee54e226cda4595abb93c0ebbe11df
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// TDRSAEncryptorPlugin.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptProtocol.h"
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDRSAEncryptorPlugin : NSObject <TDEncryptProtocol>
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6d6b82caa9fc42fa81c28fee8c4ad79
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// TDRSAEncryptorPlugin.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDRSAEncryptorPlugin.h"
|
||||
#import "TDAESEncryptor.h"
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
@interface TDRSAEncryptorPlugin ()
|
||||
|
||||
@property (nonatomic, strong) TDAESEncryptor *aesEncryptor;
|
||||
@property (nonatomic, strong) TDRSAEncryptor *rsaEncryptor;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDRSAEncryptorPlugin
|
||||
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_aesEncryptor = [[TDAESEncryptor alloc] init];
|
||||
_rsaEncryptor = [[TDRSAEncryptor alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)symmetricEncryptType {
|
||||
return [_aesEncryptor algorithm];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)asymmetricEncryptType {
|
||||
return [_rsaEncryptor algorithm];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)encryptEvent:(NSData *)event {
|
||||
return [_aesEncryptor encryptData:event];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)encryptSymmetricKeyWithPublicKey:(NSString *)publicKey {
|
||||
if (![_rsaEncryptor.key isEqualToString:publicKey]) {
|
||||
_rsaEncryptor.key = publicKey;
|
||||
}
|
||||
return [_rsaEncryptor encryptData:_aesEncryptor.key];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4d26e5bdde0c4282bf6539cf6df7001
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user