I need to encrypt the string and have .CER (x.509) in the Resources folder of my xCode project. The last two days I spent to imagine how, but failed, so it's time to ask.
Apple's documentation is very difficult to read ... and I see that this structure is probably the most difficult to understand ... neither samples helped.
So, I tried using the following code: obviously, it does not work:
On my Mac, I used OPENSSL, but did not find a way to recreate OPENSSL commands in the SDK. So, I'm very confused ... anyone ???
Many thanks:)
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);
}
Fabio source
share