iOS数据安全--密码学

2018-10-26  本文已影响0人  Mr_WangZz

网络开发原则

  1. 在网络上不允许传输用户的明文隐私数据;
  2. 在本地不允许保存用户的明文隐私数据;

常用的加密方式

哈希(散列)函数 对称加密算法 非对称加密算法
MD5 SHA1 SHA256/512 DES 3DES AES RSA

散列函数

MD5、SHA1 SHA256/512
特点:

MD5的用途:

1.作为 密码识别

2.用来 数据识别

ios + wangzzcloud : 
9e304d4e8df1b74cfa009913198428ab+836a626786650cf45af100fd21fd1083
//同个文件不同名称:
MrwangPro:desktop wangzz$ md5 124.png
MD5 (124.png) = 7dbeafb656237acd295b667a3a34621e
MrwangPro:desktop wangzz$ md5 123.png
MD5 (123.png) = 7dbeafb656237acd295b667a3a34621e
//copy复制之后:
MrwangPro:desktop wangzz$ md5 1232.png
MD5 (1232.png) = 7dbeafb656237acd295b667a3a34621e
//不同文件之间:
MrwangPro:desktop wangzz$ md5 1111.png
MD5 (1111.png) = 65de7bd275bf7152b81165ccbd4db7d9

散列破解

单纯的MD5加密破解难度低,这里的破解是指,虽然过程不可逆,但是加密结果可被收录,对比可得原字符串,见www.cmd5.com

提高MD5的安全性

  1. 加 "盐":

早期使用

  1. HMAC: 封装好的动态获取盐的方法,最近在增多

用户在注册时,从服务器获取 密钥(key),

保存在客户端本地: 钥匙串。

切换了设备,登录已有账号:重新找服务器获取

账号 key(密钥)
在新设备上获取验证的key(获取设备授权),可通过:

给定一个密钥,对 明文 进行 密钥 拼接,并做两次散列 => 32结果

特殊情况:

如何避免一种情况:

如果黑客 模拟你的网络请求..不需要拿到真实密码!用加密后的信息,也可以获得登录之后的权限!!

解决方式:让我们的密码具有 时效性 !! 也就是 加密过后的密码 有时间限制!!

客户端:

UserID = wangzz
Pass == ((123456)MD5+KEY)HMAC == e9cdab82d48dcd37af7734b6617357e6
KEY、temptime(服务器返回的时间)

发送:UserID : (Pass + '201702102020')md5

服务器:

KEY
UserID = wangzz
密码 = Pass == e9cdab82d48dcd37af7734b6617357e6

对比:

if((Pass + '201702102020')md5 == (Pass + '201702102021')md5 
|| (Pass + '201702102020')md5 == (Pass + '201702102020')md5) {
    //验证成功
}  

只要服务器当前时间或者上一分钟 加密后的字符相等!就返回登录成功!!

隐私数据本地保存方法:钥匙串

NSUserDefaults: 只是Apple封装的一个全局字典类;

采用第三方 SSKeyChain ,可保存至本地的钥匙串中,采用的是AES对称加密算法。


可逆的加密算法

对称加密 -- 传统加密算法

加密和解密使用同一个Key(密钥)

密钥的保密工作很重要;会定期更新。

经典算法:

加密方式:

如:

加密前:
00000000
11111111
22222222
00000000
11111111
22222222
00000000
11111111
22222222

//ECB方式加密 616263 是abc 的ANSI 字符串,16进制数据
MrwangPro:密码学 wangzz$ openssl enc -des-ecb -K 616263 -nosalt -in abc.txt -out msg1.bin
MrwangPro:密码学 wangzz$ xxd msg1.bin
00000000: d1f8 0876 7a80 0184 bbc2 3203 f334 210e  ...vz.....2..4!.
00000010: bb70 0dfa 0725 a619 d74a 241c f348 76d0  .p...%...J$..Hv.
00000020: 86ae 214a ff02 4f0d 1d8e 208f c43c a517  ..!J..O... ..<..
00000030: cbda 2959 e3d7 0466 22c8 28b9 905f 1701  ..)Y...f".(.._..
00000040: 8fbe 8928 46d7 0620 c835 cec6 991c 08a2  ...(F.. .5......
00000050: b8db 0794 f9ed eca9                      ........

加密前:
00000000
11111111
22222222
00000000
11111111
22222222
00000000
11111111
92222222

MrwangPro:密码学 wangzz$ openssl enc -des-ecb -K 616263 -nosalt -in abc.txt -out msg2.bin
MrwangPro:密码学 wangzz$ xxd msg2.bin
00000000: d1f8 0876 7a80 0184 bbc2 3203 f334 210e  ...vz.....2..4!.
00000010: bb70 0dfa 0725 a619 d74a 241c f348 76d0  .p...%...J$..Hv.
00000020: 86ae 214a ff02 4f0d 1d8e 208f c43c a517  ..!J..O... ..<..
00000030: cbda 2959 e3d7 0466 22c8 28b9 905f 1701  ..)Y...f".(.._..
00000040: 8fbe 8928 46d7 0620 11ad 36c4 cc59 170d  ...(F.. ..6..Y..
00000050: b8db 0794 f9ed eca9                      ........

加密前:
00000000
11111111
22222222
00000000
11111111
22222222
00000000
11111111
92222222

//采用CBC方式加密 -0102030405060708是向量iv
MrwangPro:密码学 wangzz$ openssl enc -des-cbc -iv 0102030405060708 -K 616263 -nosalt -in abc.txt -out msg3.bin
MrwangPro:密码学 wangzz$ xxd msg3.bin
00000000: ca73 66ec 42d1 fcc3 2c47 faca 95d3 7f10  .sf.B...,G......
00000010: 16b8 a242 77f6 beaa 7c02 a026 c8cc 1971  ...Bw...|..&...q
00000020: 99fc d42e c360 bdaa fa48 9748 60ff 7775  .....`...H.H`.wu
00000030: f81c 9368 c92c c082 c612 27f2 d510 90c7  ...h.,....'.....
00000040: 9bdd 0b55 2bd9 86cd 76b5 e115 a2c0 d1f0  ...U+...v.......
00000050: d347 652e 60db 7cb5                      .Ge.`.|.

加密前:
00000000
11111111
22222222
00000000
11111111
22222222
00000000
11111111
98222222

MrwangPro:密码学 wangzz$ openssl enc -des-cbc -iv 0102030405060708 -K 616263 -nosalt -in abc.txt -out msg4.bin
MrwangPro:密码学 wangzz$ xxd msg4.bin
00000000: ca73 66ec 42d1 fcc3 2c47 faca 95d3 7f10  .sf.B...,G......
00000010: 16b8 a242 77f6 beaa 7c02 a026 c8cc 1971  ...Bw...|..&...q
00000020: 99fc d42e c360 bdaa fa48 9748 60ff 7775  .....`...H.H`.wu
00000030: f81c 9368 c92c c082 c612 27f2 d510 90c7  ...h.,....'.....
00000040: 9bdd 0b55 2bd9 86cd b5a1 29c2 7828 6823  ...U+.....).x(h#
00000050: d8b3 d522 be2b f860   
MrwangPro:密码学 wangzz$ vim ss.txt -> 填入 wangzenghan 
MrwangPro:密码学 wangzz$ xxd ss.txt
00000000: 7761 6e67 7a65 6e67 6861 6e0a          wangzenghan.

通过上面的例子,可以如果采用CBC的加密方式,在改变内容后,后面的加密内容都会随之改变。而不是采用ECB的加密方式时那样,加密出来的内容只改变了之前被加密内容的部分。所以,CBC的加密方式安全性更高

非对称加密,以RSA为例

1 创建私钥

MrwangPro:密码学 wangzz$ openssl genrsa -out private.pem 512
Generating RSA private key, 512 bit long modulus
....++++++++++++
......++++++++++++
e is 65537 (0x10001)

2 根据私钥,生成公钥

MrwangPro:密码学 wangzz$ openssl rsa -in private.pem -out public.pem -pubout
writing RSA key

执行步骤1和2,生成的 私钥、公钥如下

//打印私钥
MrwangPro:密码学 wangzz$ cat private.pem
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAM3sq2rHQm3EcttlFWmAHIDvHoEgDOYNd7na6X5xqzwwcK3GbX47
4kfNt/XpfeEcYXG6q36k7NQ/j7U0w6Vweo0CAwEAAQJAK6gPN1YDKU5Ko2Ssw28b
m+IV734h/evBBC/67ImgqeOhqsS2rwntNSxkDT2vbnz+pjjwxVSGMu7/nymJ/jSO
gQIhAPB0G7N8CFJpkF85IppAZw8ewTDzAkHfFk4VpaCfVSe9AiEA2z0M1SKU6pLZ
3V5x03J9b3NMx3vzO0Yi6GfIdG5iIxECIQDtyjUW06M7NPA0a0FgAC6rEkQFj1x9
Hgxl3hx0cv14lQIgWg3xv3q9AL3hj/TDfL+Cls0dtLNNbgOZQy2eQXU2CoECICxE
xPLnHh0AVnkkj/1prTpKXOh+R3aTP3NhSCaP7ZTH
-----END RSA PRIVATE KEY-----
//打印公钥
MrwangPro:密码学 wangzz$ cat public.pem
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM3sq2rHQm3EcttlFWmAHIDvHoEgDOYN
d7na6X5xqzwwcK3GbX474kfNt/XpfeEcYXG6q36k7NQ/j7U0w6Vweo0CAwEAAQ==
-----END PUBLIC KEY-----

3 查看私钥的内容

执行

MrwangPro:密码学 wangzz$ openssl rsa -in private.pem -text -out private.txt
writing RSA key
MrwangPro:密码学 wangzz$ open private.txt

private.txt 展示内容如下:

Private-Key: (512 bit)
modulus:
    00:cd:ec:ab:6a:c7:42:6d:c4:72:db:65:15:69:80:
    1c:80:ef:1e:81:20:0c:e6:0d:77:b9:da:e9:7e:71:
    ab:3c:30:70:ad:c6:6d:7e:3b:e2:47:cd:b7:f5:e9:
    7d:e1:1c:61:71:ba:ab:7e:a4:ec:d4:3f:8f:b5:34:
    c3:a5:70:7a:8d
publicExponent: 65537 (0x10001)
privateExponent:
    2b:a8:0f:37:56:03:29:4e:4a:a3:64:ac:c3:6f:1b:
    9b:e2:15:ef:7e:21:fd:eb:c1:04:2f:fa:ec:89:a0:
    a9:e3:a1:aa:c4:b6:af:09:ed:35:2c:64:0d:3d:af:
    6e:7c:fe:a6:38:f0:c5:54:86:32:ee:ff:9f:29:89:
    fe:34:8e:81
prime1:
    00:f0:74:1b:b3:7c:08:52:69:90:5f:39:22:9a:40:
    67:0f:1e:c1:30:f3:02:41:df:16:4e:15:a5:a0:9f:
    55:27:bd
prime2:
    00:db:3d:0c:d5:22:94:ea:92:d9:dd:5e:71:d3:72:
    7d:6f:73:4c:c7:7b:f3:3b:46:22:e8:67:c8:74:6e:
    62:23:11
exponent1:
    00:ed:ca:35:16:d3:a3:3b:34:f0:34:6b:41:60:00:
    2e:ab:12:44:05:8f:5c:7d:1e:0c:65:de:1c:74:72:
    fd:78:95
exponent2:
    5a:0d:f1:bf:7a:bd:00:bd:e1:8f:f4:c3:7c:bf:82:
    96:cd:1d:b4:b3:4d:6e:03:99:43:2d:9e:41:75:36:
    0a:81
coefficient:
    2c:44:c4:f2:e7:1e:1d:00:56:79:24:8f:fd:69:ad:
    3a:4a:5c:e8:7e:47:76:93:3f:73:61:48:26:8f:ed:
    94:c7
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAM3sq2rHQm3EcttlFWmAHIDvHoEgDOYNd7na6X5xqzwwcK3GbX47
4kfNt/XpfeEcYXG6q36k7NQ/j7U0w6Vweo0CAwEAAQJAK6gPN1YDKU5Ko2Ssw28b
m+IV734h/evBBC/67ImgqeOhqsS2rwntNSxkDT2vbnz+pjjwxVSGMu7/nymJ/jSO
gQIhAPB0G7N8CFJpkF85IppAZw8ewTDzAkHfFk4VpaCfVSe9AiEA2z0M1SKU6pLZ
3V5x03J9b3NMx3vzO0Yi6GfIdG5iIxECIQDtyjUW06M7NPA0a0FgAC6rEkQFj1x9
Hgxl3hx0cv14lQIgWg3xv3q9AL3hj/TDfL+Cls0dtLNNbgOZQy2eQXU2CoECICxE
xPLnHh0AVnkkj/1prTpKXOh+R3aTP3NhSCaP7ZTH
-----END RSA PRIVATE KEY-----

4 请求生成证书

MrwangPro:密码学 wangzz$ openssl req -new -key private.pem -out rsacert.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:CN
State or Province Name (full name) []:Shandong
Locality Name (eg, city) []:Qingdao
Organization Name (eg, company) []:Wangzz
Organizational Unit Name (eg, section) []:Wangzz
Common Name (eg, fully qualified host name) []:zengzhan.wang
Email Address []:1609429926@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:19921010
MrwangPro:密码学 wangzz$ ls
private.pem private.txt public.pem  rsacert.csr

6 生成crt证书

MrwangPro:密码学 wangzz$ openssl x509 -req -days 3650 -in rsacert.csr -signkey private.pem -out rsacert.crt
Signature ok
subject=/C=CN/ST=Shandong/L=Qingdao/O=Wangzz/OU=Wangzz/CN=zengzhan.wang/emailAddress=1609429926@qq.com
Getting Private key
MrwangPro:密码学 wangzz$ cat rsacert.crt
-----BEGIN CERTIFICATE-----
MIICEDCCAboCCQD+M80V3Cnj1DANBgkqhkiG9w0BAQUFADCBjjELMAkGA1UEBhMC
Q04xETAPBgNVBAgMCFNoYW5kb25nMRAwDgYDVQQHDAdRaW5nZGFvMQ8wDQYDVQQK
DAZXYW5nenoxDzANBgNVBAsMBldhbmd6ejEWMBQGA1UEAwwNemVuZ3poYW4ud2Fu
ZzEgMB4GCSqGSIb3DQEJARYRMTYwOTQyOTkyNkBxcS5jb20wHhcNMTgxMDI1MDg1
ODEzWhcNMjgxMDIyMDg1ODEzWjCBjjELMAkGA1UEBhMCQ04xETAPBgNVBAgMCFNo
YW5kb25nMRAwDgYDVQQHDAdRaW5nZGFvMQ8wDQYDVQQKDAZXYW5nenoxDzANBgNV
BAsMBldhbmd6ejEWMBQGA1UEAwwNemVuZ3poYW4ud2FuZzEgMB4GCSqGSIb3DQEJ
ARYRMTYwOTQyOTkyNkBxcS5jb20wXDANBgkqhkiG9w0BAQEFAANLADBIAkEAzeyr
asdCbcRy22UVaYAcgO8egSAM5g13udrpfnGrPDBwrcZtfjviR8239el94Rxhcbqr
fqTs1D+PtTTDpXB6jQIDAQABMA0GCSqGSIb3DQEBBQUAA0EAKqr6DTZo3MRiv6tE
c1TKpUpqA5pgh92loDXUjrNSenqhjR7PiF2OBJABSvFlI+YC7gkUN6pUxnF+R2Ks
v1p1cw==
-----END CERTIFICATE-----

7 转换格式:

MrwangPro:密码学 wangzz$ openssl x509 -outform der -in rsacert.crt -out rsacert.der
MrwangPro:密码学 wangzz$ ls
private.pem private.txt public.pem  rsacert.crt rsacert.csr rsacert.der

8 生成p12文件

MrwangPro:密码学 wangzz$ openssl pkcs12 -export -out p.p12 -inkey private.pem -in rsacert.crt
Enter Export Password:<19921010>
Verifying - Enter Export Password:

测试

总结,非对称加密的每次加密的结果(密文)都不同。

其他

指纹识别

基础要求:iPhone 5s+ && iOS 8+

注意:
指纹和登录的区别

指纹:代表你是这个设备的主人;
登录:代表你是这个账号的主人;

代码实例:

//需要导入的头文件
#import <LocalAuthentication/LocalAuthentication.h>

/*  指纹识别的错误信息
 LAErrorAuthenticationFailed    --- 指纹无法识别
 LAErrorUserCancel              --- 用户点击了"取消"
 LAErrorUserFallback            --- 用户点击了"输入密码"
 LAErrorSystemCancel            --- 系统取消,比如激活了其他应用(用户)
 LAErrorPasscodeNotSet          --- 因为设备上没有设置密码
 LAErrorTouchIDNotAvailable     --- 设备没有Touch ID
 LAErrorTouchIDNotEnrolled      --- 因为用户没有输入指纹
 LAErrorTouchIDLockout          --- 多次输入错误,密码锁定
 LAErrorAppCancel               --- 比如电话进来,用户不可控的
 */

- (void)applyFingerprintIdentificationTechnology {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        LAContext *context = [LAContext new];
        //判断设备是否支持指纹识别
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            //请输入指纹识别
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请允许指纹识别" reply:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:@"指纹识别成功,可以做好多事了" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                        [ac addAction:confirm];
                        [self.window.rootViewController presentViewController:ac animated:YES completion:nil];
                        ac = nil;
                    });
                }
                if (error.code != -2) {
                    NSLog(@"error:%@",error);
                }
            }];
        }
    }else {
        NSLog(@"不支持");
    }
}

感谢 hank

上一篇下一篇

猜你喜欢

热点阅读