RSA Encryption on iPhone

2019-04-03 01:24发布

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条回答
劫难
2楼-- · 2019-04-03 02:11

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.

查看更多
登录 后发表回答