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。





你可能感兴趣的:(IOS,rsa,解密)