前言
这个事JSPatch集成到客户端的第二篇,第一篇链接:http://www.cnblogs.com/hxwj/p/5163158.html
安全部署链接:http://blog.cnbang.net/tech/2879/
我这里就是将原文的方案三RSA效验实现了
首先我们要得到公钥私钥,可以看我之前的一篇文章:http://www.cnblogs.com/hxwj/p/5163635.html
服务器端(由于没有写服务器我用客户端模拟了下)
#pragma mark - 服务器操作 //1.得到私钥 NSString *privkey = @"-----BEGIN PRIVATE KEY----- MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMKLDyVSf9GSlMWs iwLj2L0RNHVFm5zbrnHt6Y5iBIsXgTYuS/FmTSSUgLvgwEMAGHI35MVDYn0WQlfx eTNCSudO/8aHS135T7QPzi0rzU0nMFrX6k9cTnkmNqvA0vN/VUR9QFRYygYpNVBX TmwLQmOxjJebqZsWryP2/vKA78VBAgMBAAECgYAxzfwAqBBFDOu9d6DXCpHQRXlS lJF0JOGSFUB3zSwwl8HipxOWJ4SUWthiQPW3sWyQwTJ2Nfwe3fX6Ib3Pm6clAlZI Btz1+OltkryrQqEzLP1cw//gSYtcK/QqUZLvBMyrBZTq/9HlmFS7j8J4VyOfwMjO 7VU9qEnegzLLyRNySQJBAN+cyrxMCCENiTq63v19C25KdpESIH6aPjp+C1cXpCtG 7snnHMOz93vZpU+YucNkG09cb4BC5dbWhnlKt8hhvUMCQQDeuGxZfiXQdJplmKFo vrKZPpsb1mDUbrf8h8vu2sNTBfIPwJjTel9zMHV3fWxQK1KKCJy1BFbewEBHRhTi AekrAkADKbWYaj7nMMjTIteKoF0sBzOcI7R/9fCRZT0F01jIH+26v2Ehx4blopN7 B78A64aTXcxZ9RhJLKmkR5FH7VH/AkEAu5S+hN1PQfODOoBDqbG7QIBPKTH0mdaq lUFWTFTp35oJVqUS0kKl86FjvgDf4eGrAeDhA0ClXbS0LpNi8TtEwwJBAMPl5eFW B6vqUT57/Fjvdrv8oK5gov/bDmf0ueeoRh4E8AV/Gp5FuhHvoaLLj3uZ35O+0gQJ kmuxBVt/k9+EqmQ= -----END PRIVATE KEY-----"; //2.加密得到js的md5 NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; //md5字符串 NSString *md5String = [self md5:script]; NSLog(@"%@",md5String); //3.私钥rsa加密生成字符串 NSString *fuwuqiString = @"wIEc7z5OcsKkt1rpgsCXop2RMhSeOA09Hmi8UzRKy7kZp3fWvmxy4DKBl9JVQrEm7iuYB2zNY0IvsSB8PcHvKdzkuNRqhNFkSYBt+3PG63v0NQEaYR6i9HXvF5ZI5h5gLpCZ18bZmhpciaZK+kOZd1BiFdswSrdZp5JaWl19ESM=";
客户端
#pragma mark - 客户端操作,可以等待服务器,等待时间可以10's左右,网络请求后得到数据,然后还可以加一个开关按钮,可决定是否传这个js文件,以免出现BUG //公钥 NSString *pubkey = @"-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCiw8lUn/RkpTFrIsC49i9ETR1 RZuc265x7emOYgSLF4E2LkvxZk0klIC74MBDABhyN+TFQ2J9FkJX8XkzQkrnTv/G h0td+U+0D84tK81NJzBa1+pPXE55JjarwNLzf1VEfUBUWMoGKTVQV05sC0JjsYyX m6mbFq8j9v7ygO/FQQIDAQAB -----END PUBLIC KEY-----"; //服务器返回的RSA私钥加密的字符串字符串 NSString *fuwuqiMD5String = fuwuqiString; //服务器返回的JS字符串 NSString *script1 = script; //公钥解密得到解密后的MD5字符串 NSString *decWithPrivKey = [RSA decryptString:fuwuqiMD5String publicKey:pubkey]; [JPEngine startEngine]; if ([[self md5:script1] isEqualToString:decWithPrivKey]) { NSLog(@"匹配成功"); [JPEngine evaluateScript:script]; }else { NSLog(@"匹配失败"); }