首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将NSData转换为SecKeyRef

将NSData转换为SecKeyRef
EN

Stack Overflow用户
提问于 2013-11-07 10:49:42
回答 1查看 2.6K关注 0票数 3

我用SecKeyGeneratePair生成了私钥和公钥,我用

size_t keySize = SecKeyGetBlockSize(publicKey); NSData *keyData = NSData dataWithBytes:publicKey长度:keySize;

但不幸的是,我无法在keyData类型中重新转换SecKeyRef

下面是一个问题:如何以NSData类型重新转换SecKeyRef类型?

这是密码!

代码语言:javascript
复制
    //
    //  RSA.m
    //  example
    //
    //  Created by Ferdinando Picone on 07/11/13.
    //  Copyright (c) 2013 Ferdinando Picone. All rights reserved.
    //

    #import "RSA.h"
    #import <Security/Security.h>
    #import "NSData+Base64.h"

    static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0";
    static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0";
    
    SecKeyRef publicKey = NULL;
    SecKeyRef privateKey = NULL;

    @implementation RSA

    -(void)start{
RSA *one = [[RSA alloc]init];


// Generate public and private key
[one generateKeyPairPlease];
NSLog(@" %@", publicKey);
NSLog(@" %@", privateKey);

//Convert public key in NSData Type
size_t  keySize = SecKeyGetBlockSize(publicKey);
NSData  *keyData = [NSData dataWithBytes:publicKey length:keySize];
NSLog (@" %@", keyData);

//Convert public key in NSString Type
NSString *keyStringB64 =[keyData base64EncodedString];


//Reconvert NSString in a NSData Type (newKeyData)
NSData *newKeyData = [NSData dataFromBase64String:keyStringB64];

NSLog(@" %@", newKeyData);

// In the debug you can see that keyData==newKeyData

// NOW I NEED TO RECONVERT newKeyData in a SecKeyRef newPublicKey
SecKeyRef newPublicKey=NULL;

    }

    -(void) generateKeyPairPlease{

OSStatus status = noErr;
NSMutableDictionary *privateKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *publicKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *keyPairAttr = [[NSMutableDictionary alloc] init];

NSData * publicTag = [NSData dataWithBytes:publicKeyIdentifier
                                    length:strlen((const char *)publicKeyIdentifier)];
NSData * privateTag = [NSData dataWithBytes:privateKeyIdentifier
                                     length:strlen((const char *)privateKeyIdentifier)];


[keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA
                forKey:(__bridge id)kSecAttrKeyType];
[keyPairAttr setObject:[NSNumber numberWithInt:1024]
                forKey:(__bridge id)kSecAttrKeySizeInBits];

[privateKeyAttr setObject:[NSNumber numberWithBool:YES]
                   forKey:(__bridge id)kSecAttrIsPermanent];
[privateKeyAttr setObject:privateTag
                   forKey:(__bridge id)kSecAttrApplicationTag];

[publicKeyAttr setObject:[NSNumber numberWithBool:YES]
                  forKey:(__bridge id)kSecAttrIsPermanent];
[publicKeyAttr setObject:publicTag
                  forKey:(__bridge id)kSecAttrApplicationTag];

[keyPairAttr setObject:privateKeyAttr
                forKey:(__bridge id)kSecPrivateKeyAttrs];
[keyPairAttr setObject:publicKeyAttr
                forKey:(__bridge id)kSecPublicKeyAttrs];

status = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr,
                            &publicKey, &privateKey);


    }


    @end
EN

回答 1

Stack Overflow用户

发布于 2017-05-13 19:15:50

您可以使用SecKeyRef 10.10中提供的API从NSData创建iOS

代码语言:javascript
复制
- (SecKeyRef)secKeyRefFromFrivateKeyData:(NSData *)privateKeyData
{
    NSDictionary *attributes = @{(__bridge NSString*)kSecAttrKeyType : (__bridge NSString*)kSecAttrKeyTypeRSA,
                                 (__bridge NSString*)kSecAttrKeyClass : (__bridge NSString*)kSecAttrKeyClassPrivate};
    return SecKeyCreateWithData((__bridge CFDataRef)privateKeyData, (__bridge CFDictionaryRef)attributes, NULL);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19833977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档