Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jul 30, 2013, 02:05 AM   #1
macrumors member
Join Date: May 2012
Encryption help


I too plan to implement RSA algorithm (encryption/decryption) using public/private keys but looks like the sample available in apple documents is only applicable for iOS (kSecPrivateKeyAttrs and kSecPublicKeyAttrs are available in iOS only).

Can somebody shed some light for it's equivalent for MAC development (snow leopard onwards).

Thanks & Regards.
mycompuser is offline   0 Reply With Quote
Old Jul 30, 2013, 12:21 PM   #2
macrumors 603
Join Date: Aug 2009
When I google for kSecPrivateKeyAttrs, the first result is this:
First, you're using iOS sample code on OS X. The frameworks are similar, but not identical. But there is no OS X sample code, and the iOS sample code shows up in the OS X doc sets, ...
If that explains a solution that works, then remember that simply searching for one of the unresolved symbols came up with an answer. You should try that approach in the future.
chown33 is offline   0 Reply With Quote
Old Aug 12, 2013, 02:59 AM   #3
Thread Starter
macrumors member
Join Date: May 2012
I did google and do some investigation and raised this thread only as the last resort.

Did try to add the below mentioned code to do away with the error

#define SEC_CONST_DECL(k,v) CFTypeRef k = (CFTypeRef)(CFSTR(v))

SEC_CONST_DECL (kSecPrivateKeyAttrs, "private");
SEC_CONST_DECL (kSecPublicKeyAttrs, "public");
but found that the "SecItemCopyMatching" method for retrieving of public key information returns a sanityCheck value of -25300. Though the method "SecKeyGeneratePair" returns success.

Below is the code.

- (NSData*)publicKeyForTag:(UInt8 *)publicKeyIdentifier
    OSStatus sanityCheck = noErr;
    NSData *publicTag = [NSData dataWithBytes:publicKeyIdentifier
                                       length:strlen((const char *)publicKeyIdentifier)];
    NSData * publicKeyBits = nil;

    NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

    // Set the public key query dictionary.
    [queryPublicKey setObject:(id)kSecClassKey forKey:( id)kSecClass];
    [queryPublicKey setObject:publicTag forKey:( id)kSecAttrApplicationTag];
    [queryPublicKey setObject:( id)kSecAttrKeyTypeRSA forKey:( id)kSecAttrKeyType];
    [queryPublicKey setObject:( id)kSecAttrKeyClassPublic forKey:( id)kSecAttrKeyClass];

    [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:( id)kSecReturnData];

    // Get the key bits.
    CFTypeRef cfRef = &publicKeyBits;
    sanityCheck = SecItemCopyMatching(( CFDictionaryRef)queryPublicKey, (CFTypeRef *)cfRef);

    if (sanityCheck != noErr)
        publicKeyBits = nil;

    return publicKeyBits;
Is there any changes that I need to make to in the above method to access the private and public key information successfully?
mycompuser is offline   0 Reply With Quote
Old Aug 12, 2013, 01:20 PM   #4
Thread Starter
macrumors member
Join Date: May 2012
As an alternative, I've tried to implement RSA asymmetric key encryption using openssl api's though my first preference will still be using the framework for performing RSA encryption/decryption on OSX.

I managed to get a OSX sample project running using publicly available code for encrypt and decrypt an string using RSA. Have attached the project to this thread.

But the problem is that the size of the string that I can encrypt is Max of 128 bytes (1024/8). Read that this is a inherent limitation of the RSA algorithm.

And the way to get around that for large input data is to split the input data into smaller chunks for encryption purpose.

Can somebody guide me on those lines. Any link to code snippet/sample project implementing this will be of great help to me.

Thanks & Regrds.
Attached Files
File Type: zip RSASample.zip (85.4 KB, 20 views)
mycompuser is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: IOS Encryption With Third Party Encryption iamakasmalls Jailbreaks and iOS Hacks 0 Aug 21, 2013 08:35 PM
Encryption Times? doubledee Mac Basics and Help 2 Jul 7, 2013 10:04 PM
Encryption in an App? xArtx App Store Business, Legal and Marketıng 0 Jan 28, 2013 08:15 PM
AFP Encryption - 10.8 vs 10.6 Butler Trumpet Mac Programming 0 Dec 17, 2012 04:19 PM
Encryption Questions micrors4racer OS X 10.8 Mountain Lion 1 Oct 4, 2012 09:34 AM

Forum Jump

All times are GMT -5. The time now is 09:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC