SecKeyGeneratePair返回errSecUnimplemented

Dan*_*n F 4 security-framework ios

我试图在我的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);
}
Run Code Online (Sandbox Code Playgroud)

NSP*_*mer 9

我已将其修改为只为您完成解决方案.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);
Run Code Online (Sandbox Code Playgroud)