-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMixDecrypt.h
58 lines (46 loc) · 1.68 KB
/
MixDecrypt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// MixDecrypt.h
//
// Created by Dan.Lee on 2017/3/10.
//
#ifndef MixDecrypt_h
#define MixDecrypt_h
#import <Foundation/Foundation.h>
#include <stdio.h>
@implementation NSData (ITTAdditions)
- (NSString *)dl_decryptTextUsingXOR {
if (self == nil || self.length <= 0) {
return nil;
}
Byte *dataBytes = (Byte *)[self bytes];
NSUInteger originalBytesLength = self.length;
Byte randomByte = dataBytes[originalBytesLength - 2];
// Byte version = dataBytes[originalBytesLength + 1];
for (int i = 0; i < originalBytesLength - 2; i++) {
dataBytes[i] ^= randomByte;
}
Byte *bytesBuffer = malloc(originalBytesLength - 2);
memcpy(bytesBuffer, dataBytes, originalBytesLength - 2);
NSData *decryptedData = [[NSData alloc] initWithBytes:bytesBuffer length:(originalBytesLength - 2)];
NSString *decryptedString = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
free(bytesBuffer);
bytesBuffer = NULL;
return decryptedString;
}
@end
static NSString *dl_getRealText(NSString *obscureText)
{
static dispatch_once_t onceToken;
static NSCache *storeData;
dispatch_once(&onceToken, ^{
storeData = [NSCache new];
});
if (![storeData objectForKey:obscureText]) {
// This is the decryptText, you can use your custom decrypt method
NSString *decryptText = [[[NSData alloc] initWithBase64EncodedString:obscureText options:NSDataBase64DecodingIgnoreUnknownCharacters] dl_decryptTextUsingXOR];
[storeData setObject:decryptText
forKey:obscureText];
}
return [storeData objectForKey:obscureText];
}
#endif /* MixDecrypt_h */