Mac 上公钥加密/解密的示例代码?

发布于 2024-10-01 15:27:03 字数 245 浏览 2 评论 0原文

在哪里可以找到一些在 Mac OS X 上进行公钥加密和解密的简单示例代码?我很沮丧,Apple 的“证书、密钥和信任服务编程指南”展示了如何在 iOS 上执行此操作,但所需的 API(SecKeyEncryptSecKeyDecrypt)是显然在 Mac OS X 上不可用。在“CryptoSample”中可能有一种方法可以做到这一点,但它看起来不清晰或不简单,并且示例项目太旧,无法使用当前版本的 Xcode 打开。

Where can I find some simple sample code for public key encryption and decryption on Mac OS X? I'm frustrated that Apple's "Certificate, Key, and Trust Services Programming Guide" shows how to do this stuff on iOS, but the needed APIs (SecKeyEncrypt, SecKeyDecrypt) are apparently not available on Mac OS X. There's probably a way to do it in "CryptoSample", but it doesn't look clear or simple, and the sample project is too old to open with the current version of Xcode.

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

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

发布评论

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

评论(3

圈圈圆圆圈圈 2024-10-08 15:27:04

安全框架 API 在 Mac OS 版本之间变化相当频繁。最佳方法取决于您的目标版本:

  1. 如果您的代码只需要在 10.7 及更高版本上运行,您可以使用 Security Transforms,这是一种用于加密转换的新高级公共 API。安全转换编程指南有有用(而且简单!)的示例代码:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecTransformPG/SecurityTransformsBasics/SecurityTransformsBasics.html

您需要创建一个使用 SecEncryptTransformCreateSecDecryptTransformCreate 进行转换,使用 SecTransformSetAttribute 设置其输入,并使用 SecTransformExecute 执行它。

  1. 如果你需要支持Mac OS 10.6或更低版本,你必须使用低级且相当可怕的CDSA API。 CryptoSamplecdsaEncrypt 是一个简洁的示例。

https://developer.apple.com/library/archive/ Samplecode/CryptoSample/Listings/libCdsaCrypt_libCdsaCrypt_cpp.html

您可以使用 SecKeyGetCSPHandle 和 <从 SecKeyRef 获取 CSSM_CSP_HANDLECSSM_KEY分别是代码>SecKeyGetCSSMKey。

要了解有关 CDSA 的更多信息,可以从 Open Group 获取完整规范(免费,但需要注册):

https://www2.opengroup.org/ogsys/jsp/publications/PublicationDetails.jsp?publicationid=11287

祝你好运!

  1. 如果私钥被创建为可导出的,您可以将其以不受保护的格式导出并直接使用 openssl。这会将原始密钥数据直接放入应用程序的地址空间中,因此它违背了钥匙串的主要目的之一。不要这样做。

  2. 最后,您可以使用私有函数。 Mac OS 10.6 和 10.7 包含但不公开声明 SecKeyEncryptSecKeyDecrypt,其参数与 iOS 上相同。快速而肮脏的解决方案是简单地声明和使用它们(弱链接,带有通常的警告)。在您计划分发给其他人的代码中,这可能是一个坏主意。

The Security Framework APIs change rather frequently between Mac OS releases. The best approach depends on what version you target:

  1. If your code only needs to run on 10.7 and above, you can use Security Transforms, a new high-level public API for cryptography transformations. The Security Transforms Programming Guide has useful (and simple!) example code:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecTransformPG/SecurityTransformsBasics/SecurityTransformsBasics.html

You'll want to create a transform using SecEncryptTransformCreate or SecDecryptTransformCreate, set its input using SecTransformSetAttribute and execute it with SecTransformExecute.

  1. If you need to support Mac OS 10.6 or below, you must use the low-level and rather scary CDSA APIs. CryptoSample's cdsaEncrypt is a concise example.

https://developer.apple.com/library/archive/samplecode/CryptoSample/Listings/libCdsaCrypt_libCdsaCrypt_cpp.html

You can get a CSSM_CSP_HANDLE and a CSSM_KEY from a SecKeyRef by using SecKeyGetCSPHandle and SecKeyGetCSSMKey, respectively.

To learn more about CDSA, the full specification is available from the Open Group (free, but requires registration):

https://www2.opengroup.org/ogsys/jsp/publications/PublicationDetails.jsp?publicationid=11287

Good luck!

  1. If the private key was created exportable, you can export it in an unprotected format and use openssl directly. This puts the raw key data directly in the address space of your application, so it defeats one of the primary purposes of the Keychain. Don't do this.

  2. Finally, you can mess around with private functions. Mac OS 10.6 and 10.7 include, but do not publicly declare, SecKeyEncrypt and SecKeyDecrypt, with the same arguments as on iOS. The quick'n'dirty solution is to simply declare and use them (weakly linked, with the usual caveats). This is probably a bad idea to do in code that you plan to distribute to others.

不乱于心 2024-10-08 15:27:04

有一个使用公钥解密数据的实现: https://github.com/karstenBriksoft/CSSMPublicKeyDecrypt
Security.framework 没有用于此类功能的公共 API,这就是为什么 CSSM 需要直接使用,即使它被标记为已弃用。
要使用公钥加密,只需使用 SecEncryptTransformCreate,但对于公钥解密,您需要使用 CSSMPublicKeyDecrypt 类。

There's an implementation of decrypting data using the Public-Key at: https://github.com/karstenBriksoft/CSSMPublicKeyDecrypt.
The Security.framework does not have a public API for that kind of functionality, which is why CSSM needs to be use directly even though its marked as deprecated.
To encrypt with the public key, simply use the SecEncryptTransformCreate, but for public-key decryption you need to use the CSSMPublicKeyDecrypt class.

一世旳自豪 2024-10-08 15:27:04

Mac OS X 在 libcrypto 中包含 OpenSSL。 CommonCrypto 框架似乎源自 OpenSSL 的前身 SSLeay。

Mac OS X contains OpenSSL in libcrypto. The CommonCrypto framework seems to be derived from SSLeay, the precursor of OpenSSL.

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