iOS RSA加解密签名和验证
转自:http://www.jianshu.com/p/81b0b54436b8
Pre:在公司负责了一个项目,需要用到iOS RSA验证签名的功能。后台给我的仅仅是一个公钥的字符串。经过起初的一段时间的挣扎,发现远远没有那么简单。iOS RSA是需要证书的,而java的后台只能给我一个公钥字符串。搜索了无数网页还是没找到能用的成型的代码。最后还是参考了下支付宝的SDK的签名机制,明白可以先把公钥字符串写入文件然后读取文件得到openssl 中的RSA结构体指针。现在精心整理了下iOS RSA使用openssll的加密,解密,签名和验证签名的实现。代码地址https://github.com/HustBroventure/iOSRSAHandler
1生成RSA密钥
生成RSA私钥openssl genrsa -out rsa_private_key.pem 1024
生成RSA公钥openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
pem文件可以直接文本编辑器打开
将RSA私钥转换成PKCS8格式openssl pkcs8 -topk8 -inform PEM -in private_rsa.pem -outform PEM -nocrypt -out private_key.pem
(后边一定要加-out private_key.pem将转换后的私钥保存在private_key.pem,不然得到的结果要设置密码且显示在终端中,这个和得到pem中的私钥有差异。)
2导入OpenSSL,导入HBRSAHandler文件夹
小技巧:拖进去后:librarySearchpath:会自动设置,然后拷贝路径到headSearchpath,后边添加/include。
3使用:
两种方式导入密钥然后调用响相应的方法即可。
NSString* private_key_string = @"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALgv/syFH337KzC29KvR0p6cP+glRqjDYAQno5ifafXZjgf1EhBjZblKv+HiLAzNBOlYU1PnLuOOkZj6pg1A5HUZLpsbYa5Mwr1bUHALjXLaB3THCpZX51/b5L14erGo52Jv/j/63YljEtMm8ALmkY8S+3fPxFeY7ya+2VXMEtplAgMBAAECgYAguvauZWGpQ37zUy+7cLfa061PlYAu8TkYw+qAbqOnupdQtq4VF3S2LqBWhZiKVcxvovB70nM0oNsisjfb1xJBpyfDBFug7d+y2f8yr6aTOezoY5DBYEF3Svg9Kp9ra+vvAYX/7fh+tHCU0HOvp0z8ikZiRSWZaQ+3A2GiCIJrwQJBAPKVji89hGAMEWLJJFZaPiLBqZUwR2W/rp7Ely5ddKfjcosHhggHfOb71BnrMOm0h4S85Gx6a87n9R2To0c51q0CQQDCX6yYdt/9JGORyNSXfzMfSZyVOrMpIo77R0YwKa3UOwwLA56l2Lc4AYO10/lyAyZCKse2/5D9ZZUB7xoYEmGZAkB8MEJVPuoY/bSc3RqENrjetERsAwZaObJcx4oaC3AgTxmhwV1FmQfBfKTODBDDZE+Ijedm/ZlZmHhtBtstKJgVAkBKma/DgHRtUscIT90QHBjB3F3FhJb4pbPcyzksCQMXXmY73/LG0ktXqnUjlyy4zm6jnIm0OZgrOQ6chGkubfeZAkBMCGF2tPfEJh8XODOvlw5ADnUiq+Qe/abcpKowkiT9zP+rYT9XJAx7QxChjdwTZb6ahnJY1+ny1emEHUOs2fm8";
NSString* public_key_string = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4L/7MhR99+yswtvSr0dKenD/oJUaow2AEJ6OYn2n12Y4H9RIQY2W5Sr/h4iwMzQTpWFNT5y7jjpGY+qYNQOR1GS6bG2GuTMK9W1BwC41y2gd0xwqWV+df2+S9eHqxqOdib/4/+t2JYxLTJvAC5pGPEvt3z8RXmO8mvtlVzBLaZQIDAQAB";
NSString *publicKeyFilePath = [[NSBundle mainBundle] pathForResource:@"rsa_public_key.pem" ofType:nil];
NSString *privateKeyFilePath = [[NSBundle mainBundle] pathForResource:@"rsa_private_key.pem" ofType:nil];
HBRSAHandler* handler = [HBRSAHandler new];
//两种方式导入
// [handler importKeyWithType:KeyTypePublic andPath:publicKeyFilePath];
//[handler importKeyWithType:KeyTypePrivate andPath:privateKeyFilePath];
[handler importKeyWithType:KeyTypePrivate andkeyString:private_key_string];
[handler importKeyWithType:KeyTypePublic andkeyString:public_key_string];
NSString* sig = [handler signString:@"签名字符串"];
NSString* sigMd5 = [handler signMD5String:@"签名字符串"];
NSLog(@"%@ %@",sig,sigMd5);
BOOL isMatch = [handler verifyString:@"签名字符串" withSign:sig];
BOOL isMatchMd5 = [handler verifyMD5String:@"签名字符串
NSLog(@"%d %d",isMatch,isMatchMd5);
NSString* enString = [handler encryptWithPublicKey:@"加密字符串"];
NSString* deString = [handler decryptWithPrivatecKey:enString];
NSLog(@"%@",deString);
4 结果验证(注:公钥加密,每次加密的结果会不一样,但是私钥签名的结果每次都一样)
![](http://upload-images.jianshu.io/upload_images/1083284-1e900d8911e55fc2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/1083284-7bbb629a4c359b7a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
原文链接:http://www.jianshu.com/p/81b0b54436b8
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。