Xcode NSData的SecKeyRef

Xcode NSData的SecKeyRef,xcode,rsa,nsdata,Xcode,Rsa,Nsdata,这是我生成新密钥对的代码 //Create RSA Key Pair CFMutableDictionaryRef parameters = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,

这是我生成新密钥对的代码

    //Create RSA Key Pair
CFMutableDictionaryRef parameters = CFDictionaryCreateMutable(
                                                              kCFAllocatorDefault,
                                                              0,
                                                              &kCFTypeDictionaryKeyCallBacks,
                                                              &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(parameters, kSecAttrKeyType, kSecAttrKeyTypeRSA);

int32_t rawnum = 2048;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,
                                 kCFNumberIntType, &rawnum);
CFDictionarySetValue(parameters,
                     kSecAttrKeySizeInBits,
                     num);
publicKey = NULL;
privateKey = NULL;

SecKeyGeneratePair(parameters, &publicKey, &privateKey);
我试着用这种方法获取数据

- (NSData *)getRSAKeyBitsFromKey:(SecKeyRef)givenKey {

    static const uint8_t publicKeyIdentifier[] = "com.company";
    NSData *publicTag = [[NSData alloc] initWithBytes:publicKeyIdentifier length:sizeof(publicKeyIdentifier)];

    OSStatus sanityCheck = noErr;
    NSData * publicKeyBits = nil;

    NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
    [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];

    // Temporarily add key to the Keychain, return as data:
    NSMutableDictionary * attributes = [queryPublicKey mutableCopy];
    [attributes setObject:(__bridge id)givenKey forKey:(__bridge id)kSecValueRef];
    [attributes setObject:@YES forKey:(__bridge id)kSecReturnData];
    CFTypeRef result;
    sanityCheck = SecItemAdd((__bridge CFDictionaryRef) attributes, &result);
    if (sanityCheck == errSecSuccess) {
        publicKeyBits = CFBridgingRelease(result);

        // Remove from Keychain again:
        (void)SecItemDelete((__bridge CFDictionaryRef) queryPublicKey);
    }

    return publicKeyBits;
}
我知道可能我没有设置PublicKeyIdentifier和其他属性(我应该设置吗?)