views:

99

answers:

2

I have encrypted file and public key. How can I decrypt it from app without installing certificates?

file public.key looks like "e+ztydr5GG7saZyrIOtSWGQgHlQbuFn1IVlIIggPIWuLUNTOqN0Y..."

Here are some code:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"public" ofType:@"key"];  
NSData* publicKeyData = [NSData dataWithContentsOfFile:filePath];  

NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
[queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
[queryPublicKey setObject:publicKeyData forKey:(id)kSecAttrApplicationTag];
[queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];

OSStatus resultCode = noErr;
SecKeyRef publicKeyReference = NULL;
resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef*)&publicKeyReference);

However resultCode = -25300 (The specified item could not be found in the keychain). What I'm doing wrong? any suggestions?

A: 

file public.key looks like "e+ztydr5GG7saZyrIOtSWGQgHlQbuFn1IVlIIggPIWuLUNTOqN0Y..."

That looks very much like a Base64 encoded string, to my eye. You almost certainly need the raw, unencoded bytes to pass to the security APIs. If you're putting this file in your app's bundle, the easiest thing would probably be to decode it beforehand, and store the raw binary in your app.

Sixten Otto
Are there any API funcs to make conversion? Also I'm worrying about that simulator (I read somewhere) doesn't support certificates or I'm wrong?
Aston
Not in the Cocoa APIs, no, but I'm sure Google will turn up someone's code for it.
Sixten Otto
I used few examples of decoding from: http://www.cocoadev.com/index.pl?BaseSixtyFour and the problem remains the same - error -25300. Also I have found that key: kSecAttrApplicationTag is for private key, however I'm using public one and there's no such key for public, maybe here is the problem? Any suggestions?
Aston
A: 

You can't decrypt a file with a public key, the public key encrypts and the private key decrypts, that is the basic principle of asymmetric algos. Where is the security in an encrypted file if the public key (i.e. the one everyone knows) can decrypt it?

Patrick
This type on encryption calls 'signing'. Security is not in the data, but the data sender!
Aston