我试图通过示例代码询问有关此主题的后续问题,该示例代码说明了如何生成对我不起作用的 rsa 密钥,但由于某种原因它被版主删除了。因此,我将尝试再次发布一个新问题。
我对这个答案的问题是 Xcode 告诉我没有声明“kSecPrivateKeyAttrs”和“kSecPublicKeyAttrs”标识符。苹果开发者文档中提到了这些标识符,但它们似乎不存在于 Secure 框架中。
我正在使用 Xcode 4.5 和 OS X SDK 10.8。
感谢我能得到的任何帮助,我是一个相当新的 att 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