无法确定如何发布在iPhon上生成的公钥

2024-09-30 14:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用iPhone上的commoncrypto库来创建一对RSA密钥,并尝试将公钥发送到服务器(Python),以便使用它来验证从电话发送的签名。在

我使用的是CommonCrypto示例中的精确代码,方法是getPublicKeyBits(),如下所示:

`-(NSData)getPublicKeyBits{ OSStatus sanityCheck=noErr; NSDatapublicKeyBits=nil; NSData*publicTag=[[NSData alloc]初始化WithBytes:publicKeyIdentifier长度:sizeof(publicKeyIdentifier)]; CFDataRef cfresult=空

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

// Set the public key query dictionary.
[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];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnData];

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


if (sanityCheck != noErr)
{
    publicKeyBits = nil;
}
else 
{
    publicKeyBits = (__bridge_transfer NSData *)cfresult;
}

return publicKeyBits;

}`

问题是我不知道如何,确切地说,密钥是如何存储的,或者如何传递它。我使用getPublicKeyBits()将其放入NSData结构中,并导入了一个库来将其编码在base64中。我得到了一个密钥(SHA1,我想移到SHA256,但这是第二个有效的方法),而且base64版本看起来像我在这里和其他人解决RSA问题时发现的其他密钥。在

我一直在尝试使用Python中的M2Crypto库,当我试图验证我得到的是错误"RSA Error: No Start Line"。下面是我用来接收公钥的代码:

^{pr2}$

我用来检查签名的代码:

def validate(sessionKey, sig, pem):
    bio = BIO.MemoryBuffer(pem.encode('ascii'))
    rsa = RSA.load_pub_key_bio(bio)
    pubkey = EVP.PKey()
    pubkey.assign_rsa(rsa)

    pubkey.reset_context(md='sha1')
    pubkey.verify_init()
    pubkey.verify_update(sessionKey)

    return pubkey.verify_final(sig)

我真的很难说我做错了什么,但我觉得我越来越接近了。如果我的整个方法不是你所希望的那样,我很乐意听到任何其他方法来在电话上生成RSA密钥,将公钥发布到服务器,然后在该服务器上验证来自电话的签名。在

谢谢!在


Tags: 方法代码服务器id密钥rsabridge电话
2条回答

M2Crypto的底层实现是OpenSSL。当您导出纯公钥时,OpenSSL不会导入它,但只有当它“嵌入”在可能的自签名证书中时。了解如何使用公共加密或安全框架创建自签名证书,嵌入公钥并用私钥签名,提取其数据并将其发送到服务器:它应该可以工作。在

另请参见openssl_pkey_get_public not open public key, "no start line" error(第二个答案)

您可以在Apple CryptoExercise代码中使用SecKeyWrapper中的getPublicKeyBits方法

https://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_h.html

这将得到DER编码的二进制数据。然后使用以下方法将其加载到Python中的M2Crypto中:(基本上,只需base64并调用一个方法)

https://stackoverflow.com/a/5765576/584616

我有这个getPublicKeyBits方法的启用ARC的版本,但是我还没有来得及发布它。在

更新-重读您的问题,答案就在这里:

How to find out the modulus and exponent of RSA Public Key on iPhone/Objective C

您可以使用它来获得作为NSData的模数和指数,您应该能够轻松地将其转换为base64或您选择的表示形式。在

相关问题 更多 >