zoukankan      html  css  js  c++  java
  • keyChain RSA加密 KVO

     1 #import "ViewController.h"
     2 #import "KeychainItemWrapper.h"
     3 #import "RSA.h"
     4 #import "Person.h"
     5 @interface ViewController ()
     6 @property (weak, nonatomic) IBOutlet UITextField *userNameField;
     7 @property (weak, nonatomic) IBOutlet UITextField *passwordField;
     8 
     9 @end
    10 
    11 @implementation ViewController
    12 
    13 - (void)viewDidLoad {
    14     [super viewDidLoad];
    15 //    //创建钥匙串对象
    16 //    KeychainItemWrapper *item = [[KeychainItemWrapper alloc] initWithIdentifier:@"唯一标识" accessGroup:nil];
    17 //    //系统提供两种key,非系统的键是没法添加到钥匙串里的,而且这两种key都是C的,用在OC需要强转
    18 //    id KUserNameKey = (__bridge id)kSecAttrAccount;
    19 //    id KPasswordKey = (__bridge id)kSecValueData;
    20 //    [item setObject:@"ZhangSan" forKey:KUserNameKey];
    21 //    [item setObject:@"12345" forKey:KPasswordKey];
    22 //
    23 //     //取值
    24 //    NSString *user = [item objectForKey:KUserNameKey];
    25 //    NSString *password = [item objectForKey:KPasswordKey];
    26 //    NSLog(@"%@,%@",user,password);
    27     
    28     //RSA加密,非对称加密
    29     //公钥和私钥都是由证书生成
    30     NSString *pubkey = @"-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
    zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
    NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
    qeox88Lc1ld7MsfggQIDAQAB
    -----END PUBLIC KEY-----";
    31     //私钥
    32     NSString *privkey  = @"-----BEGIN RSA PRIVATE KEY-----
    MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMQKGp7zSUktNOQk
    PdfcvJ3sWP7O46ENmSO4s0+iDdhHbR7klyt2oj0gXM1sAJj8ZBWFudu8GpiDKqXw
    N88IZemfT5c1LEQshTD1WHfZC6EY2vf9MGl5yGtV5WkU8vDJpg0STUJrCAcJ5Cp/
    m5qqJqgEM5Op6jHzwtzWV3syx+CBAgMBAAECgYEApSzqPzE3d3uqi+tpXB71oY5J
    cfB55PIjLPDrzFX7mlacP6JVKN7dVemVp9OvMTe/UE8LSXRVaFlkLsqXC07FJjhu
    wFXHPdnUf5sanLLdnzt3Mc8vMgUamGJl+er0wdzxM1kPTh0Tmq+DSlu5TlopAHd5
    IqF3DYiORIen3xIwp0ECQQDj6GFaXWzWAu5oUq6j1msTRV3mRZnx8Amxt1ssYM0+
    JLf6QYmpkGFqiQOhHkMgVUwRFqJC8A9EVR1eqabcBXbpAkEA3DQfLVr94vsIWL6+
    VrFcPJW9Xk28CNY6Xnvkin815o2Q0JUHIIIod1eVKCiYDUzZAYAsW0gefJ49sJ4Y
    iRJN2QJAKuxeQX2s/NWKfz1rRNIiUnvTBoZ/SvCxcrYcxsvoe9bAi7KCMdxObJkn
    hNXFQLav39wKbV73ESCSqnx7P58L2QJABmhR2+0A5EDvvj1WpokkqPKmfv7+ELfD
    HQq33LvU4q+N3jPn8C85ZDedNHzx57kru1pyb/mKQZANNX10M1DgCQJBAMKn0lEx
    QH2GrkjeWgGVpPZkp0YC+ztNjaUMJmY5g0INUlDgqTWFNftxe8ROvt7JtUvlgtKC
    XdXQrKaEnpebeUQ=
    -----END RSA PRIVATE KEY-----";
    33     //需要解密的字符串
    34    NSString  *encWithPubKey = @"p/hm1SvJm9SuuPy66rrf37+EhynkpVnCxbSCZfKznrAKVfpciX/TZM9GfLrAs+bXlND+GeOeZDz2zm+nZDtxpGV1pyQY03hOWn1MQ2+wBKKQdveEdYJ4TVXwGtC3PMaA3dwdRY+WqInQj9WX4JfuQfkYCqbmI0w86uydjFpenwE=";
    35     
    36     NSString *encStr = [RSA encryptString:@"你好" publicKey:pubkey];
    37     //输出加密后的字符串
    38     NSLog(@"%@",encStr);
    39     
    40     //RSA解密
    41     NSString *decStr = [RSA decryptString:encStr privateKey:privkey];
    42     NSLog(@"%@",decStr);
    43     
    44     
    45     
    46     //创建Person对象,并赋值
    47     Person *p1 = [Person new];
    48     p1.name = @"ZhangSan";
    49     //注册观察者(指定观察者,被观察者,被观察者属性),可以添加多个选项
    50     [p1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld | NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionPrior context:nil];
    51     //触发观察回调方法
    52     p1.name = @"ZhangSi";
    53     //移除观察者
    54     [p1 removeObserver:self forKeyPath:@"name"];
    55 
    56 }
    57 //观察者回调方法
    58 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
    59 {
    60     //keyPath 属性路径
    61     //object 被观察者
    62     //change 字典存储 kind:观察者选项,被观察对象属性值
    63     //context 回调方法可以带字符串参数
    64     NSLog(@"%@",change);
    65     
    66 }
    67 
    68 - (void)didReceiveMemoryWarning {
    69     [super didReceiveMemoryWarning];
    70     // Dispose of any resources that can be recreated.
    71 }
    72 //页面跳转
    73 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    74 {
    75     //创建钥匙串对象
    76     KeychainItemWrapper *item = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyKeyChainID" accessGroup:nil];
    77     
    78     
    79     //存储加密内容
    80     [item setObject:self.userNameField.text forKey:(__bridge id)kSecAttrAccount];
    81     
    82     [item setObject:self.passwordField.text forKey:(__bridge id)kSecValueData];
    83 }
    84 
    85 @end
  • 相关阅读:
    php基本语法与安装
    面向对象编程 es5和es6的构造函数
    利用正则搜索替换
    正则特殊符号
    正则边界符 限定符
    面试官给我挖坑:rm删除文件之后,空间就被释放了吗?
    为什么 IPv6 难以取代 IPv4
    Docker系列教程04-Docker构建镜像的三种方式
    Docker系列教程03-Docker私有仓库搭建(registry)
    Linux-I/O模型详解
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5293452.html
Copyright © 2011-2022 走看看