IPhone encryption with certificate

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


        }
+3
source share
1 answer

I tried the updated code with the addition of two lines at the end:

NSData *cipherData = [NSData dataWithBytes:aCipherText length:iCipherLength];
NSLog(@"(%d) %@", status, cipherData);

This works great:

2011-02-17 22:24:04.204 Untitled[45121:207] (0) <87a2eb07 25ab693a 7fe88329 974b6820
843c5c33 8c5d4606 aecea682 0176e4cb 10482c9b fd2e2242 1c77d349 d3037e91 8d704783
f2e04c82 ef273815 bdb6aa73 f8646542 243f3e12 518147ba 53636441 fd9399d3 b198ed6a
615d51d1 4105fb75 27180f0d 09835551 5162e156 33dedf39 a87e17f8 16881990 c5e57a38
7cd7ec63>

, , , . , importing-an-ssl-cert-under-the-iphone-sdk . , , , .

, ( OSStatus, ). ?


SecTrustEvaluate() SecTrustRef SecTrustCopyPublicKey(). SecTrustCopyPublicKey(), .


, :

, :

http://greghaygood.com/2009/01/17/asymmetric-encryption-with-the-iphone-sdk-and-securityframework

SSL iPhone SDK

, OpenSSL Mac, OpenSSL iPhone. , :

http://www.therareair.com/2009/01/01/tutorial-how-to-compile-openssl-for-the-iphone/

+2

Source: https://habr.com/ru/post/1792577/


All Articles