无法弄清楚如何发布在iPhone上生成的公钥

bre*_*nen 6 python security iphone rsa objective-c

我正在使用commoncryptoiPhone上的库创建一对RSA密钥,我正在尝试将公钥发送到服务器(Python),以便我可以使用它来验证从手机发送的签名.

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

` - (NSData )getPublicKeyBits {OSStatus sanityCheck = noErr; NSData publicKeyBits = nil; NSData*publicTag = [[NSData alloc] initWithBytes:publicKeyIdentifier length:sizeof(publicKeyIdentifier)]; CFDataRef cfresult = NULL;

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;
Run Code Online (Sandbox Code Playgroud)

}`

问题是我不知道密钥是如何存储的,或者如何传递密钥.我正在使用它getPublicKeyBits()来获取它在一个NSData结构中,我已经导入了一个库来编码它base64.我得到一个密钥(SHA1,我想转移到SHA256,但这是继续工作的第二个),base64版本看起来像我在这里和其他人解决RSA问题的其他键.

我一直在尝试使用M2CryptoPython中的库,当我尝试验证我收到错误"RSA Error: No Start Line".这是我用来接收公钥的代码:

pubKey = request.form['publickey']

uid = uuid4().hex
while not unique(uid, User):
    uid = uuid.uuid4().hex
user = User(uid, email, secret, pubKey)
Run Code Online (Sandbox Code Playgroud)

我用来检查签名的代码:

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)
Run Code Online (Sandbox Code Playgroud)

我真的很难过我做错了什么,但我觉得我已经接近了.如果我的整个方法不是您的方法,我很乐意听到任何其他方式在手机上生成RSA密钥,将公钥发布到服务器,然后验证该服务器上的手机签名.

谢谢!

StC*_*ero 1

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

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

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

/sf/answers/403590351/

我有这个getPublicKeyBits方法的 ARC 启用版本,但我还没有抽出时间发布它。

更新- 重新阅读您的问题时,答案实际上在这里:

如何在 iPhone/Objective C 上找出 RSA 公钥的模数和指数

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