为什么SecItemAdd返回我-50(无效参数) [英] Why SecItemAdd return me -50 (invalid params)

查看:198
本文介绍了为什么SecItemAdd返回我-50(无效参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在钥匙串中存储值"MyKeyValue",我这样做:

I want to store in the keychain the value "MyKeyValue" and I do like this :

NSData* key = [@"MyKeyValue" dataUsingEncoding:NSUTF8StringEncoding];
NSData* tag = [@"com.example.MyKey" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* addquery = @{ (id)kSecValueRef: key,
                            (id)kSecClass: (id)kSecClassKey,
                            (id)kSecAttrApplicationTag: tag,
                           };
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addquery, NULL);

但是失败,错误为-50(无效的参数) 我做错了什么?

but this failed with error -50 (Invalid params) What i did wrong ?

我想在钥匙串中存储一个字符串,如果用户卸载并重新安装我的应用程序,则可以检索该字符串.

I would like to store in the keychain a string that can be retrieved if the user uninstall and reinstall my app.

推荐答案

由于kSecValueRef,因此发生了错误,按照Apple的准则kSecValueRef接受可以通过SecKeyRef生成的加密密钥,请在下面找到,

The error is occurring because of kSecValueRef, as per Apple's guideline kSecValueRef accepts a cryptographic key which can be generated through SecKeyRef, Please find below,

    NSData* tag = [@"com.example.keys.mykey" dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary* attributes =
    @{ (id)kSecAttrKeyType:               (id)kSecAttrKeyTypeRSA,
       (id)kSecAttrKeySizeInBits:         @2048,
       (id)kSecPrivateKeyAttrs:
           @{ (id)kSecAttrIsPermanent:    @YES,
              (id)kSecAttrApplicationTag: tag,
              },
       };
    CFErrorRef error = NULL;
    SecKeyRef privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes,
                                                 &error);
    SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
    NSDictionary* addquery = @{ (id)kSecValueRef: (__bridge id)publicKey,
                                (id)kSecClass: (id)kSecClassKey,
                                (id)kSecAttrApplicationTag: tag,
                                };
    OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addquery, NULL);

有关更多信息,请参考在钥匙串中存储钥匙

For more info please refer Storing Keys in the Keychain

这篇关于为什么SecItemAdd返回我-50(无效参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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