gpt4 book ai didi

ios - 将 NSData 转换为 SecKeyRef

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:06 27 4
gpt4 key购买 nike

我用 SecKeyGeneratePair 生成了私钥和公钥我已经将 SecKeyRef 转换为 NSData

size_t keySize = SecKeyGetBlockSize(publicKey);

NSData *keyData = [NSData dataWithBytes:publicKey length:keySize];

但不幸的是,我无法将 keyData 重新转换为 SecKeyRef 类型。

问题如下:如何将 NSData 重新转换为 SecKeyRef 类型?

这是代码!

    //
// 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

最佳答案

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

- (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);
}

关于ios - 将 NSData 转换为 SecKeyRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19833977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com