android - iOS 5.0 : generated x509 rsa public key of size 2048 is 270 bytes instead of 294 bytes. Why? -


i developing sdk ios 5 @ work, , communicating device via socket interface. device requires sent base64 encoded rsa x509 public key of size 2048.

i generate key pair following code:

osstatus status = noerr; nsmutabledictionary *privatekeyattr = [[nsmutabledictionary alloc] init]; nsmutabledictionary *publickeyattr= [[nsmutabledictionary alloc] init]; nsmutabledictionary *keypairattr = [[nsmutabledictionary alloc] init];  nsdata * publictag = [nsdata datawithbytes:publickeyidentifier                                     length:strlen((const char *)publickeyidentifier)]; nsdata * privatetag = [nsdata datawithbytes:privatekeyidentifier                                      length:strlen((const char *)privatekeyidentifier)];  seckeyref publickey = null; seckeyref privatekey = null;  [keypairattr setobject:(__bridge id)ksecattrkeytypersa                 forkey:(__bridge id)ksecattrkeytype]; [keypairattr setobject:[nsnumber numberwithint:2048]                 forkey:(__bridge id)ksecattrkeysizeinbits];  [privatekeyattr setobject:[nsnumber numberwithbool:yes]                    forkey:(__bridge id)ksecattrispermanent]; [privatekeyattr setobject:privatetag                    forkey:(__bridge id)ksecattrapplicationtag];  [publickeyattr setobject:[nsnumber numberwithbool:yes]                   forkey:(__bridge id)ksecattrispermanent]; [publickeyattr setobject:publictag                   forkey:(__bridge id)ksecattrapplicationtag];  [keypairattr setobject:privatekeyattr                 forkey:(__bridge id)ksecprivatekeyattrs]; [keypairattr setobject:publickeyattr                 forkey:(__bridge id)ksecpublickeyattrs];  status = seckeygeneratepair((__bridge cfdictionaryref)keypairattr,                             &_publickey, &_privatekey); 

i use following code obtain raw data public key:

nsdata* publictag = [[nsdata alloc] initwithbytes:publickeyidentifier length:sizeof(publickeyidentifier)];  osstatus sanitycheck = noerr; nsdata* publickeybits = nil;  nsmutabledictionary* querypublickey = [[nsmutabledictionary alloc] init]; [querypublickey setobject:(__bridge id)ksecclasskey forkey:(__bridge id)ksecclass]; [querypublickey setobject:publictag forkey:(__bridge id)ksecattrapplicationtag]; [querypublickey setobject:(__bridge id)ksecattrkeytypersa forkey:(__bridge id)ksecattrkeytype];  // temporarily add key keychain, return data: nsmutabledictionary* attributes = [querypublickey mutablecopy]; [attributes setobject:(__bridge id)key forkey:(__bridge id)ksecvalueref]; [attributes setobject:@yes forkey:(__bridge id)ksecreturndata]; cftyperef result; sanitycheck = secitemadd((__bridge cfdictionaryref)attributes, &result); if (sanitycheck == errsecsuccess) {     publickeybits = cfbridgingrelease(result);      // remove keychain again:     (void)secitemdelete((__bridge cfdictionaryref)querypublickey); } return publickeybits; 

the above code yields nsdata of 270 bytes long public key; base64 encode data , send device, rejected.

my colleague @ work has completed implementation of same functionality android, , generates key pair follows:

    keypairgenerator kpg = keypairgenerator.getinstance("rsa");     kpg.initialize(2048);     keypair key = kpg.generatekeypair(); 

and uses:

    key.getpublic().getencoded()  

to access public key data, 294 bytes long, , device accepts.

also, if take raw bytes of generated public key, , use base64 encode , send, accepted device.

what difference here? why key 294 bytes, while mine 270? , how can fix this? appreciated.

edit

i have found https://crypto.stackexchange.com/questions/14491/why-is-a-2048-bit-public-rsa-key-represented-by-540-hexadecimal-characters-in-x, states:

note not count encoding says "this rsa public key"; takes additional 24 bytes (including overhead).

this sounds need, although don't know how include information.

anyone?

here's answer: http://blog.wingsofhermes.org/?p=42

"first off, when export key iphone keychain, it’s exported in cut down format – public key , exponent without of other asn.1 stuff you’d expect in encoded public key."

i thought this; i've been smashing head against monitor day..

bloody apple.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -