I have to encrypt a string and have a .CER (x.509) in the Resources folder of my xCode project. The last two days I have spent to imagine how, but no success, so it's time to ask.
The documentation of Apple is very difficult to read... and I see this framework is probably the hardest one to understand... neither the samples have helped.
So, I have tried with the following code: obviously it does not work:
On my Mac I used OPENSSL but I found no way to recreate OPENSSL commands in the SDK. So, I'm pretty confused... anyone???
Thanks a lot :)
NSString *certPath = [[NSBundle mainBundle] pathForResource:@"Certificate" ofType:@"cer"];
SecCertificateRef myCertificate = nil;
NSData *certificateData = [[NSData alloc] initWithContentsOfFile:certPath];
myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
SecTrustCreateWithCertificates(myCertificate, myPolicy, &myTrust);
SecKeyRef publicKey = SecTrustCopyPublicKey(myTrust);
uint8_t *pPlainText = (uint8_t*)"This is a test";
uint8_t aCipherText[1024];
size_t iCipherLength = 1024;
OSStatus status = SecKeyEncrypt(publicKey,
kSecPaddingPKCS1,
pPlainText,
strlen( (char*)pPlainText ) + 1,
aCipherText,
&iCipherLength);
}