你的位置:首页 > 操作系统

[操作系统]JSPath安全部署


前言

这个事JSPath集成到客户端的第二篇,第一篇链接: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-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMKLDyVSf9GSlMWs\niwLj2L0RNHVFm5zbrnHt6Y5iBIsXgTYuS/FmTSSUgLvgwEMAGHI35MVDYn0WQlfx\neTNCSudO/8aHS135T7QPzi0rzU0nMFrX6k9cTnkmNqvA0vN/VUR9QFRYygYpNVBX\nTmwLQmOxjJebqZsWryP2/vKA78VBAgMBAAECgYAxzfwAqBBFDOu9d6DXCpHQRXlS\nlJF0JOGSFUB3zSwwl8HipxOWJ4SUWthiQPW3sWyQwTJ2Nfwe3fX6Ib3Pm6clAlZI\nBtz1+OltkryrQqEzLP1cw//gSYtcK/QqUZLvBMyrBZTq/9HlmFS7j8J4VyOfwMjO\n7VU9qEnegzLLyRNySQJBAN+cyrxMCCENiTq63v19C25KdpESIH6aPjp+C1cXpCtG\n7snnHMOz93vZpU+YucNkG09cb4BC5dbWhnlKt8hhvUMCQQDeuGxZfiXQdJplmKFo\nvrKZPpsb1mDUbrf8h8vu2sNTBfIPwJjTel9zMHV3fWxQK1KKCJy1BFbewEBHRhTi\nAekrAkADKbWYaj7nMMjTIteKoF0sBzOcI7R/9fCRZT0F01jIH+26v2Ehx4blopN7\nB78A64aTXcxZ9RhJLKmkR5FH7VH/AkEAu5S+hN1PQfODOoBDqbG7QIBPKTH0mdaq\nlUFWTFTp35oJVqUS0kKl86FjvgDf4eGrAeDhA0ClXbS0LpNi8TtEwwJBAMPl5eFW\nB6vqUT57/Fjvdrv8oK5gov/bDmf0ueeoRh4E8AV/Gp5FuhHvoaLLj3uZ35O+0gQJ\nkmuxBVt/k9+EqmQ=\n-----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-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCiw8lUn/RkpTFrIsC49i9ETR1\nRZuc265x7emOYgSLF4E2LkvxZk0klIC74MBDABhyN+TFQ2J9FkJX8XkzQkrnTv/G\nh0td+U+0D84tK81NJzBa1+pPXE55JjarwNLzf1VEfUBUWMoGKTVQV05sC0JjsYyX\nm6mbFq8j9v7ygO/FQQIDAQAB\n-----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(@"匹配失败");  }

最新demo链接:http://pan.baidu.com/s/1i4yEejV