【问题标题】:Objective C Secure.h RSA key-pair generatingObjective C Secure.h RSA 密钥对生成
【发布时间】:2012-09-29 18:56:55
【问题描述】:

我试图在this topic 上提出一个后续问题,其中包含有关如何生成对我不起作用的 rsa 密钥的示例代码,但由于某种原因它被版主删除了。所以我将尝试发布一个新问题。

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

我正在使用 Xcode 4.5 和 OS X SDK 10.8。

感谢我能得到的任何帮助,我是 OC 编程的新手。 如果我让它工作,我还想知道如何将 pubkey 和 privkey 作为 NSString 或 NSData。

谢谢

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

EDIT2 正如我所说,我正在尝试我发布的链接中的代码,但无论如何这里是完整的代码:

密钥对.h

#import <Security/Security.h>

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

密钥对.m

#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

【问题讨论】:

  • 您的回答已被删除,因为这不是论坛。请出示您的代码;你几乎可以肯定没有#import 一个头文件。
  • 我已经发布了我尝试使用的完整代码。找不到任何丢失的#import。也许我需要 OSX 的其他东西?我不明白为什么这与 IOS 不同,但如果是,请建议我应该使用除 kSec{Public,Private}KeyAttrs 之外的其他内容。
  • 好的,所以我通过简单地将 kSecPublicKeyAttrs 替换为 @"public" 和 @"private" 来实现这一点,出于某种原因,它们只存在于 Ios 版本的 Security.framework 中。现在它只是检索公钥以供以后使用的问题。 OT:为什么苹果讨厌我?

标签: xcode macos security rsa key-pair


【解决方案1】:

之后你能找回钥匙吗? 我拿不回钥匙。就像它们不在钥匙链中一样。 调用 SecItemCopyMatching 返回错误,提示找不到密钥。

【讨论】:

    【解决方案2】:

    有点晚了,但由于我目前正在处理一个相关问题,所以我想我有所了解。

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

    【讨论】: