Xcode 目标C安全.h RSA密钥对生成

Xcode 目标C安全.h RSA密钥对生成,xcode,macos,security,rsa,key-pair,Xcode,Macos,Security,Rsa,Key Pair,我试图用示例代码问一个关于如何生成rsa密钥的后续问题,但这对我来说不起作用,但由于某种原因,它被版主删除了。因此,我将再次尝试发布一个新问题 这个答案的问题是Xcode告诉我没有声明“kSecPrivateKeyAttrs”和“kSecPublicKeyAttrs”标识符。苹果开发者文档中提到了这些标识符,但它们似乎不存在于安全框架中 我使用的是Xcode 4.5和OSXSDK10.8 非常感谢您对我的帮助,我是att OC编程新手。 如果我能做到这一点,我还想知道如何将pubkey和priv

我试图用示例代码问一个关于如何生成rsa密钥的后续问题,但这对我来说不起作用,但由于某种原因,它被版主删除了。因此,我将再次尝试发布一个新问题

这个答案的问题是Xcode告诉我没有声明“kSecPrivateKeyAttrs”和“kSecPublicKeyAttrs”标识符。苹果开发者文档中提到了这些标识符,但它们似乎不存在于安全框架中

我使用的是Xcode 4.5和OSXSDK10.8

非常感谢您对我的帮助,我是att OC编程新手。 如果我能做到这一点,我还想知道如何将pubkey和privkey作为NSString或NSData获取

多谢各位

编辑:我仍然有这个问题,肯定有其他人有同样的问题,已经解决了它,可以告诉我正确的方向吗

EDIT2正如我所说,我正在尝试我发布的链接中的代码,但下面是完整的代码:

钥匙对

#import <Security/Security.h>

@interface Keypair
{
    SecKeyRef publicKey;
    SecKeyRef privateKey;
    NSData *publicTag;
    NSData *privateTag;
 }
 - (void)generateKeyPair:(NSUInteger)keySize;
 @end

那之后你能取回钥匙吗? 我拿不回钥匙。就好像他们不在钥匙链上。
调用SecItemCopyMatching返回一个错误,表示找不到密钥。

有点晚,但由于我目前正在处理一个相关问题,我想我可以解释一下


对于iOS,您可以为私钥和公钥指定单独的字典,它们的属性可以不同。但对于Mac,您将所有属性直接放在一个字典中,并将它们应用于两个键。

您的答案已被删除,因为这不是一个论坛。请出示你的密码;您几乎肯定无法导入头文件。我已经发布了我试图使用的完整代码。找不到任何缺少的导入。也许我需要OSX的其他东西?我不明白为什么这会与IOS不同,但如果是,请建议我应该使用除kSec{Public,Private}KeyAttrs之外的其他方法。好的,我只是用@“Public”和@“Private”替换kSecPublicKeyAttrs来实现这一点,因为某些原因,它们只存在于IOS版本的Security.framework中。现在,它只是一种检索公钥以备将来使用的方法。OT:苹果为什么恨我?
#import "Keypair.h"

@implementation Keypair

static const UInt8 publicKeyIdentifier[] = "com.XXXXXXX.publickey\0";
static const UInt8 privateKeyIdentifier[] = "com.XXXXXXX.privatekey\0";

+ (void)generateKeyPair:(NSUInteger)keySize {
    OSStatus sanityCheck = noErr;
    SecKeyRef publicKey = NULL;
    SecKeyRef privateKey = NULL;
    NSData *publicTag;
    NSData *privateTag;

    // Container dictionaries.
    NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init];

    // Set top level dictionary for the keypair.
    [keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
    [keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(__bridge id)kSecAttrKeySizeInBits];
    [keyPairAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];

    // Set the private key dictionary.
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [privateKeyAttr setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set the public key dictionary.
    [publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [publicKeyAttr setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set attributes to top level dictionary.
    [keyPairAttr setObject:privateKeyAttr forKey:(id)kSecPrivateKeyAttrs];
    [keyPairAttr setObject:publicKeyAttr forKey:(id)kSecPublicKeyAttrs];


    // SecKeyGeneratePair returns the SecKeyRefs just for educational purposes.
    sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKey, &privateKey);
    if(sanityCheck == noErr  && publicKey != NULL && privateKey != NULL)
    {
        NSLog(@"Successful");
    }
}

@end