当前位置:   article > 正文

iOS使用security.framework实现RSA加解密_(__bridge cfdataref)

(__bridge cfdataref)

最近研究了下使用security.framework框架进行RSA加解密。从cer证书文件中读取公钥,从pfx证书文件中读取私钥

1、从cer证书文件中读取公钥

  1. /**
  2. * 获取公钥
  3. *
  4. * @return
  5. */
  6. - (SecKeyRef)getPublicKey {
  7. if (!_publicKey) {
  8. [self loadPublicKeyFromFile:self.cerFilePath];
  9. }
  10. return _publicKey;
  11. }
  12. /**
  13. * 通过文件路径加载公钥
  14. *
  15. * @param cerFilePath 公钥文件路径
  16. */
  17. - (void)loadPublicKeyFromFile:(NSString *)cerFilePath {
  18. if (self.cerFilePath == nil) {
  19. return;
  20. }
  21. NSData *cerData = [[NSData alloc] initWithContentsOfFile:cerFilePath];
  22. [self loadPublicKeyFromData:cerData];
  23. }
  24. /**
  25. * 通过NSData加载公钥
  26. * (此方法可用于将公钥配置在服务端,以Base64字符串传到移动端来加载)
  27. * @param derData 公钥data
  28. */
  29. - (void)loadPublicKeyFromData:(NSData *)cerData {
  30. _publicKey = [self getPublicKeyRefrenceFromeData:cerData];
  31. }
  32. #pragma mark - Private Methods
  33. /**
  34. * (私有方法)从data获取公钥
  35. *
  36. * @param derData data
  37. *
  38. * @return 公钥
  39. */
  40. - (SecKeyRef)getPublicKeyRefrenceFromeData:(NSData *)certData {
  41. SecKeyRef publicKeyRef = NULL;
  42. CFDataRef myCertData = (__bridge CFDataRef)certData;
  43. SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef)myCertData);
  44. if (cert == nil) {
  45. NSLog(@"Can not read certificate from %@", self.cerFilePath);
  46. return nil;
  47. }
  48. SecPolicyRef policy = SecPolicyCreateBasicX509();
  49. SecCertificateRef certArray[1] = {cert};
  50. CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray,1, NULL);
  51. SecTrustRef trust;
  52. OSStatus
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/332166
推荐阅读
相关标签
  

闽ICP备14008679号