OpenSSL publicly written objective-c encryption

I have compiled and created openssl in my iOS project,

but struggling with objective-c with writing equivalent code to this command line:

openssl rsautl -encrypt -inkey publicKey.pem -pubin -in textfile.txt -out encrypted.bin

      

How can I achieve this?

+3


source to share


2 answers


So I solved my problem, here is the function of my question that encrypts the NSString:

I changed the code from the code in this question: Send RSA public key to iphone and use it for encryption



(Also at the end of my code I am using QSUtilities to base64 encode the message)

#pragma mark Encryption using OpenSSL
+ (NSString *)EncryptMessage:(NSString *)message {
NSString *path = [[NSBundle mainBundle] pathForResource:@"pubkey" ofType:@"pem"];
FILE *pubkey = fopen([path cStringUsingEncoding:1], "r");
if (pubkey == NULL) {
    NSLog(@"duh: %@", [path stringByAppendingString:@" not found"]);
    return NULL;
}

RSA *rsa = PEM_read_RSA_PUBKEY(pubkey, NULL, NULL, NULL);
if (rsa == NULL) {
    NSLog(@"Error reading RSA public key.");
    return NULL;
}

const char *msgInChar = [message UTF8String];
unsigned char *encrypted = (unsigned char *) malloc(512); //I'm not so sure about this size
int bufferSize = RSA_public_encrypt(strlen(msgInChar), (unsigned char *)msgInChar, encrypted, rsa, RSA_PKCS1_PADDING);
if (bufferSize == -1) {
    NSLog(@"Encryption failed");
    return NULL;
}

NSData *data = [NSData dataWithBytes:(const void *)encrypted length:512]; //I'm not so sure about this length
NSString *result = [QSStrings encodeBase64WithData:data];

free(rsa);
fclose(pubkey);
free(encrypted);

return result;

      

+2


source


Hi i had the same problem, finally i found what i was looking for. What I need, as CodeInChaos, is my self-signed certificate. My code works fine with it. For this I use this command:

openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

      

I found this post very helpful:

http://blog.iamzsx.me/show.html?id=155002



Answers many questions. Not in English, but google translated so this is not a big problem.

I made this little function to encrypt data with the code I found and my own. I have a public key in my package and I am returning a base64 encoded NSDaa message to send to the server:

+ (NSString *)encryptWithPublicKeyMessage:(NSString *) message
{
NSLog(@"encrypting...");
NSData *inputData = [message dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [inputData bytes];
int length = [inputData length];
uint8_t *plainText = malloc(length);
memcpy(plainText, bytes, length);

/* Open and parse the cert*/
NSData *certData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"]];
SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certData);
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
OSStatus status = SecTrustCreateWithCertificates(cert, policy, &trust);

/* You can ignore the SecTrustResultType, but you have to run SecTrustEvaluate
 * before you can get the public key */
SecTrustResultType trustResult;
if (status == noErr) {
    status = SecTrustEvaluate(trust, &trustResult);
}

/* Now grab the public key from the cert */
SecKeyRef publicKey = SecTrustCopyPublicKey(trust);

/* allocate a buffer to hold the cipher text */
size_t cipherBufferSize;
uint8_t *cipherBuffer; 
cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);

/* encrypt!! */
SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plainText, length, cipherBuffer, &cipherBufferSize);


 NSData *d = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];

/* Free the Security Framework Five! */
CFRelease(cert);
CFRelease(policy);
CFRelease(trust);
CFRelease(publicKey);
free(cipherBuffer);
NSLog(@"encrypted");
return [d encodeBase64ForData];
}

      

I hope this helps me, it takes me a while to find the correct code

+5


source







All Articles