【问题标题】:iPhone encryption with certificate带证书的 iPhone 加密
【发布时间】:2011-02-16 16:32:16
【问题描述】:

我必须加密一个字符串并在我的 xCode 项目的 Resources 文件夹中有一个 .CER (x.509)。过去两天我一直在想象如何,但没有成功,所以是时候问了。

Apple 的文档很难阅读……而且我认为这个框架可能是最难理解的……这些示例都没有帮助。

所以,我尝试了以下代码:显然它不起作用:

在我的 Mac 上,我使用了 OPENSSL,但我发现无法在 SDK 中重新创建 OPENSSL 命令。所以,我很困惑......有人吗???

非常感谢:)

NSString *certPath = [[NSBundle mainBundle] pathForResource:@"Certificate" ofType:@"cer"]; 
    SecCertificateRef myCertificate = nil;

    NSData *certificateData = [[NSData alloc] initWithContentsOfFile:certPath]; 
    myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);

    SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
    SecTrustRef myTrust;
    SecTrustCreateWithCertificates(myCertificate, myPolicy, &myTrust);
    SecKeyRef publicKey = SecTrustCopyPublicKey(myTrust);



        uint8_t *pPlainText = (uint8_t*)"This is a test";
        uint8_t aCipherText[1024];
        size_t iCipherLength = 1024;
        OSStatus status = SecKeyEncrypt(publicKey,
                                        kSecPaddingPKCS1,
                                        pPlainText,
                                        strlen( (char*)pPlainText ) + 1,
                                        aCipherText,
                                        &iCipherLength);


        }

【问题讨论】:

  • 你找到解决这个@Fabio的方法了吗

标签: iphone security encryption sdk openssl


【解决方案1】:

我尝试了您更新后的代码,并在末尾添加了两行:

NSData *cipherData = [NSData dataWithBytes:aCipherText length:iCipherLength];
NSLog(@"(%d) %@", status, cipherData);

效果很好:

2011-02-17 22:24:04.204 Untitled[45121:207] (0) <87a2eb07 25ab693a 7fe88329 974b6820
843c5c33 8c5d4606 aecea682 0176e4cb 10482c9b fd2e2242 1c77d349 d3037e91 8d704783
f2e04c82 ef273815 bdb6aa73 f8646542 243f3e12 518147ba 53636441 fd9399d3 b198ed6a
615d51d1 4105fb75 27180f0d 09835551 5162e156 33dedf39 a87e17f8 16881990 c5e57a38
7cd7ec63>

现在的一个区别是我使用的公钥在我的钥匙串中。如果不是,您可能需要查看下面的 importing-an-ssl-cert-under-the-iphone-sdk 链接。到目前为止,我只在模拟器上进行了测试,也可能有所不同,但我相信这是正确的。

如果您仍然遇到问题,请务必检查每次调用的结果(如果返回 OSStatus,请检查)。哪一块失败了?


在拨打SecTrustCopyPublicKey() 之前,您忘记在您的SecTrustRef 上拨打SecTrustEvaluate()。查看SecTrustCopyPublicKey() 上的文档,其中解释了这一点。


没那么有用的旧信息:

我相信这些帖子应该为您指明正确的方向:

http://greghaygood.com/2009/01/17/asymmetric-encryption-with-the-iphone-sdk-and-securityframework

Importing an SSL cert under the iPhone SDK

另外请注意,如果您已经有 Mac 的 OpenSSL 代码,您可以编译 iPhone 的 OpenSSL。当我开发构建脚本时,这篇文章对我很有用:

http://www.therareair.com/2009/01/01/tutorial-how-to-compile-openssl-for-the-iphone/

【讨论】:

  • 谢谢 :) 我已经尝试过了……这真的需要很多时间,我认为已经阅读了所有可能的内容。你(或任何人)能告诉我我的代码中缺少什么吗?我想这一定是对的!我只想记录加密字符串...
  • 有人吗? :( 我真的不习惯要求样品,但这是 Apple 文档很烂而且没有人发布样品的情况...... :( 真的很沮丧......
  • 我刚买了两张 Apple Developer Support Ticket,希望他们能给我解决方案。非常感谢您的帮助,但我仍然缺少一些东西。如果我从 Apple 获得解决方案,我将在此处发布解决方案!
猜你喜欢
  • 2012-05-16
  • 1970-01-01
  • 2010-09-16
  • 2023-03-12
  • 1970-01-01
  • 2012-05-01
  • 2012-11-24
  • 2019-08-04
  • 1970-01-01
相关资源
最近更新 更多