|
@@ -1,230 +0,0 @@
|
|
|
-//
|
|
|
-// ERequestEncrypt.m
|
|
|
-// Kido Watch
|
|
|
-//
|
|
|
-// Created by 振兴 刘 on 2019/12/3.
|
|
|
-// Copyright © 2019 BaH Cy. All rights reserved.
|
|
|
-//
|
|
|
-
|
|
|
-#import "ERequestEncrypt.h"
|
|
|
-#import "NSString+EAddition.h"
|
|
|
-#import "NSString+md5.h"
|
|
|
-#import "NSData+AES.h"
|
|
|
-
|
|
|
-@implementation ERequestEncryptEntity
|
|
|
-@end
|
|
|
-
|
|
|
-
|
|
|
-@implementation ERequestEncrypt
|
|
|
-
|
|
|
-+ (NSMutableDictionary *)encryptWithParameters:(NSDictionary *)parameters {
|
|
|
- return nil;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-+ (ERequestEncryptEntity *)getURLRquestAesEntity
|
|
|
-{
|
|
|
- ERequestEncryptEntity * entity = [[ERequestEncryptEntity alloc]init];
|
|
|
-
|
|
|
- NSString * uuid = [NSString uniqueString];
|
|
|
- NSString * seedStr = [[self class] getAES256EncrypteRandomStrForUUID:uuid];
|
|
|
- NSString * msec_time = [NSString stringWithFormat:@"%@",@((long long)([[NSDate date] timeIntervalSince1970] * 1000))];
|
|
|
- NSString * timestamp = [NSString stringWithFormat:@"%@",@((long long)([[NSDate date] timeIntervalSince1970]))];
|
|
|
-
|
|
|
-
|
|
|
- NSString * token = @"";
|
|
|
- NSString * secret =@"";
|
|
|
- NSString * qid = @"";
|
|
|
-
|
|
|
- NSString * key = [[self class] getAES256EncrypteKeyForSeed:seedStr Token:token Secret:secret QidMd5:[qid md5HexEncode] TimestampMd5:[msec_time md5HexEncode]];
|
|
|
- NSString * iv = [[self class] getAES256EncrypteIVForMd5Seed:[seedStr md5HexEncode] SecretMd5:[secret md5HexEncode] QidMd5:[qid md5HexEncode] TimestampMd5:[msec_time md5HexEncode]];
|
|
|
-
|
|
|
- entity.uuid = uuid;
|
|
|
- entity.seedStr = seedStr;
|
|
|
- entity.msec_time = msec_time;
|
|
|
- entity.timestamp = timestamp;
|
|
|
- entity.token = token;
|
|
|
- entity.secret = secret;
|
|
|
- entity.qid = qid;
|
|
|
- entity.key = key;
|
|
|
- entity.iv = iv;
|
|
|
-
|
|
|
- return entity;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSMutableDictionary *)requesEncryptWithParameters:(NSDictionary *)parameters entity:(ERequestEncryptEntity*)entity
|
|
|
-{
|
|
|
- NSString * uuid = entity.uuid;
|
|
|
- NSString * seedStr = entity.seedStr;
|
|
|
- NSString * msec_time = entity.msec_time;
|
|
|
- NSString * timestamp = entity.timestamp;
|
|
|
- NSString * key = entity.key;
|
|
|
- NSString * iv = entity.iv;
|
|
|
-
|
|
|
- NSLog(@"===================");
|
|
|
- NSLog(@"parameters:%@", parameters);
|
|
|
- NSLog(@"uuid:%@", uuid);
|
|
|
- NSLog(@"seedStr:%@", seedStr);
|
|
|
- NSLog(@"msec_time:%@", msec_time);
|
|
|
- NSLog(@"key:%@", key);
|
|
|
- NSLog(@"iv:%@", iv);
|
|
|
- NSLog(@"===================");
|
|
|
-
|
|
|
- NSString* requetParamString = [[self class] getParamsEncryptString:parameters SeedStr:seedStr Timestamp:timestamp Msec_time:msec_time uuid:uuid Key:key IV:iv];
|
|
|
-
|
|
|
- NSMutableDictionary* dictParam = [[NSMutableDictionary alloc] init];
|
|
|
- dictParam[@"json"] = requetParamString;
|
|
|
- dictParam[@"seed"] = seedStr;
|
|
|
- dictParam[@"timeMillis"] = msec_time;
|
|
|
- dictParam[@"id"] = [uuid md5HexEncode];
|
|
|
- return dictParam;
|
|
|
-}
|
|
|
-
|
|
|
-+ (id)responseDecryptWithParameters:(id)dataEncrypt entity:(ERequestEncryptEntity*)entity
|
|
|
-{
|
|
|
- NSString * dataStr = dataEncrypt[@"d"];
|
|
|
- if (dataStr == (id)[NSNull null] || dataStr.length <= 0) {
|
|
|
- return nil;
|
|
|
- }
|
|
|
-
|
|
|
- NSData *baseData = [[NSData alloc]initWithBase64EncodedString:dataStr options:0];
|
|
|
- if (baseData) {
|
|
|
- baseData = [baseData AES256Decrypt:entity.key iv:entity.iv];
|
|
|
- }
|
|
|
-
|
|
|
- id dic = [NSJSONSerialization JSONObjectWithData:baseData options:0 error:0];
|
|
|
-
|
|
|
- NSMutableDictionary* ret = [NSMutableDictionary dictionaryWithDictionary:dataEncrypt];
|
|
|
- ret[@"d"] = dic;
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString*)getParamsEncryptString:(NSDictionary*)parameters SeedStr:(NSString *)seedStr Timestamp:(NSString *)timestamp Msec_time:(NSString *)msec_time uuid:(NSString *)uuid Key:(NSString *)key IV:(NSString *)iv{
|
|
|
-
|
|
|
- NSData *data= [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
|
|
|
-
|
|
|
- data = [data AES256Encrypt:key iv:iv];
|
|
|
-
|
|
|
- data = [data base64EncodedDataWithOptions:0];
|
|
|
-
|
|
|
- NSString *encodeString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
-
|
|
|
- return encodeString;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)getAES256EncrypteKeyForUUID:(NSString *)uuid SeedStr:(NSString *)seedStr Timestamp:(NSString *)timestamp
|
|
|
-{
|
|
|
- NSString * uuidMd5 = [uuid md5HexEncode];
|
|
|
-
|
|
|
- if (uuidMd5.length > 30) {
|
|
|
- uuidMd5 = [uuidMd5 substringWithRange:NSMakeRange(9, 16)];
|
|
|
- }
|
|
|
-
|
|
|
- uuidMd5 = [uuidMd5 md5HexEncode];
|
|
|
-
|
|
|
- NSArray * array = [[self class] getEncrypteKeyCompareWithArray:@[seedStr,uuidMd5,[timestamp md5HexEncode]]];
|
|
|
-
|
|
|
- NSString *Key = [array componentsJoinedByString:@""];
|
|
|
-
|
|
|
- Key = [Key md5HexEncode];
|
|
|
-
|
|
|
- return Key;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)getAES256EncrypteIVForUUID:(NSString *)uuid SeedStr:(NSString *)seedStr Timestamp:(NSString *)timestamp
|
|
|
-{
|
|
|
- seedStr = [seedStr md5HexEncode];
|
|
|
-
|
|
|
- NSString * uuidMd5 = [uuid md5HexEncode];
|
|
|
-
|
|
|
- if (uuidMd5.length > 30) {
|
|
|
- uuidMd5 = [uuidMd5 substringWithRange:NSMakeRange(16, 9)];
|
|
|
- }
|
|
|
-
|
|
|
- uuidMd5 = [uuidMd5 md5HexEncode];
|
|
|
-
|
|
|
- NSArray * array = [[self class] getEncrypteKeyCompareWithArray:@[seedStr,uuidMd5,[timestamp md5HexEncode]]];
|
|
|
-
|
|
|
- NSString *Key = [[array componentsJoinedByString:@""] md5HexEncode];
|
|
|
- if (Key.length > 30) {
|
|
|
- Key = [Key substringWithRange:NSMakeRange(9, 16)];
|
|
|
- }
|
|
|
-
|
|
|
- return Key;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)getAES256EncrypteRandomStrForUUID:(NSString *)uuid
|
|
|
-{
|
|
|
- NSString *uuidMD5 = [uuid md5HexEncode];
|
|
|
-
|
|
|
- NSString * timestamp = [NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970] * 1000]];
|
|
|
-
|
|
|
- NSString * seedStr = [[NSString stringWithFormat:@"%@%@%@",uuidMD5,timestamp,[NSString return16LetterAndNumber]] md5HexEncode];
|
|
|
-
|
|
|
- return seedStr;
|
|
|
-}
|
|
|
-
|
|
|
-#pragma mark 登录后
|
|
|
-+ (NSString *)getAES256EncrypteKeyForSeed:(NSString *)seed Token:(NSString *)token Secret:(NSString *)secret QidMd5:(NSString *)qidMd5 TimestampMd5:(NSString *)timestampMd5
|
|
|
-{
|
|
|
- NSArray * array = [[self class] getEncrypteKeyCompareWithArray:@[seed,token,secret,qidMd5,timestampMd5]];
|
|
|
-
|
|
|
- NSString *Key = [array componentsJoinedByString:@""];
|
|
|
-
|
|
|
- Key = [Key md5HexEncode];
|
|
|
-
|
|
|
- return Key;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)getAES256EncrypteIVForMd5Seed:(NSString *)seedMd5 SecretMd5:(NSString *)secretMd5 QidMd5:(NSString *)qidMd5 TimestampMd5:(NSString *)timestampMd5
|
|
|
-{
|
|
|
- NSArray * array = [[self class] getEncrypteKeyCompareWithArray:@[seedMd5,secretMd5,qidMd5,timestampMd5]];
|
|
|
-
|
|
|
- NSString *Key = [[array componentsJoinedByString:@""] md5HexEncode];
|
|
|
- if (Key.length > 30) {
|
|
|
- Key = [Key substringWithRange:NSMakeRange(9, 16)];
|
|
|
- }
|
|
|
-
|
|
|
- return Key;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-+ (NSArray *)getEncrypteKeyCompareWithArray:(NSArray *)array
|
|
|
-{
|
|
|
- NSArray *myary = [array sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString * _Nonnull obj2) {
|
|
|
- return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
|
|
|
- }];
|
|
|
- return myary;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-+ (NSString *)getLoginUUID {
|
|
|
- NSString* strUuid = [self getRandomUUID];
|
|
|
- strUuid = [strUuid substringToIndex:10];
|
|
|
- return strUuid;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)getRandomUUID {
|
|
|
- NSMutableString *mStrUUID = [NSMutableString string];
|
|
|
- for (int i=0; i<10; i++) {
|
|
|
- int index = arc4random() % 9;
|
|
|
- [mStrUUID appendFormat:@"%d",index];
|
|
|
- }
|
|
|
- return mStrUUID;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-+ (NSString*)stringINJSONFormatForObject:(id)obj
|
|
|
-{
|
|
|
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj
|
|
|
- options:0 // If that option is not set, the most compact possible JSON will be generated
|
|
|
- error:nil];
|
|
|
-
|
|
|
- NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
- jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
|
|
|
-
|
|
|
- return jsonString;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-@end
|