一、数据安全

术语-----

密钥:密钥是一种参数, 它是在明文转换为密文, 或将密文转换为明文的算法中输入的参数. 密钥分为对称密钥和非对称密钥(也可以根据用途来分为加密密钥和解密密钥)

明文:没有进行加密, 能够直接代表原文含义的信息

密文:经过加密处理之后, 隐藏原文含义的信息

加密:将明文转换成密文的实施过程

解密:将密文转换成明文的实施过程

数据安全-----

数据安全: 是一种主动的包含措施, 数据本身的安全必须基于可靠的加密算法与安全体系,主要是有对称算法与公开密钥密码体系两种(非对称算法), 都包含了数据的加密和解密过程

二、MD5

哈希算法-----

哈希算法:哈希算法将任意长度的二进制值映射为较短的固定长度的二进制值, 这个小的二进制值成为哈希值

哈希值是一段数据唯一且及其紧凑的数值表示形式. 数据的哈希值可以检验数据的完整性. 一般用于快速查找和加密算法

典型的哈希算法就有MD5

MD5-----

MD5:Message Digest Alogorithm MD5 (消息摘要算法第五版) 为计算机安全领域广泛使用的一种散列函数, 用以提供小米的完整性保护MD5算法具有以下特点:

1、压缩性: 任意长度的数据, 算出的MD5值长度都是固定的 (16进制, 32位)

2、容易计算: 从原数据计算出MD5值很容易

3、抗修改性: 对原数据进行任何改动, 哪怕只修改1个字节, 所得到的MD5值都有很大区别

4、强抗碰撞: 已知原数据和其MD5值, 想找到一个具有相同MD5值的数据(伪造数据)是非常困难的

获取字符串MD5值-----引入头文件

#import <CommonCrypto/CommonCrypto.h>

  1. //1.准备一个字符串用于加密
  2. NSString *str = @"go ahead";//同一个字符串进行MD5加密出来的内容相同
  3. #pragma mark - ??????????????一个内容加了const有什么含义
  4. //2.因为MD5基于C语言, 所以需要将字符串转为C语言的字符串进行编码
  5. const char *data = [str UTF8String];
  6.  
  7. //3.使用字符串数组去存取加密后相关的内容(16进制, 32位)
  8. //CC_MD5_DIGEST_LENGTH表示长度
  9. unsigned char result[CC_MD5_DIGEST_LENGTH];
  10.  
  11. //4.进行MD5加密
  12. //参数1: 要加密的内容
  13. //参数2: 要加密的data的一个长度
  14. //参数3: MD5
  15. CC_MD5(data, (CC_LONG)strlen(data), result);
  16.  
  17. //5.创建可变字符串, 保存结果
  18. NSMutableString *mutableString = [NSMutableString string];
  19.  
  20. //6.遍历结果数组, 然后添加到可变字符串中
  21. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
  22.  
  23. //16进制 //%x 16进制, 02不足两位时补0.
  24. [mutableString appendFormat:@"%02x", result[i]];
  25. }
  26. NSLog(@"%@", mutableString);

获取其他对象MD5的值-----

加密NSData

  1. #pragma mark - 加密NSData
  2.  
  3. //需求: 创建一个数组, 数组中存储元素, 将这个数组写入到沙盒里
  4.  
  5. //1.创建一个数组
  6. NSArray *array = @[@"go", @"rose"];
  7. //2.找沙盒路径
  8. NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  9.  
  10. //3.拼接路径
  11. NSString *arrayPath = [documentPath stringByAppendingPathComponent:@"array.plist"];
  12.  
  13. //4.写入
  14. [array writeToFile:arrayPath atomically:YES];
  15.  
  16. //从沙盒中取出NSData类型的数据
  17. NSData *data = [NSData dataWithContentsOfFile:arrayPath];
  18.  
  19. NSLog(@"%@", data);
  20.  
  21. //NSData类型数据加密过程
  22.  
  23. //1.创建MD5对象
  24. CC_MD5_CTX md5;
  25.  
  26. //2.初始化MD5对象
  27. CC_MD5_Init(&md5);
  28.  
  29. //3.准备开始进行数据的加密
  30. CC_MD5_Update(&md5, data.bytes, (CC_LONG)data.length);
  31.  
  32. //4.准备一个字符串数组用来存储结果
  33. unsigned char result[CC_MD5_DIGEST_LENGTH];
  34.  
  35. //5.创建一个可变字符串保存结果
  36. NSMutableString *string = [NSMutableString string];
  37. //结束加密
  38. CC_MD5_Final(result, &md5);
  39. //6.遍历数组给可变字符串赋值
  40. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
  41. [string appendFormat:@"%02x", result[i]];
  42. }
  43. NSLog(@"%@", string);

钥匙串加密-----

苹果iOS和Mac OS X系统自带了一套敏感信息保存方案: "钥匙串"(Keychain)

保存在钥匙串的内容相当于系统对其做了保护, 在设备锁定时进行了加密处理

钥匙串中的条目成为SecItem, 但它是存储在CFDictionary中的 SecItemRef类型并不存在, SecItem有五类:通用密码、互联网密码、证书、密钥、和身份, 在大多数情况下, 我们用到的都是通用密码

钥匙串的使用和字典非常相似

用原生的 Security.framework就可以实现钥匙串的访问、续写, 但是只能在真机上进行, 通常我们使用KeychainItemWrapper来完成钥匙串的加密

引入头文件

#import "KeychainItemWrapper.h"

  1. /*
  2. File: KeychainItemWrapper.h
  3. Abstract:
  4. Objective-C wrapper for accessing a single keychain item.
  5.  
  6. Version: 1.2
  7.  
  8. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  9. Inc. ("Apple") in consideration of your agreement to the following
  10. terms, and your use, installation, modification or redistribution of
  11. this Apple software constitutes acceptance of these terms. If you do
  12. not agree with these terms, please do not use, install, modify or
  13. redistribute this Apple software.
  14.  
  15. In consideration of your agreement to abide by the following terms, and
  16. subject to these terms, Apple grants you a personal, non-exclusive
  17. license, under Apple's copyrights in this original Apple software (the
  18. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  19. Software, with or without modifications, in source and/or binary forms;
  20. provided that if you redistribute the Apple Software in its entirety and
  21. without modifications, you must retain this notice and the following
  22. text and disclaimers in all such redistributions of the Apple Software.
  23. Neither the name, trademarks, service marks or logos of Apple Inc. may
  24. be used to endorse or promote products derived from the Apple Software
  25. without specific prior written permission from Apple. Except as
  26. expressly stated in this notice, no other rights or licenses, express or
  27. implied, are granted by Apple herein, including but not limited to any
  28. patent rights that may be infringed by your derivative works or by other
  29. works in which the Apple Software may be incorporated.
  30.  
  31. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  32. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  33. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  35. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  36.  
  37. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  38. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  41. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  42. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  43. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  44. POSSIBILITY OF SUCH DAMAGE.
  45.  
  46. Copyright (C) 2010 Apple Inc. All Rights Reserved.
  47.  
  48. */
  49.  
  50. #import <UIKit/UIKit.h>
  51.  
  52. /*
  53. The KeychainItemWrapper class is an abstraction layer for the iPhone Keychain communication. It is merely a
  54. simple wrapper to provide a distinct barrier between all the idiosyncracies involved with the Keychain
  55. CF/NS container objects.
  56. */
  57. @interface KeychainItemWrapper : NSObject
  58. {
  59. NSMutableDictionary *keychainItemData; // The actual keychain item data backing store.
  60. NSMutableDictionary *genericPasswordQuery; // A placeholder for the generic keychain item query used to locate the item.
  61. }
  62.  
  63. @property (nonatomic, retain) NSMutableDictionary *keychainItemData;
  64. @property (nonatomic, retain) NSMutableDictionary *genericPasswordQuery;
  65.  
  66. // Designated initializer.
  67. - (id)initWithIdentifier: (NSString *)identifier accessGroup:(NSString *) accessGroup;
  68. - (void)setObject:(id)inObject forKey:(id)key;
  69. - (id)objectForKey:(id)key;
  70.  
  71. // Initializes and resets the default generic keychain item data.
  72. - (void)resetKeychainItem;
  73.  
  74. @end
  1. /*
  2. File: KeychainItemWrapper.m
  3. Abstract:
  4. Objective-C wrapper for accessing a single keychain item.
  5.  
  6. Version: 1.2
  7.  
  8. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  9. Inc. ("Apple") in consideration of your agreement to the following
  10. terms, and your use, installation, modification or redistribution of
  11. this Apple software constitutes acceptance of these terms. If you do
  12. not agree with these terms, please do not use, install, modify or
  13. redistribute this Apple software.
  14.  
  15. In consideration of your agreement to abide by the following terms, and
  16. subject to these terms, Apple grants you a personal, non-exclusive
  17. license, under Apple's copyrights in this original Apple software (the
  18. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  19. Software, with or without modifications, in source and/or binary forms;
  20. provided that if you redistribute the Apple Software in its entirety and
  21. without modifications, you must retain this notice and the following
  22. text and disclaimers in all such redistributions of the Apple Software.
  23. Neither the name, trademarks, service marks or logos of Apple Inc. may
  24. be used to endorse or promote products derived from the Apple Software
  25. without specific prior written permission from Apple. Except as
  26. expressly stated in this notice, no other rights or licenses, express or
  27. implied, are granted by Apple herein, including but not limited to any
  28. patent rights that may be infringed by your derivative works or by other
  29. works in which the Apple Software may be incorporated.
  30.  
  31. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  32. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  33. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  35. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  36.  
  37. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  38. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  41. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  42. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  43. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  44. POSSIBILITY OF SUCH DAMAGE.
  45.  
  46. Copyright (C) 2010 Apple Inc. All Rights Reserved.
  47.  
  48. */
  49.  
  50. #import "KeychainItemWrapper.h"
  51. #import <Security/Security.h>
  52.  
  53. /*
  54.  
  55. These are the default constants and their respective types,
  56. available for the kSecClassGenericPassword Keychain Item class:
  57.  
  58. kSecAttrAccessGroup - CFStringRef
  59. kSecAttrCreationDate - CFDateRef
  60. kSecAttrModificationDate - CFDateRef
  61. kSecAttrDescription - CFStringRef
  62. kSecAttrComment - CFStringRef
  63. kSecAttrCreator - CFNumberRef
  64. kSecAttrType - CFNumberRef
  65. kSecAttrLabel - CFStringRef
  66. kSecAttrIsInvisible - CFBooleanRef
  67. kSecAttrIsNegative - CFBooleanRef
  68. kSecAttrAccount - CFStringRef
  69. kSecAttrService - CFStringRef
  70. kSecAttrGeneric - CFDataRef
  71.  
  72. See the header file Security/SecItem.h for more details.
  73.  
  74. */
  75.  
  76. @interface KeychainItemWrapper (PrivateMethods)
  77. /*
  78. The decision behind the following two methods (secItemFormatToDictionary and dictionaryToSecItemFormat) was
  79. to encapsulate the transition between what the detail view controller was expecting (NSString *) and what the
  80. Keychain API expects as a validly constructed container class.
  81. */
  82. - (NSMutableDictionary *)secItemFormatToDictionary:(NSDictionary *)dictionaryToConvert;
  83. - (NSMutableDictionary *)dictionaryToSecItemFormat:(NSDictionary *)dictionaryToConvert;
  84.  
  85. // Updates the item in the keychain, or adds it if it doesn't exist.
  86. - (void)writeToKeychain;
  87.  
  88. @end
  89.  
  90. @implementation KeychainItemWrapper
  91.  
  92. @synthesize keychainItemData, genericPasswordQuery;
  93.  
  94. - (id)initWithIdentifier: (NSString *)identifier accessGroup:(NSString *) accessGroup;
  95. {
  96. if (self = [super init])
  97. {
  98. // Begin Keychain search setup. The genericPasswordQuery leverages the special user
  99. // defined attribute kSecAttrGeneric to distinguish itself between other generic Keychain
  100. // items which may be included by the same application.
  101. genericPasswordQuery = [[NSMutableDictionary alloc] init];
  102.  
  103. [genericPasswordQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
  104. [genericPasswordQuery setObject:identifier forKey:(id)kSecAttrGeneric];
  105.  
  106. // The keychain access group attribute determines if this item can be shared
  107. // amongst multiple apps whose code signing entitlements contain the same keychain access group.
  108. if (accessGroup != nil)
  109. {
  110. #if TARGET_IPHONE_SIMULATOR
  111. // Ignore the access group if running on the iPhone simulator.
  112. //
  113. // Apps that are built for the simulator aren't signed, so there's no keychain access group
  114. // for the simulator to check. This means that all apps can see all keychain items when run
  115. // on the simulator.
  116. //
  117. // If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
  118. // simulator will return -25243 (errSecNoAccessForItem).
  119. #else
  120. [genericPasswordQuery setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
  121. #endif
  122. }
  123.  
  124. // Use the proper search constants, return only the attributes of the first match.
  125. [genericPasswordQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  126. [genericPasswordQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
  127.  
  128. NSDictionary *tempQuery = [NSDictionary dictionaryWithDictionary:genericPasswordQuery];
  129.  
  130. NSMutableDictionary *outDictionary = nil;
  131.  
  132. if (! SecItemCopyMatching((CFDictionaryRef)tempQuery, (CFTypeRef *)&outDictionary) == noErr)
  133. {
  134. // Stick these default values into keychain item if nothing found.
  135. [self resetKeychainItem];
  136.  
  137. // Add the generic attribute and the keychain access group.
  138. [keychainItemData setObject:identifier forKey:(id)kSecAttrGeneric];
  139. if (accessGroup != nil)
  140. {
  141. #if TARGET_IPHONE_SIMULATOR
  142. // Ignore the access group if running on the iPhone simulator.
  143. //
  144. // Apps that are built for the simulator aren't signed, so there's no keychain access group
  145. // for the simulator to check. This means that all apps can see all keychain items when run
  146. // on the simulator.
  147. //
  148. // If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
  149. // simulator will return -25243 (errSecNoAccessForItem).
  150. #else
  151. [keychainItemData setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
  152. #endif
  153. }
  154. }
  155. else
  156. {
  157. // load the saved data from Keychain.
  158. self.keychainItemData = [self secItemFormatToDictionary:outDictionary];
  159. }
  160.  
  161. [outDictionary release];
  162. }
  163.  
  164. return self;
  165. }
  166.  
  167. - (void)dealloc
  168. {
  169. [keychainItemData release];
  170. [genericPasswordQuery release];
  171.  
  172. [super dealloc];
  173. }
  174.  
  175. - (void)setObject:(id)inObject forKey:(id)key
  176. {
  177. if (inObject == nil) return;
  178. id currentObject = [keychainItemData objectForKey:key];
  179. if (![currentObject isEqual:inObject])
  180. {
  181. [keychainItemData setObject:inObject forKey:key];
  182. [self writeToKeychain];
  183. }
  184. }
  185.  
  186. - (id)objectForKey:(id)key
  187. {
  188. return [keychainItemData objectForKey:key];
  189. }
  190.  
  191. - (void)resetKeychainItem
  192. {
  193. OSStatus junk = noErr;
  194. if (!keychainItemData)
  195. {
  196. self.keychainItemData = [[NSMutableDictionary alloc] init];
  197. }
  198. else if (keychainItemData)
  199. {
  200. NSMutableDictionary *tempDictionary = [self dictionaryToSecItemFormat:keychainItemData];
  201. junk = SecItemDelete((CFDictionaryRef)tempDictionary);
  202. NSAssert( junk == noErr || junk == errSecItemNotFound, @"Problem deleting current dictionary." );
  203. }
  204.  
  205. // Default attributes for keychain item.
  206. [keychainItemData setObject:@"" forKey:(id)kSecAttrAccount];
  207. [keychainItemData setObject:@"" forKey:(id)kSecAttrLabel];
  208. [keychainItemData setObject:@"" forKey:(id)kSecAttrDescription];
  209.  
  210. // Default data for keychain item.
  211. [keychainItemData setObject:@"" forKey:(id)kSecValueData];
  212. }
  213.  
  214. - (NSMutableDictionary *)dictionaryToSecItemFormat:(NSDictionary *)dictionaryToConvert
  215. {
  216. // The assumption is that this method will be called with a properly populated dictionary
  217. // containing all the right key/value pairs for a SecItem.
  218.  
  219. // Create a dictionary to return populated with the attributes and data.
  220. NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionaryToConvert];
  221.  
  222. // Add the Generic Password keychain item class attribute.
  223. [returnDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
  224.  
  225. // Convert the NSString to NSData to meet the requirements for the value type kSecValueData.
  226. // This is where to store sensitive data that should be encrypted.
  227. NSString *passwordString = [dictionaryToConvert objectForKey:(id)kSecValueData];
  228. [returnDictionary setObject:[passwordString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData];
  229.  
  230. return returnDictionary;
  231. }
  232.  
  233. - (NSMutableDictionary *)secItemFormatToDictionary:(NSDictionary *)dictionaryToConvert
  234. {
  235. // The assumption is that this method will be called with a properly populated dictionary
  236. // containing all the right key/value pairs for the UI element.
  237.  
  238. // Create a dictionary to return populated with the attributes and data.
  239. NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionaryToConvert];
  240.  
  241. // Add the proper search key and class attribute.
  242. [returnDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  243. [returnDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
  244.  
  245. // Acquire the password data from the attributes.
  246. NSData *passwordData = NULL;
  247. if (SecItemCopyMatching((CFDictionaryRef)returnDictionary, (CFTypeRef *)&passwordData) == noErr)
  248. {
  249. // Remove the search, class, and identifier key/value, we don't need them anymore.
  250. [returnDictionary removeObjectForKey:(id)kSecReturnData];
  251.  
  252. // Add the password to the dictionary, converting from NSData to NSString.
  253. NSString *password = [[[NSString alloc] initWithBytes:[passwordData bytes] length:[passwordData length]
  254. encoding:NSUTF8StringEncoding] autorelease];
  255. [returnDictionary setObject:password forKey:(id)kSecValueData];
  256. }
  257. else
  258. {
  259. // Don't do anything if nothing is found.
  260. NSAssert(NO, @"Serious error, no matching item found in the keychain.\n");
  261. }
  262.  
  263. [passwordData release];
  264.  
  265. return returnDictionary;
  266. }
  267.  
  268. - (void)writeToKeychain
  269. {
  270. NSDictionary *attributes = NULL;
  271. NSMutableDictionary *updateItem = NULL;
  272. OSStatus result;
  273.  
  274. if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
  275. {
  276. // First we need the attributes from the Keychain.
  277. updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];
  278. // Second we need to add the appropriate search key/values.
  279. [updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
  280.  
  281. // Lastly, we need to set up the updated attribute list being careful to remove the class.
  282. NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
  283. [tempCheck removeObjectForKey:(id)kSecClass];
  284.  
  285. #if TARGET_IPHONE_SIMULATOR
  286. // Remove the access group if running on the iPhone simulator.
  287. //
  288. // Apps that are built for the simulator aren't signed, so there's no keychain access group
  289. // for the simulator to check. This means that all apps can see all keychain items when run
  290. // on the simulator.
  291. //
  292. // If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
  293. // simulator will return -25243 (errSecNoAccessForItem).
  294. //
  295. // The access group attribute will be included in items returned by SecItemCopyMatching,
  296. // which is why we need to remove it before updating the item.
  297. [tempCheck removeObjectForKey:(id)kSecAttrAccessGroup];
  298. #endif
  299.  
  300. // An implicit assumption is that you can only update a single item at a time.
  301.  
  302. result = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
  303. NSAssert( result == noErr, @"Couldn't update the Keychain Item." );
  304. }
  305. else
  306. {
  307. // No previous item found; add the new one.
  308. result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
  309. NSAssert( result == noErr, @"Couldn't add the Keychain Item." );
  310. }
  311. }
  312.  
  313. @end
  1. //1.创建钥匙串对象
  2. //参数1: 标识, 用于识别将要加密的内容(回传标识符)
  3. //参数2: 分组 一般为nil
  4. KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"myItemWrapper" accessGroup:nil];
  5.  
  6. //常用于加密用户名和密码
  7. //系统提供键值对中的两个键, 非系统的键是没法添加到字典中的
  8.  
  9. id kUserName = (__bridge id)kSecAttrAccount;
  10. id kUserPassword = (__bridge id)kSecValueData;
  11.  
  12. [keychainItem setObject:@"MBBoy" forKey:kUserName];
  13. [keychainItem setObject:@"123456" forKey:kUserPassword];
  14.  
  15. //从keychain中获取存储的数据
  16. //用户名
  17. NSString *userName = [keychainItem objectForKey:kUserName];
  18. //密码
  19. NSString *password = [keychainItem objectForKey:kUserPassword];

KVO加密-----

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4.  
  5. //初始化
  6. self.array = [NSMutableArray array];
  7.  
  8. //第一步: 注册观察者
  9. //参数1: 添加的观察者对象
  10. //参数2: 字符串标识的key
  11. //参数3: 什么时候会触发观察者对象
  12. //参数4: 文本内容 一般为nil
  13.  
  14. [self addObserver:self forKeyPath:@"array" options:NSKeyValueObservingOptionNew context:nil];
  15.  
  16. }
  17.  
  18. //第二步: 实现回调方法
  19. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
  20.  
  21. NSLog(@"keyPath = %@", keyPath);
  22. NSLog(@"object = %@", object);
  23. NSLog(@"change = %@", change);
  24.  
  25. //可以进行刷新UI的操作
  26.  
  27. }
  28.  
  29. //视图将要消失的时候
  30. - (void)viewWillDisappear:(BOOL)animated {
  31.  
  32. //在不要需要观察者的时候需要把它给干掉
  33. [self removeObserver:self forKeyPath:@"array"];
  34.  
  35. }
  36.  
  37. //第三步: 触发可变数组进行改变
  38. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  39.  
  40. NSArray *subArray = @[@"1", @"2", @"3"];
  41.  
  42. //根据keyPath获取到可变数组对象设置数据
  43. [[self mutableArrayValueForKeyPath:@"array"] setArray:subArray];
  44. }

iOS之数据安全的更多相关文章

  1. 保护 iOS 用户数据安全: Keychain 和 Touch ID

    原文:How To Secure iOS User Data: The Keychain and Touch ID 作者:Tim Mitra 译者:kmyhy 更新说明:本教程由 Tim Mitra ...

  2. iOS开发中的数据安全隐患和解决方案

    移动互联网的兴起,每天都会涌现大量的app产品,无论公司开发还是个人开发,大家都在追求更快的抢占市场,但是确忽略了打磨产品,也忽略了移动开发中的数据安全隐患,如果我们用Charles工具,很容易截获到 ...

  3. 苹果系统iOS、macOS应用管理机制

    iOS.macOS系统应用管理机制 苹果系统包括:iOS.macOS.watchOS.tvOS.应用软件的生命周期为:开发.上线.安装.使用.卸载.这篇文档将从应用生命周期的各个环节介绍苹果系统对应用 ...

  4. iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态

    一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...

  5. 【原】iOS学习40网络之数据安全

    在互联网发展趋势迅猛的今天,数据安全的重要性日趋凸显.也成为我们必须了解的互联网知识. 在移动互联网浪潮下,用户的资金安全.企业的信息安全都是我们实际开发中必须考虑的内容.

  6. iOS开发网络篇—数据安全

    iOS开发网络篇—数据安全 一.简单说明 1.说明 在开发应用的时候,数据的安全性至关重要,而仅仅用POST请求提交用户的隐私数据,还是不能完全解决安全问题. 如:可以利用软件(比如Charles)设 ...

  7. iOS开发 - 网络数据安全加密(MD5)

    提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 用户的隐私数据登 ...

  8. iOS——数据安全

    在移动互联网快速发展的今天,iOS应用直接运行在用户的手机上,与运行在服务器后台服务相比,更有可能被黑客攻击. a.网络安全: 1.1 安全地传输用户密码 事先生成一对用于加密的公私钥,客户端登录的时 ...

  9. iOS 数据安全、数据加密传输

    近期接到一个新需求:APP企业版需要接入热更新功能. 热更新需要下发补丁脚本, 脚本下发过程中需要保证脚本传输安全,且需要避免中间人攻击. 需要用到数据加密传输方面的知识,以下是我设计的加密解密流程: ...

随机推荐

  1. IOI2011ricehub米仓

    Description 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺 ...

  2. appium 1.6.3+Xcode运 For Mac

    1. 下载两个版本的Xcode, Xcode 1.7.3 与Xcode 8.2 ,放置在不同的目录下.进入terminal, 选择需要的版本: sudo xcode-select -switch // ...

  3. Linux启动盘-ultraiso

    感觉windos和linux安装系统的启动盘不一样 其实我也不太懂. windos的时候我是用老毛桃  然后安装linux我就选择了ultraios作为启动盘 一: 先在百度下载 ultraios 二 ...

  4. WINDOWS7(vs2012+wdk7.6) 配置驱动开发环境

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q  Q:408365330     E-Mail:egojit@qq.com 1.新建C ...

  5. Best Time to Buy and Sell Stock III [LeetCode]

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. SHAREPOINT 2013 列表之间相互关联

    修改内容 1.增加列表设置,隐藏Aid字段操作 SharePoint 列表之间相互关联 例如两张列表之间的父子关系. 思路如下: 列表中新增列表项后会有一个唯一的ID,我们获取到该ID赋予子表即可将两 ...

  7. css中各种情况下的元素的垂直和水平居中的问题

    问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器垂直水平居中显示?? No1: 外边的容器宽度和高度确认,里边是行内元素 .container{width:200px; height ...

  8. react native 页面跳转

    React Native目前有几个内置的导航器组件,一般来说我们首推Navigator.它使用纯JavaScript实现了一个导航栈,因此可以跨平台工作 场景简单来说其实就是一个全屏的React组件. ...

  9. js 判断是什么类型浏览器

    //  firefoxif ( window.sidebar && "object" == typeof( window.sidebar ) && ...

  10. solution to E: failed to fetch .......

    There are some issues today for me that my desktop can't boot as I expected, I installed windows 8.1 ...