NSString 到 SecKeyRef [英] NSString to SecKeyRef

查看:92
本文介绍了NSString 到 SecKeyRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码:https://stackoverflow.com/a/19221754/849616,但不是一切对我来说都很清楚.

I'm using this code: https://stackoverflow.com/a/19221754/849616, however not everything is clear for me.

我想使用公钥 NSString *pubKey = "1111" 加密 NSString *msg = "0000".因此,我正在更新常量:

I want to encrypt NSString *msg = "0000" using public key NSString *pubKey = "1111". Because of this, I'm updating constants:

static const UInt8 publicKeyIdentifier[] = 1111; 
// i want to encrypt only, so private key doesn't matter and I'm not posting it here

在函数 testAsymmetricEncryptionAndDecryption 中我已经更新:

In function testAsymmetricEncryptionAndDecryption I've updated:

const char inputString[] = 0000

然而结果是错误的.publicKeyIdentifier 是放置我的密钥字符串的正确位置吗?如果我的方法不对,我该怎么办..?

However the result is wrong. Is publicKeyIdentifier a right place to put my key string..? How should I do it if my approach is wrong..?

推荐答案

这个问题错了.我什至不应该尝试将其转换为 NSString.您应该将两个密钥都放在您的项目中并使用以下内容:

Well the question is wrong. I shouldn't even try to convert it to NSString. You should put both keys to your project and use something like:

- (SecKeyRef)getPrivateKeyRef {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"rsaPrivate" ofType:@"p12"];
    NSData *p12Data = [NSData dataWithContentsOfFile:resourcePath];

    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];

    SecKeyRef privateKeyRef = NULL;

    //change to the actual password you used here
    [options setObject:@"!@#EWQ" forKey:(__bridge id)kSecImportExportPassphrase];
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
    OSStatus securityError = SecPKCS12Import((__bridge CFDataRef)p12Data, (__bridge CFDictionaryRef)options, &items);

    if (securityError == noErr && CFArrayGetCount(items) > 0) {
        CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
        SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);

        securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
        if (securityError != noErr) {
            privateKeyRef = NULL;
        }
    }

    CFRelease(items);
    return privateKeyRef;
}

- (SecKeyRef)getPublicKeyRef {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"rsaCert" ofType:@"der"];
    NSData *certData = [NSData dataWithContentsOfFile:resourcePath];
    SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData);
    SecKeyRef key = NULL;
    SecTrustRef trust = NULL;
    SecPolicyRef policy = NULL;

    if (cert != NULL) {
        policy = SecPolicyCreateBasicX509();
        if (policy) {
            if (SecTrustCreateWithCertificates((CFTypeRef)cert, policy, &trust) == noErr) {
                SecTrustResultType result;
                if (SecTrustEvaluate(trust, &result) == noErr) {
                    key = SecTrustCopyPublicKey(trust);
                }
            }
        }
    }
    if (policy) CFRelease(policy);
    if (trust) CFRelease(trust);
    if (cert) CFRelease(cert);
    return key;
}

我不是自己写的(只是修改过),它大部分是复制的,但我真的不知道从哪里来 - 一些开源社区.仍然非常感谢编写它的人.

I didn't write it all by my own (just modified), it's mostly copied but really I have no idea where from - some open source community. Still, many thanks to the person who wrote it.

这篇关于NSString 到 SecKeyRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆