SecKeyGeneratePair 返回 errSecUnimplemented

2024-01-03

我试图在我的 iOS 应用程序中实现 RSA 加密算法,但是当我尝试生成公钥和私钥对时,该函数向我返回 errSecUnimplemented 错误。我正在使用 5.1 SDK,目前目标是 5.1。

我可以不使用此功能吗?还是我在尝试生成该对时设置了错误?

这是我的密钥生成代码:

SecKeyRef publicKey, privateKey;
CFDictionaryRef parameters;
const void* keys[] = {kSecAttrKeyType, kSecAttrKeyTypeRSA};
int keySize = 1024;
const void *values[] = {kSecAttrKeySizeInBits, &keySize};

parameters = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 2, NULL, NULL);
OSStatus ret = SecKeyGeneratePair(parameters, &publicKey, &privateKey);
if ( ret == errSecSuccess )
{
    NSLog(@"Key success!");
}
else 
{
    NSLog(@"Key Failure! %li", ret);
}

我已经对其进行了修改,以便为您完成解决方案。 1) 您需要使用 CFNumberRef 而不是指向 int 的指针作为数值。 2)值必须是值,键必须是键——您在每个“键”和“值”中混合了键和值。

SInt32 iKeySize = 1024;
CFNumberRef keySize = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &iKeySize);
const void* values[] = { kSecAttrKeyTypeRSA, keySize };
const void* keys[] = { kSecAttrKeyType, kSecAttrKeySizeInBits };
CFDictionaryRef parameters = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 2, NULL, NULL);

SecKeyRef publicKey, privateKey;
OSStatus ret = SecKeyGeneratePair(parameters, &publicKey, &privateKey);
if ( ret == errSecSuccess )
    NSLog(@"Key success!");
else 
    NSLog(@"Key Failure! %li", ret);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SecKeyGeneratePair 返回 errSecUnimplemented 的相关文章

随机推荐