iOS RSA的加密和签名

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

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

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

关键代码

    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);

公钥和私钥都是字符串,用字符串加密也可以。

 Dem下载 密码:4bpw

原文地址:https://www.cnblogs.com/qq95230/p/5908407.html