TestCommon.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // TestCommon.swift
  3. // xingchuangke
  4. //
  5. // Created by Virgil on 2019/6/1.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class TestCommon: NSObject {
  10. // MARK: 身份证识别
  11. ///上传图片到海科识别
  12. func uploadHaiKe() {
  13. let testKey: [UInt8] = [UInt8](CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
  14. var test: [Int8] = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
  15. let key = "abcdefghabcde05h"
  16. let dic = NSMutableDictionary()
  17. dic.setValue("12312312323", forKey: "mercNo")
  18. let dicPara = NSMutableDictionary()
  19. dicPara.setValue(dic, forKey: "data")
  20. let jsonStr = CommonJSON.dicToJson(dic: dicPara).replacingOccurrences(of: "\n", with: "")
  21. let tools = CryptorTools()
  22. tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  23. let keyNew = RSAEncryptor.encryptData(NSKeyedArchiver.archivedData(withRootObject: test), publicKeyWithContentsOfFile: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  24. //let keyNew = RSAEncryptor.encryptString(key, publicKeyWithContentsOfFile: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  25. ///Data 方式
  26. var signKey = tools.rsaEncryptStringData(key) //keyNew!.data(using: .utf8)! //
  27. let dataContent1 = jsonStr.data(using: .utf8)! as NSData
  28. let dataContent = dataContent1.aes128EncryptedData(withKey: "", iv: "mjVyGyIPWPClJJnuHtdIiA==")!
  29. // dataContent.aes
  30. //let dataContent2 = CryptorTools.aesEncryptString(jsonStr, keyData: signKey, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
  31. // signKey.append(dataContent)
  32. let base = signKey.base64EncodedData()
  33. var re = URLRequest(url: URL(string: "http://hkrt.uenpay.com/api/uhf/mercManage/getMerchantInfo")!)
  34. re.httpMethod = "POST"
  35. re.setValue("utf-8", forHTTPHeaderField: "Accept-Charset")
  36. re.setValue("application/x-json", forHTTPHeaderField: "Content-Type")
  37. re.setValue("\(base.count)", forHTTPHeaderField: "Content-Length")
  38. re.httpBody = base
  39. let manage = AFURLSessionManager()
  40. manage.responseSerializer = AFHTTPResponseSerializer()
  41. manage.uploadTask(with: re, from: base, progress: { (_) in
  42. //print(progress);
  43. }) {[weak self] (_, any, error) in
  44. if error != nil {
  45. print(error)
  46. } else {
  47. print("=====")
  48. if any != nil {
  49. let data = any as! Data
  50. let re = self!.getResultString(str: String(data: data, encoding: .utf8)!)
  51. print(re)
  52. }
  53. }
  54. }.resume()
  55. }
  56. func getResultString(str: String) -> String {
  57. let tools = CryptorTools()
  58. tools.loadPublicKey(withFilePath: Bundle.main.path(forResource: "uen", ofType: "cer")!)
  59. let bstr11 = CommonBase64Tools.base64DecodingToData(encodedString: str) //base64Decoding(encodedString: str);
  60. let data1 = bstr11.subdata(in: Range(NSRange(location: 0, length: 256))!)
  61. let data2 = bstr11.subdata(in: Range(NSRange(location: 256, length: bstr11.count - 256))!)
  62. let key = tools.rsaDecryptData(data1)
  63. let keyData = key.subdata(in: Range(NSRange(location: key.count - 16, length: 16))!)
  64. let data = CryptorTools.aesDecryptData(data2, keyData: keyData, iv: CommonBase64Tools.base64DecodingToData(encodedString: "mjVyGyIPWPClJJnuHtdIiA=="))
  65. let result = String(data: data, encoding: .utf8)!
  66. return result
  67. }
  68. }