iOSRSA加解密


iOS RSA加解密,签名效验。

分为两种,证书版公私钥和无证书版公私钥。


demo代码地址:

https://github.com/zaiweiwoaini/RSAEncryptDecrypt



1、证书版RSA(系统方法):

公钥证书例如:der格式。私钥证书例如:p12格式,pfx格式。

调用示例:

    //加载秘钥

    NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"der"];

    NSString *privateKeyPath = [[NSBundle mainBundle] pathForResource:@"pkcs" ofType:@"p12"];


    [[ZWRSA sharedInstance] loadPublicKeyFromCertificateFile:publicKeyPath];

    [[ZWRSA sharedInstance] loadEveryThingFromPKCS12File:privateKeyPath passphrase:@"123456"];

    

    //加密

    NSString *str = [[ZWRSA sharedInstance] encryptStringWithSourceString:@“qwerty”];

   //解密

    NSString *str  = [[ZWRSA sharedInstance] decryptStringWithSourceString:_labelA.text];




2、无证书版RSA公私钥(openssl方法)

公钥证书例如:pem格式,内容-----BEGIN PUBLIC KEY-----   xxxxx  -----END PUBLIC KEY-----


私钥证书例如:pem格式,内容 -----BEGIN RSA PRIVATE KEY-----   xxxxx  -----END RSA PRIVATE KEY-----

//加载秘钥

    ZWOpenSSLRSA *rsa = [ZWOpenSSLRSA sharedInstance];

    [rsa importRSAPublicKeyPEMFilePath:[[NSBundle mainBundle] pathForResource:@"rsa_public_key" ofType:@"pem"]];

    [rsa importRSAPrivateKeyPEMFilePath:[[NSBundle mainBundle] pathForResource:@"rsa_private_key" ofType:@"pem"]];

//加密

    NSString *str1 = [[ZWOpenSSLRSA sharedInstance] decryptWithPrivateKeyUsingPadding:RSA_PADDING_TYPE_PKCS1 cipherString:_labelB.text];

//解密

    NSString *signString = [[ZWOpenSSLRSA sharedInstance] signWithPrivateKeyUsingDigest:RSA_SIGN_DIGEST_TYPE_SHA256 plainString:_TextFiledB.text];


3、签名,效验,其他内容详见demo。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值