详细资料,请参看苹果官方文档Keychain Services Reference 。

ios中的keychain,用于保存用户的机密信息,对keychain的操作有4种,就是 增,删,改,查:

SecItemCopyMatching 查

Returns one or more keychain items that match a search query, or copies attributes of specific keychain items.

SecItemAdd 增
Adds one or more items to a keychain.

SecItemUpdate 改
Modifies items that match a search query.

SecItemDelete 删
Deletes items that match a search query.


每个操作,都需要定义相应的CFDictionary,下面看看如何定义。

操作函数需要的CFDictionary中的信息,介绍如下:

A dictionary containing an item class key-value pair (“Keychain Item Class Keys and Values” (page )) and optional attribute key-value pairs (“Attribute Item Keys and Values” (page )) specifying the item's attribute values. 

上边提到了2种key-value pair,一种是 item class key-value pair,一种是optional attribute key-value pair,首先要确定item所用的class,之后根据不同的class会用到不同的attribute。

item class key-value pair,它被描述成Keychain Item Class Keys and Values。它的key是kSecClass ,它的value有5种:

kSecClassGenericPassword,//存储普通的密码,
kSecClassInternetPassword,//存储网络密码
kSecClassCertificate,//存储证书,证书中包含共有密匙
kSecClassKey,//存储密匙,其实就是私有密匙
kSecClassIdentity,//存储Identity item,包括一个证书和一个密匙
这5种value都有相应的attribute的key-value pair 可以使用,请参看官方文档。这些attribute key-value pair 的key都是固定的常量; 他们的value,有的是常量, 有的是NSString之类的变量,可以写入自定义内容。另外,这些attribute key-value pair 有的指定了检索的条件,有的指定了检索的返回值,等等,功能不同。


下面看看如何添加一个普通密码到keychain中
- (void)addIdentityIntoKeyChain
{
OSStatus sanityCheck = noErr; NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys: (__bridge id)(kSecClassGenericPassword) ,kSecClass, @"this a my description",kSecAttrDescription, [@""dataUsingEncoding:NSUTF8StringEncoding], kSecValueData, // [@"5678" dataUsingEncoding:NSUTF8StringEncoding], kSecValueRef, nil]; NSLog(@"add dic is %@",dic); CFDictionaryRef dicRef = ( __bridgeCFDictionaryRef)dic; sanityCheck = SecItemAdd(dicRef, nil); NSLog(@"%ld",sanityCheck); }

再看看针对上面这个item的2种不同的查询,

第一种,查询具体的密码,即我们真正需要保存的东西

- (void)getPass2
{
NSLog(@"pass 2==========="); NSDictionary *queryDictionary = [NSDictionarydictionaryWithObjectsAndKeys: @"this a my description",kSecAttrDescription, kCFBooleanTrue, kSecReturnData, kSecClassGenericPassword, kSecClass, nil]; CFTypeRef handle = NULL; OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)queryDictionary, &handle); NSLog(@"status is %ld",status); if (status == noErr) { NSData *data = (__bridge NSData *)handle; NSString *str = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"str is %@",str); } }

第二种,查询这个item的各种属性,不查询具体的密码

- (NSString *)getPasswordFromKeyChain

{

    NSLog(@"pass 1===========");

    NSDictionary *queryDictionary = [NSDictionarydictionaryWithObjectsAndKeys:

                                    @"this a my description",kSecAttrDescription,

                                    kCFBooleanTrue, kSecReturnAttributes,

                                    kSecClassGenericPassword, kSecClass,

                                    nil];   

    CFTypeRef handle = NULL;

    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)queryDictionary, &handle);    

    NSLog(@"status is %ld",status);    

    if (status == noErr) {

       NSDictionary *dic = (__bridge_transfer NSDictionary*) handle ;

        NSLog(@"dic is %@",dic);        

    }    

   return@"";

} 

请查看具体的输出,来理解这2中查询的不同。

-- ::15.338 KeyChainTest[:] add dic is {
class = genp;
desc = "this a my description";
"v_Data" = <>;
}
-- ::15.356 KeyChainTest[:] -
-- ::15.359 KeyChainTest[:] pass ===========
-- ::15.369 KeyChainTest[:] status is
-- ::15.376 KeyChainTest[:] dic is {
acct = "";
agrp = "BDN8QNY54S.com.Kings.test.KeyChainTest";
cdat = "2013-10-16 06:02:54 +0000";
desc = "this a my description";
mdat = "2013-10-16 06:02:54 +0000";
pdmn = ak;
svce = "";
}
-- ::15.381 KeyChainTest[:] pass ===========
-- ::15.389 KeyChainTest[:] status is
-- ::15.393 KeyChainTest[:] str is

另外,在keychain的使用中,还经常涉及一个问题,就是2个程序共享机密信息。这常常发生在以下情况:大型公司专门写了一个用户认证程序,指定的机器可以装上这个程序,用这个程序时要求输入帐号和密码,验证通过后,程序会从服务器取公司的证书和用户的私有密匙,并将他们加入到程序的keychain中 。之后该公司写的其他程序,如果需要帐号和密码,或者证书进行服务器验身份证时,就可以用刚才提到的用户认证程序在keychain中保存的信息。

ios程序默认的keychain是不能够和其他程序共享的,如果想共享自己的信息,那么需要加入到一个Keychain Access Groups中,xcode5在工程设定中可以添加。主要注意的是这个Keychain Access Groups的名字是有限制的,必须和你的provisioning profile向一致,这就保证不可能访问到其他公司的机密信息。

转一些别人的心得:

1.相同bundle下生成的程序都可以共享相同group的keyChain.(我对这一条的理解不太一样,我感觉是这样的:bundle Name 分别是 com.companyName.A 和 com.companyName.B,那么这两个程序的keychain group name 就必须是 com.companyName.xxxx)

相同bundle解释下就是:比如:2个程序分别使用的provision对应bundle是com.jv.key1和com.jv.key2,那你配置文件肯定是{Identifer}.com.jv.{name},其中identifer是苹果生成的随机串号,可以在申请证书时看到,复制过来即可,name可以自己取,程序中指定属于哪个Group即可。

2.如果你在 addkey时,没有指定group,则会默认添加你keychain-access-groups里第一个group(这是keychain group中经常写2个group的原因,简历一个自己的group,防止污染主要的keychain group),如果你没有设置Entitlements,则默认使用对应的程序的bundle name,比如com.jv.key1,表示只能给自己程序使用。

3.如果你程序添加的group并不存在你的配置文件中,程序会奔溃,表示无法添加。因此你只能添加你配置文件中支持的keychain。

iOS keyChain 的使用的更多相关文章

  1. iOS - keychain 详解及变化

    keychain介绍 iOS keychain 是一个相对独立的空间,保存到keychain钥匙串中的信息不会因为卸载/重装app而丢失, .相对于NSUserDefaults.plist文件保存等一 ...

  2. iOS Keychain钥匙串,应用间数据共享打造iOS上的全家桶

    Demo先行:https://github.com/rayshen/GIKeychainGroupDemo 该demo里有2个工程,你先运行任何一个会存储一个值,再运行另一个会访问之前的app存储的值 ...

  3. iOS keyChain

    keychain在ios中是保存在sqlite数据库中的. 这个数据库文件的位置: 真机: /private/var/Keychains/keychain-2.db 虚拟机: /Users/USER- ...

  4. (转)iOS keychain API及其封装

    一. Keychain API KeyChain中item的结构为: 1.增加keychain Item OSStatus SecItemAdd (CFDictionaryRef attributes ...

  5. iOS keyChain(钥匙串)的简单使用

    通常在开发中我们需要长久的保存某些值比如用户的账号密码等,对于隐私度很高的数据来说保证数据的安全性是尤为重要的.ios中的keyChain是一种很好的选择. 首先去开发者网站(https://deve ...

  6. iOS Keychain,SSKeychain,使用 理解 原理

    https://www.cnblogs.com/m4abcd/p/5242254.html Keychain 使用? ---为了实用最大化我觉得我应该直接先说使用! 当然是使用第三方库啦:sskeyc ...

  7. 聊聊iOS Keychain

    某天,小熊碰见这样一个错误 Couldn't update the Keychain Item问题处理 ,网上搜索了下网上很多解决方案,依然百撕不得骑姐.后来参考下面两篇文章.才发现是用法不正确,网上 ...

  8. iOS keychain入门

    学了很久的iOS,一直都是明文保存用户名和密码在本地,手机一般都是自己用的,而且非越狱手机东西也不怎么能拿到数据,所以也就没在乎那么多,当然,这是不科学的.悄悄的说,这块一直不是我写的~~~ 用户隐私 ...

  9. [iOS Keychain本地长期键值存储]

    目前本地存储方式大致有:Sqlite,Coredata,NSUserdefaults.但他们都是在删除APP后就会被删除,如果长期使用存储,可以使用Keychain钥匙串来实现. CHKeychain ...

随机推荐

  1. aop测试jdk代理机制

    //测试jdk代理机制 @Test public void testProxy(){ final UsbDisk usbDisk = new UsbDisk(); //类加载器,接口,匿名内部类 // ...

  2. char,short ,int ,long,long long,unsigned long long数据范围

    from:http://www.cnblogs.com/A123456A/archive/2013/01/25/2876634.html char,short ,int ,long,long long ...

  3. 如何在html中做圆角矩形和 只有右边的"分隔线"

    这个网站满好的,可以常看看 css-matic中有几个很好的写css可视化的工具 其实做css 版式布局等都可以有工具的 推荐40个优秀的免费CSS工具 debugger正则表达式在线 其实是对(理论 ...

  4. Java字节流:BufferedInputStream BufferedOutputStream

    -----------------------------------------------------------------------------------BufferedInputStre ...

  5. for 循环中 i++和 ++i

    在标准C语言中, i++和 ++i的区别显而易见. 但是,当在for循环中使用 i++和 ++i的时候,会发现.只要这两种语句不用来赋值操作(作为右值,赋值给左值),那么这两种写法其实是一样的. fo ...

  6. Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】

    众所周知,win平台的服务器版本默认是不能运行php的,需要对服务器进行环境配置. 而许多朋友纠结如何配置,在百度上搜索出的教程一大堆,基本步骤复杂,新手配置容易出错. 今天,邹颖峥教大家一种快速配置 ...

  7. [译] ASP.NET MVC 6 attribute routing – the [controller] and [action] tokens

    原文:http://www.strathweb.com/2015/01/asp-net-mvc-6-attribute-routing-controller-action-tokens/ 当在Web ...

  8. VTK初学一,b_PolyVertex_CellArray多个点的绘制

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  9. Swift实战之2048小游戏

    上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...

  10. CentOS-6.5-NFS部署

                      nfs-server与nfs-client端配置一样   NFS(network file system)网络文件系统:用于在网络上共享存储. 服务端-192.16 ...