iPhone 使用证书加密

发布于 2024-10-17 17:04:35 字数 1322 浏览 7 评论 0原文

我必须加密一个字符串,并在 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);


        }

I have to encrypt a string and have a .CER (x.509) in the Resources folder of my xCode project. The last two days I have spent to imagine how, but no success, so it's time to ask.

The documentation of Apple is very difficult to read... and I see this framework is probably the hardest one to understand... neither the samples have helped.

So, I have tried with the following code: obviously it does not work:

On my Mac I used OPENSSL but I found no way to recreate OPENSSL commands in the SDK. So, I'm pretty confused... anyone???

Thanks a lot :)

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);


        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-10-24 17:04:35

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

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/ametry-encryption-with-the-iphone-sdk-and-securityframework< /a>

在 iPhone SDK 下导入 SSL 证书

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

I tried your updated code with the addition of two lines at the end:

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

That works fine:

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>

Now one difference is that the public key I'm using is in my keychain. If yours isn't, you may want to look at the importing-an-ssl-cert-under-the-iphone-sdk link below. So far I've only tested on Simulator, which also can be different, but I believe this is correct.

If you still have trouble, make sure to check the result of each call (and if it returns OSStatus, check that). Which piece is failing?


You forgot to call SecTrustEvaluate() on your SecTrustRef before calling SecTrustCopyPublicKey(). Check the docs on SecTrustCopyPublicKey() which explains this.


Old information that wasn't as useful:

I believe these posts should point you in the right direction:

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

Importing an SSL cert under the iPhone SDK

Also note that if you have OpenSSL code for Mac already, you can compile OpenSSL for iPhone. This post was useful for me when I developed my build scripts:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文