乐趣区

关于ios:智汀云盘开发指南iOS文件夹加密逻辑

1. 加密文件夹—加密过程

1. 文件夹的创立可设置私人文件夹及加密过程:

2. 拜访服务器接口,创立私人文件夹

            NetworkManager.shared.createFolder(name: name, pool_name: pool_name, partition_name: partition_name, is_encrypt: is_encrypt, pwd: pwd, confirm_pwd: confirmPwd, mode: mode, auth: members) {[weak self] _ in
                self?.showToast("保留胜利".localizedString)
                self?.navigationController?.popViewController(animated: true)

            } failureCallback: {[weak self] code, err in
                self?.hideLoading()
                self?.showToast(err)
            }


3. 创立给可拜访用户后可在该用户的文件列表内展现。

2. 加密文件夹—解密过程

1. 文件列表数据 Model 中属性 is_encrypt 为 1 时,则为加密文件或文件夹

2. 加密文件夹款式的辨别。

3. 加密文件夹进入的逻辑如下

  • tableview 点击相应代理办法,具体参考 MyFileViewController.swift
                if file.is_encrypt == 1 {// 加密文件
                // 存储的明码对象
                let key = AreaManager.shared.currentArea.scope_token + file.path
                    let pwdJsonStr:String = UserDefaults.standard.value(forKey: key) as? String ?? ""
                let pwdModel = PasswordModel.deserialize(from: pwdJsonStr)
                if pwdModel != nil {
                    // 计算时间差
                    let timeTemp = TimeTool.TimeInterval(FromTime: pwdModel!.saveTime)
                    if timeTemp > 72 {// 大于 72 小时
                        // 从新输出明码
                        pushToFolder(isNeedPwd: true, file: file)
                    }else{
                        // 无需输出明码
                        pushToFolder(isNeedPwd: false, file: file)
                    }
                }else{
                    // 须要从新输出明码
                    pushToFolder(isNeedPwd: true, file: file)
                }
            }else{
                // 无需输出明码
                pushToFolder(isNeedPwd: false, file: file)
            }
   private func pushToFolder(isNeedPwd:Bool,file:FileModel){let vc = FolderViewController()
        vc.currentPath = file.path
        vc.currentPaths = ["文件",file.name]
        vc.encrytRootFile = file
        vc.isWriteRoot = (file.write == 1)
        
        let key = AreaManager.shared.currentArea.scope_token + file.path
        if file.is_encrypt == 1 {vc.rootPasswordKey = key}else{vc.rootPasswordKey = ""}
        if isNeedPwd {
            // 存储文件夹根目录 Key
            self.tipsTestFieldAlert = TipsTestFieldAlertView.show(message: "请输出明码", sureCallback: { pwd in
                print("明码是 \(pwd)")
                NetworkManager.shared.decryptFolder(name: file.path, password: pwd) {[weak self] response in
                    guard let self = self else {return}
                    // 存储工夫和明码
                    let pwdModel = PasswordModel()
                    pwdModel.password = pwd
                    // 以后工夫
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                    pwdModel.saveTime = dateFormatter.string(from: Date())
                    UserDefaults.standard.setValue(pwdModel.toJSONString(prettyPrint:true), forKey: key)
                    
                    // 解密胜利,进入文件夹
                    let nav = BaseNavigationViewController(rootViewController: vc)
                    nav.modalPresentationStyle = .fullScreen
                    nav.transitioningDelegate = self.transitionUtil
                    self.navigationController?.present(nav, animated: true, completion: nil)
                } failureCallback: { code, err in
                    self.showToast(err)
                }
            })
        }else{
            // 无需输出明码
            let nav = BaseNavigationViewController(rootViewController: vc)
            nav.modalPresentationStyle = .fullScreen
            nav.transitioningDelegate = transitionUtil
            self.navigationController?.present(nav, animated: true, completion: nil)
        }
    }
  • 加密文件夹进入子集目录需携带最后加密的文件,不便获取门路以及明码
  • 解密过程逻辑如下:(其中规定依据本身我的项目的需要来定,本我的项目以超出 72 小时则主动生效为准则, 所以需存储首次解密的工夫)
     

              // 存储文件夹根目录 Key
              self.tipsTestFieldAlert = TipsTestFieldAlertView.show(message: "请输出明码", sureCallback: { pwd in
                  print("明码是 \(pwd)")
                  NetworkManager.shared.decryptFolder(name: file.path, password: pwd) {[weak self] response in
                      guard let self = self else {return}
                      // 存储工夫和明码
                      let pwdModel = PasswordModel()
                      pwdModel.password = pwd
                      // 以后工夫
                      let dateFormatter = DateFormatter()
                      dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                      pwdModel.saveTime = dateFormatter.string(from: Date())
                      UserDefaults.standard.setValue(pwdModel.toJSONString(prettyPrint:true), forKey: key)
                      
                      // 解密胜利,进入文件夹
                      let nav = BaseNavigationViewController(rootViewController: vc)
                      nav.modalPresentationStyle = .fullScreen
                      nav.transitioningDelegate = self.transitionUtil
                      self.navigationController?.present(nav, animated: true, completion: nil)
                  } failureCallback: { code, err in
                      self.showToast(err)
                  }
              })

退出移动版