分别完成RSA和AES之后我们需要汇通两者对接口进行加密操作,逻辑流程图(java端同事简书说明)。
核心代码如下:
NSDictionary *stuDic = [NSDictionary dictionaryWithObjectsAndKeys:
@"aaabbbc",@"userAccount",
@"123456",@"phoneCode",
@"123456",@"password",
@"1",@"AppTye",
nil];
NSString *tempStr = [@"1|" stringByAppendingString:[self dictionaryToJson:stuDic]];
NSString * jsonString =[self HAReplaceString:[NSString stringWithFormat:@"1|%@",[self dictionaryToJson:stuDic]] excuseString:@" " replaceSting:@""] ;
NSString* random =[NSString stringWithFormat:@"%@",[self getRandomNumber]];
// NSString * BSAESKey = [GTMBase64 stringByEncodingData:[@"123456789" dataUsingEncoding:NSUTF8StringEncoding]];
NSString * BSAESKey = [GTMBase64 stringByEncodingData:[random dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(@"random==\r%@",random);
NSString * enAESkey = [BBRSACryptor encryptString:BSAESKey publicKey:public_key_string];
// NSString * enParam = [BBRSACryptor encryptString:jsonString publicKey:public_key_string];
NSString * sign = [BBRSACryptor singString:jsonString privateKey:private_key_string];
// NSLog(@"签名:\r%@",sign);
NSString * enParam = [SecurityUtil encryptAESData:jsonString app_key:random];
// NSLog(@"enString==\r%@",enParam);
// NSString * deString = [SecurityUtil decryptAESString:enParam app_key:@"1234567891234567"];
// NSString * enParam = [ BBRSACryptor AESEncryptWithKey:BSAESKey plainData:jsonString];
// NSLog(@"deString==%@",deString);;
// NSDictionary * dic =@{@"param":[NSString stringWithFormat:@"%@",enParam],@"aesKey":BSAESKey};
NSDictionary * dic =@{@"param":[NSString stringWithFormat:@"%@|%@|%@",enAESkey,enParam,sign]};
// NSLog(@"dic==%@===",dic);
[HABaseRequest requestWithURLName:@"register/registV"
Parameter:dic
SuccessBlock:^(id returnValue) {
NSLog(@"returnValue ==%@",returnValue);
NSString * deString = [SecurityUtil decryptAESString:returnValue app_key:random];
NSLog(@"deString ==%@",deString);
} FailBlock:^(NSError *error) {
}];
};
//生成随机数
- (NSString *)getRandomNumber{
NSString *strRandom = @"";
for(int i=0; i<16; i++)
{
strRandom = [ strRandom stringByAppendingFormat:@"%i",(arc4random() % 9)];
}
return strRandom;
}
//格式化Json
-(NSString *)HAReplaceString:(NSString *)replaceString excuseString:(NSString *)excuse replaceSting:(NSString *)replace{
NSString *tempString = nil;
if([replaceString rangeOfString:excuse].location != NSNotFound){
//NSLog(@"字符串存在+");
tempString = [replaceString stringByReplacingOccurrencesOfString:excuse withString:replace];
return tempString;
}
//DLog(@"temp=%@==%@=%@", replaceString,excuse,replace);
return replaceString;
}
三方联调参考代码
****RAS+AES同步请求****
工程中由于需要在一个模块中进行下载操作,引入了同步请求,是因为使用AF的同步并不能达到理想效果,然而在我未使用第三方框架前提下,对上传数据需要做一些转码操作方能进行加密任务,不然会对加密结果产生影响,显然需要一个方案来解决这个问题,encode URL这个可以对一些非法字符做转码操作来格式化链接
/**
* URL encode
*
* @param unencodedString <#unencodedString description#>
*
* @return String
*/
+(NSString*)encodeString:(NSString*)unencodedString{
// CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
// CharactersToLeaveUnescaped = @"[].";
NSString*encodedString=(NSString*)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
如有问题请留言······
网友评论