iOSRSAHandler 使用注意结果示例 介绍
iOSRSAHandler 是 iOS下使用openssl的加密解密签名和验证的封装代码。
使用
1.导入openssl和本项目的类文件
2.使用示例
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);
注意
如果使用字符串形式的私钥,要使用openssl pkcs8 -topk8 -inform PEM -in private_rsa.pem -outform
PEM -nocrypt -out private_rsa_pkcs8.pem 转化,取转化后的pem中的私钥字符串。(后边一定要加-out
XXX.pem将转换后的私钥保存在XXX.pem,不然得到的结果要设置密码且显示在终端中,这个和得到pem中的私钥有差异。)
结果示例
iOSRSAHandler 使用注意结果示例 官网
https://github.com/HustBroventure/iOSRSAHandler
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。