我正在尝试在我的 Mac OS X 应用程序中生成 RSA 密钥,我使用以下代码:
CFStringRef privateTag = (CFStringRef)@"com.example.privatekey";
CFStringRef publicTag = (CFStringRef)@"com.example.publickey";
int bits = 1024;
CFMutableDictionaryRef publicAttr = CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(publicAttr, kSecAttrIsPermanent, kCFBooleanTrue);
CFDictionaryAddValue(publicAttr, kSecAttrApplicationTag, publicTag);
CFMutableDictionaryRef privateAttr = CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(privateAttr, kSecAttrIsPermanent, kCFBooleanTrue);
CFDictionaryAddValue(privateAttr, kSecAttrApplicationTag, publicTag);
CFMutableDictionaryRef keyPairAttr = CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(keyPairAttr, kSecAttrKeyType, kSecAttrKeyTypeRSA);
CFDictionaryAddValue(keyPairAttr, kSecAttrKeySizeInBits, CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &bits));
CFDictionaryAddValue(keyPairAttr, kSecPublicKeyAttrs, privateAttr);
CFDictionaryAddValue(keyPairAttr, kSecPrivateKeyAttrs, publicAttr);
status = SecKeyGeneratePair((CFDictionaryRef)keyPairAttr, &publicKey, &privateKey);
if (status != noErr) {
NSLog(@"something went wrong %d", (int)status);
}else {
NSLog(@"New key");
}
如果我尝试构建 kSecPublicKeyAttrs 是一个未声明的标识符,我不知道为什么。有人可以帮忙吗?
问候,菲利普