iPhone encryption with certificate
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:

1) I don't know ho to read the encrypted string in a log;
2) the padding would be PKCS#1, but I have no idea where and how to put this info;
3) the CER file is in the Resource folder and is x.509;

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

    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	NSString *thePath = [[NSBundle mainBundle]
    						 pathForResource:@"myCert" ofType:@"cer"];
       dataCer = [[NSData alloc] initWithContentsOfFile:thePath];
    	
    	SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef) dataCer); 
    	SecPolicyRef policy;
    	SecTrustRef trust;
    	SecTrustCreateWithCertificates(cert, policy, &trust);
    	SecKeyRef pub_key_leaf = SecTrustCopyPublicKey(trust);
    	
    	
    		
    		uint8_t *pPlainText = (uint8_t*)"This is a test";
    		uint8_t aCipherText[1024];
    		size_t iCipherLength = 1024;
    		OSStatus status = SecKeyEncrypt(pub_key_leaf,
    										kSecPaddingNone,
    										pPlainText,
    										strlen( (char*)pPlainText ) + 1,
    										aCipherText,
    										&iCipherLength);
    	
    	NSLog(@"%@",status);
    
    	
    	}
iPhone encryption with certificate
===

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:

1) I don't know ho to read the encrypted string in a log;
2) the padding would be PKCS#1, but I have no idea where and how to put this info;
3) the CER file is in the Resource folder and is x.509;

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

    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	NSString *thePath = [[NSBundle mainBundle]
    						 pathForResource:@"myCert" ofType:@"cer"];
       dataCer = [[NSData alloc] initWithContentsOfFile:thePath];
    	
    	SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef) dataCer); 
    	SecPolicyRef policy;
    	SecTrustRef trust;
    	SecTrustCreateWithCertificates(cert, policy, &trust);
    	SecKeyRef pub_key_leaf = SecTrustCopyPublicKey(trust);
    	
    	
    		
    		uint8_t *pPlainText = (uint8_t*)"This is a test";
    		uint8_t aCipherText[1024];
    		size_t iCipherLength = 1024;
    		OSStatus status = SecKeyEncrypt(pub_key_leaf,
    										kSecPaddingNone,
    										pPlainText,
    										strlen( (char*)pPlainText ) + 1,
    										aCipherText,
    										&iCipherLength);
    	
    	NSLog(@"%@",status);
    
    	
    	}