iOS RSA的加密和签名

 

一、RSA加密使用服务端给的公钥.pem,RSA签名使用客户端的私钥.pem。this

 参考文章:http://www.jianshu.com/p/4580bee4f62f加密

把文件夹导入项目中,而后配置这两个地方就OK了,若是是新建项目记得关闭Bitcode.spa

 

关键代码code

    HBRSAHandler* handler = [HBRSAHandler new]; //加密
    NSString *PublicFilePath = [[NSBundle mainBundle] pathForResource:@"serverPubKey.pem" ofType:nil]; [handler importKeyWithType:KeyTypePublic andPath:PublicFilePath];//加载公钥
    NSString *test = @"须要加密的数据"; test = [handler encryptWithPublicKey:test]; NSLog(@"加密结果 =%@",test); //签名
    NSString *privateKeyFilePath = [[NSBundle mainBundle] pathForResource:@"clientPriKey.pem" ofType:nil]; [handler importKeyWithType:KeyTypePrivate andPath:privateKeyFilePath];//加载私钥
    NSString *xml = @"this is test message!"; xml = [handler signString:xml]; NSLog(@"签名结果:%@",xml);

 

公钥和私钥都是字符串,用字符串加密也能够。server

 Dem下载 密码:4bpwxml

相关文章
相关标签/搜索