码迷,mamicode.com
首页 > 其他好文 > 详细

简单易用的Model加密本地存储方案

时间:2015-09-25 22:53:23      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

简单易用的Model加密本地存储方案

技术分享

 

说明

1. 加密方案采用了策略设计模式(一种加密方案是一种策略)

2. 对一个Model进行存储而不进行NSCoding编码非常便利

3. 非常简单易用

4. 支持AES、DES、CAST加密

 

源码

https://github.com/YouXianMing/StoreValueEncryptStrategy

//
//  ViewController.m
//  StoreValueEncryptStrategy
//
//  Created by YouXianMing on 15/9/25.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import "ViewController.h"
#import "AppValue.h"
#import "NSObject+AppValue.h"

#import "AES256EncryptStrategy.h"
#import "DESEncryptStrategy.h"
#import "CASTEncryptStrategy.h"

#import "Model.h"
#import "Student.h"

#define MODEL  @"model"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 设定加密策略与加密秘钥(不设定的话存储策略就为不加密,此为全局设定,你可以替换成DESEncryptStrategy或者是CASTEncryptStrategy)
    [AppValue shareInstance].appValueEncryptStrategy = [[AES256EncryptStrategy alloc] init];
    [AppValue shareInstance].password                = @"jxs340_1";
    
    // 创建Model
    Model *model   = [[Model alloc] init];
    model.name     = @"YouXianMing";
    model.age      = @(18);
    model.students = @[[Student studentWithName:@"A"],
                       [Student studentWithName:@"B"],
                       [Student studentWithName:@"C"],
                       [Student studentWithName:@"D"]];
    
    // 将Model存储到本地
    [model storeValueWithKey:MODEL];
    
    // 0.25s 后从本地取出加密的Model
    [self performSelector:@selector(event) withObject:nil afterDelay:0.25f];
}

- (void)event {

    // 获取存储在本地的数据
    Model *newModel = [AppValue accessStoreValueWithKey:MODEL];
    
    NSLog(@"%@", newModel.name);
    NSLog(@"%@", newModel.age);
    
    for (int count = 0; count < newModel.students.count; count++) {
        
        Student *student = newModel.students[count];
        NSLog(@"%@", student.name);
    }
}

@end

 

细节

技术分享

 

简单易用的Model加密本地存储方案

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4839554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!