我正在使用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密钥,将公钥发布到服务器,然后验证该服务器上的手机签名
谢谢!
答案 0 :(得分:1)
您可以在Apple CryptoExercise代码中使用SecKeyWrapper中的 getPublicKeyBits 方法
这将为您提供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或您选择的表示形式。
答案 1 :(得分:0)
M2Crypto的底层实现是OpenSSL。 OpenSSL在导出时不会导入纯公钥,但只有在可能是自签名证书中“嵌入”时才会导入。查看如何使用Common Crypto或Security框架创建自签名证书,嵌入公钥并使用私钥对其进行签名,提取数据并将其发送到服务器:它应该可以正常工作。
另见openssl_pkey_get_public not open public key, "no start line" error(第二个答案)