我试图在我的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);
}
答案 0 :(得分: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);
答案 1 :(得分:0)
不应该是:
const void* keys[] = {kSecAttrKeyType, kSecAttrKeySizeInBits};
int keySize = 1024;
const void *values[] = {kSecAttrKeyTypeRSA, &keySize};
即,键应该是dict的键和值的值,目前你在键中有一个(键,值)对,在值中有一个。
答案 2 :(得分:0)
使用kCFAllocatorSystemDefault
代替kCFAllocatorDefault
返回errSecSuccess
。