Mac 上公钥加密/解密的示例代码?
在哪里可以找到一些在 Mac OS X 上进行公钥加密和解密的简单示例代码?我很沮丧,Apple 的“证书、密钥和信任服务编程指南”展示了如何在 iOS 上执行此操作,但所需的 API(SecKeyEncrypt
、SecKeyDecrypt
)是显然在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
安全框架 API 在 Mac OS 版本之间变化相当频繁。最佳方法取决于您的目标版本:
https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecTransformPG/SecurityTransformsBasics/SecurityTransformsBasics.html
您需要创建一个使用
SecEncryptTransformCreate
或SecDecryptTransformCreate
进行转换,使用SecTransformSetAttribute
设置其输入,并使用SecTransformExecute
执行它。CryptoSample
的cdsaEncrypt
是一个简洁的示例。https://developer.apple.com/library/archive/ Samplecode/CryptoSample/Listings/libCdsaCrypt_libCdsaCrypt_cpp.html
您可以使用
SecKeyGetCSPHandle
和 <从 SecKeyRef 获取CSSM_CSP_HANDLE
和CSSM_KEY
分别是代码>SecKeyGetCSSMKey。要了解有关 CDSA 的更多信息,可以从 Open Group 获取完整规范(免费,但需要注册):
https://www2.opengroup.org/ogsys/jsp/publications/PublicationDetails.jsp?publicationid=11287
祝你好运!
如果私钥被创建为可导出的,您可以将其以不受保护的格式导出并直接使用 openssl。这会将原始密钥数据直接放入应用程序的地址空间中,因此它违背了钥匙串的主要目的之一。不要这样做。
最后,您可以使用私有函数。 Mac OS 10.6 和 10.7 包含但不公开声明
SecKeyEncrypt
和SecKeyDecrypt
,其参数与 iOS 上相同。快速而肮脏的解决方案是简单地声明和使用它们(弱链接,带有通常的警告)。在您计划分发给其他人的代码中,这可能是一个坏主意。The Security Framework APIs change rather frequently between Mac OS releases. The best approach depends on what version you target:
https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecTransformPG/SecurityTransformsBasics/SecurityTransformsBasics.html
You'll want to create a transform using
SecEncryptTransformCreate
orSecDecryptTransformCreate
, set its input usingSecTransformSetAttribute
and execute it withSecTransformExecute
.CryptoSample
'scdsaEncrypt
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 aCSSM_KEY
from a SecKeyRef by usingSecKeyGetCSPHandle
andSecKeyGetCSSMKey
, 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!
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.
Finally, you can mess around with private functions. Mac OS 10.6 and 10.7 include, but do not publicly declare,
SecKeyEncrypt
andSecKeyDecrypt
, 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.有一个使用公钥解密数据的实现: 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.
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.