RSA Encryption on iPhone

寵の児 提交于 2019-12-21 06:56:01

问题


According the discussion on http://forums.macrumors.com/showthread.php?t=551476 the code seen below would do for RSA encryption. The datatype of the key ("public") is SecKeyRef. I will not be using the keychain, though, as I'm only interested in encryption where the key is public and is no secret. Is it even possible to use the crypto API then? My current idea is to construct a SecKeyRef struct from my public key only and use the API. I don't know how the struct is declared, though. Does anyone know? Do you think my approach will work?

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

回答1:


You probably want to look at this thread on the Apple Developer Forums, and also check out the "CryptoExercise" sample code.

In short, the recommendation is that you distribute your public key as a DER-encoded X.509 certificate, because the iPhone has good tools for working with that format. You would use SecCertificateCreateWithData to read in the DER-encoded certificate, then SecTrustCopyPublicKey to get the SecKeyRef.



来源:https://stackoverflow.com/questions/2418337/rsa-encryption-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!