繁体   English   中英

iOS 5.0:生成的大小为2048的x509 rsa公钥为270字节,而不是294字节。 为什么?

[英]iOS 5.0 : generated x509 rsa public key of size 2048 is 270 bytes instead of 294 bytes. Why?

我正在工作中为iOS 5开发SDK,并且正在通过套接字接口与设备通信。 此设备需要发送大小为2048的base64编码的rsa x509公钥。

我用以下代码生成密钥对:

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

然后,我使用以下代码来获取公钥的原始数据:

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 to the Keychain, return as 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 from Keychain again:
    (void)SecItemDelete((__bridge CFDictionaryRef)queryPublicKey);
}
return publicKeyBits;

上面的代码为公共密钥产生了270个字节长的 NSData。 我base64编码此数据并将其发送到设备,但被拒绝。

我的同事在工作中已经完成了针对android的相同功能的实现,并且他生成了如下的密钥对:

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    KeyPair key = kpg.generateKeyPair();

他使用:

    key.getPublic().getEncoded() 

访问设备可以接受的公共密钥数据,该数据的长度为294个字节

另外,如果我获取他生成的公共密钥的原始字节,并使用我的base64编码并发送,则设备也将接受它。

这里有什么区别? 为什么我的密钥是294个字节,而我的是270个字节? 我该如何解决呢? 任何帮助将非常感激。

编辑

我刚刚发现https://crypto.stackexchange.com/questions/14491/why-is-a-2048-bit-public-rsa-key-represented-by-540-hexadecimal-characters-in-x指出:

注意,这不包括表示“这是RSA公钥”的编码。 占用额外的24个字节(包括开销)。

这听起来像我需要的,尽管我不知道如何包括这些信息。

任何人?

答案是: http : //blog.wingsofhermes.org/?p=42

“首先,当您从iPhone钥匙串中导出密钥时,它以缩减格式导出-仅公开密钥和指数,而没有您期望在完全编码的公开密钥中的任何其他ASN.1内容。”

我以为是这样。 我整天在监视器上砸头。

血腥的苹果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM