前言: 這裡算是比較實用的一篇了吧,進行了網路安全部署 準備: bmob賬號,JSPatch,公鑰,私鑰,MD5加密,AFNetWorking下載文件 #import <CommonCrypto/CommonDigest.h> #import <BmobSDK/Bmob.h> #import "AF
前言:
這裡算是比較實用的一篇了吧,進行了網路安全部署
準備:
bmob賬號,JSPatch,公鑰,私鑰,MD5加密,AFNetWorking下載文件
#import <CommonCrypto/CommonDigest.h>
#import <BmobSDK/Bmob.h>
#import "AFNetworking.h"
#import "JPEngine.h"
#import "RSA.h"
原理:
1.使用bmob,得到私鑰加密後的jsMD5,JS文件地址
2.根據地址下載js文件,並且保存到沙盒中
3.根據加密MD5匹配JS文件內容,成功就執行
4.沒有進行網路判斷,只有有網的情況才能執行,沒網的時候就使用以前的OC代碼
代碼片段:
1.請求bmob上的數據
//這裡是bmob的key自己去申請個賬號,創建一個應用就有了 [Bmob registerWithAppKey:@"這裡填寫自己的key"]; BmobQuery *bquery = [BmobQuery queryWithClassName:@"JSPathString"]; //約束,只取版本號為1的 [bquery whereKey:@"version" equalTo:@"1"]; //是1得就取得 [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) { //刪除操作,如果。。。。刪除文件。。。。 if (!error) { if (array.count > 0) { BmobObject *object = array[0]; NSString *PrivateMD5String = [object objectForKey:@"PrivkeyMD5String"]; BmobFile *file = (BmobFile*)[object objectForKey:@"JSData"]; NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //取得第一個Documents文件夾的路徑 NSString *filePath = [path objectAtIndex:0]; // NSLog(@"%@",filePath); [self downloadFileURL:file.url savePath:filePath fileName:file.name tag:0 PrivateMD5String:PrivateMD5String]; } }else { NSLog(@"失敗的情況。。。"); } }];
2.根據提供的地址下載文件
/** * 下載文件,這是用的,網路請求,要換,現在都是af3.0了 */ + (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag PrivateMD5String:(NSString *)PrivateMD5String { NSFileManager *fileManager = [NSFileManager defaultManager]; //檢查本地文件是否已存在 NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName]; //檢查附件是否存在 if ([fileManager fileExistsAtPath:fileName]) { // NSLog(@"本地已經存在"); [self doJSPathWith:PrivateMD5String fileName:fileName]; }else{ //創建附件存儲目錄 if (![fileManager fileExistsAtPath:aSavePath]) { [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil]; } //下載附件 NSURL *url = [[NSURL alloc] initWithString:aUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.inputStream = [NSInputStream inputStreamWithURL:url]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO]; //下載進度控制 [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead); }]; //已經完成下載 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation, id responseObject) { //下載的數據 NSLog(@"下載成功"); [self doJSPathWith:PrivateMD5String fileName:fileName]; } failure:^(AFHTTPRequestOperation * operation, NSError * error) { NSLog(@"下載失敗"); }]; [operation start]; } }
3.匹配檢查JS文件是否正確
+ (void)doJSPathWith:(NSString *)PrivateMD5String fileName:(NSString *)fileName{ #pragma mark - 客戶端操作,可以等待伺服器,等待時間可以10's左右,網路請求後得到數據,然後還可以加一個開關按鈕,可決定是否傳這個js文件,以免出現BUG //公鑰 NSString *pubkey = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCiw8lUn/RkpTFrIsC49i9ETR1\nRZuc265x7emOYgSLF4E2LkvxZk0klIC74MBDABhyN+TFQ2J9FkJX8XkzQkrnTv/G\nh0td+U+0D84tK81NJzBa1+pPXE55JjarwNLzf1VEfUBUWMoGKTVQV05sC0JjsYyX\nm6mbFq8j9v7ygO/FQQIDAQAB"; //伺服器返回的RSA私鑰加密的字元串字元串 NSString *fuwuqiMD5String = PrivateMD5String; //伺服器返回的JS字元串 NSString *script = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];; //公鑰解密得到解密後的MD5字元串 NSString *decWithPrivKey = [RSA decryptString:fuwuqiMD5String publicKey:pubkey]; [JPEngine startEngine]; if ([[self md5:script] isEqualToString:decWithPrivKey]) { // NSLog(@"匹配成功"); [JPEngine evaluateScript:script]; }else { // NSLog(@"匹配失敗"); } }
4.其它(MD5加密)
//md5加密 + (NSString *)md5:(NSString *)input { const char* str = [input UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, (unsigned)strlen((const char *)str), result); NSMutableString *ret = [NSMutableString string]; for(int i = 0; i<16; i++) { [ret appendFormat:@"%02X",result[i]]; } return ret; }