RSA通過終端openssl指令生成.der
和p12
的證書的加解密就不說了挂洛,太多了
pre:在公司負責了一個項目梢杭,需要用到iOS RSA驗證簽名的功能彤灶。后臺給我的僅僅是一個公鑰的字符串询张。經(jīng)過起初的一段時間的掙扎,發(fā)現(xiàn)遠遠沒有那么簡單橄浓。iOS RSA是需要證書的,而java的后臺只能給我一個公鑰字符串亮航。搜索了無數(shù)網(wǎng)頁還是沒找到能用的成型的代碼荸实。最后還是參考了下支付寶的SDK的簽名機制,明白可以先把公鑰字符串寫入文件然后讀取文件得到openssl 中的RSA結構體指針〗闪埽現(xiàn)在精心整理了下iOS RSA使用openssll的加密准给,解密,簽名和驗證簽名的實現(xiàn)重抖。代碼地址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使用:
兩種方式導入密鑰然后調(diào)用響相應的方法即可。
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 結果驗證(注:公鑰加密畜埋,每次加密的結果會不一樣莫绣,但是私鑰簽名的結果每次都一樣)