[英]Cannot figure out how to publish public key generated on iPhone
我正在使用iPhone上的commoncrypto
库来创建一对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;
}`
问题是我不知道密钥是如何存储的,或者如何传递密钥。 我使用getPublicKeyBits()
在NSData
结构中获取它,并且我导入了一个库以在base64
对其进行编码。 我得到一个密钥(SHA1,我想转移到SHA256,但这是继续使用它), base64
版本看起来像我在这里和其他人解决RSA问题的其他键。
我一直在尝试使用Python中的M2Crypto
库,当我尝试验证我收到错误"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)
我用来检查签名的代码:
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密钥,将公钥发布到服务器,然后验证该服务器上的手机签名。
谢谢!
您可以在Apple CryptoExercise代码中使用SecKeyWrapper中的getPublicKeyBits方法
这将为您提供DER编码的二进制数据。 然后使用以下方法将其加载到Python中的M2Crypto中:(基本上,只需要base64它并调用一个方法。)
https://stackoverflow.com/a/5765576/584616
我有一个这个getPublicKeyBits方法的ARC启用版本,但我还没有发布它。
更新 - 重新阅读您的问题,答案实际上在这里:
如何在iPhone / Objective C上找出RSA公钥的模数和指数
您可以使用它来获得NSData的模数和指数,您应该能够轻松地将其转换为base64或您选择的表示形式。
M2Crypto的底层实现是OpenSSL。 OpenSSL在导出时不会导入纯公钥,但只有在可能是自签名证书中“嵌入”时才会导入。 查看如何使用Common Crypto或Security框架创建自签名证书,嵌入您的公钥并使用您的私钥对其进行签名,提取其数据并将其发送到您的服务器:它应该可以正常工作。
另请参见openssl_pkey_get_public未打开公钥,“无起始行”错误 (第二个答案)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.