iPhone上的RSA加密

AOO*_*AOO 1 iphone cryptography rsa keychain cryptoapi

根据http://forums.macrumors.com/showthread.php?t=551476上的讨论,下面的代码可用于RSA加密.密钥("public")的数据类型是SecKeyRef.但是,我不会使用钥匙串,因为我只对密钥是公开的加密感兴趣并且不是秘密.那么甚至可以使用加密API吗?我目前的想法是仅使用我的公钥构造SecKeyRef结构并使用API​​.但是我不知道如何声明结构.有人知道吗?你认为我的方法会起作用吗?

uint8_t *pPlainText = (uint8_t*) "This is a test";
uint8_t aCipherText[1024];
size_t iCipherLength = 1024;

status = SecKeyEncrypt(public,
                       kSecPaddingNone,
                       pPlainText,
                       strlen((char*) pPlainText ) + 1,
                       aCipherText,
                       &iCipherLength);
Run Code Online (Sandbox Code Playgroud)

Dav*_*har 5

您可能希望在Apple Developer论坛上查看此主题,并查看"CryptoExercise"示例代码.

简而言之,建议您将公钥作为DER编码的X.509证书进行分发,因为iPhone具有处理该格式的良好工具.您将使用SecCertificateCreateWithData读取DER编码的证书,然后使用SecTrustCopyPublicKey来获取SecKeyRef.