Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从base64字符串创建RSA公钥_Ios_Swift_Rsa - Fatal编程技术网

Ios 从base64字符串创建RSA公钥

Ios 从base64字符串创建RSA公钥,ios,swift,rsa,Ios,Swift,Rsa,我在swift 3中使用SWCrypt。我正在获取base64字符串格式的服务器公钥。我应该用服务器公钥解密这个JSON文件 { "address":"asdf", "city": "asdf", "telephone": "345435", "zipCode": "1235458", "mobile":"3453245" } 在RSA解密和加密方法中,公钥-私钥类型为数据,我尝试将服务器公钥转换为数据,但无法加密数据,并出现解码错误 这是我创建公钥和加密数据的代码。尝试加密数据时,我发现此代

我在swift 3中使用SWCrypt。我正在获取base64字符串格式的服务器公钥。我应该用服务器公钥解密这个JSON文件

{ "address":"asdf", "city": "asdf", "telephone": "345435", "zipCode": "1235458", "mobile":"3453245" }
在RSA解密和加密方法中,公钥-私钥类型为数据,我尝试将服务器公钥转换为数据,但无法加密数据,并出现解码错误

这是我创建公钥和加密数据的代码。尝试加密数据时,我发现此代码的解码错误

 @IBAction func generateKey(_ sender: Any) {

    do {

    let (privateKey, publicKey) = try CC.RSA.generateKeyPair(2048)

    let privateKeyPEM = SwKeyConvert.PrivateKey.derToPKCS1PEM(privateKey)
    let publicKeyPEM = SwKeyConvert.PublicKey.derToPKCS8PEM(publicKey)

      privatePem = privateKeyPEM
      publicPem = publicKeyPEM

      print("this is generate public pem : \(publicPem!)")


    }
    catch {

      print(error.localizedDescription)
    }

  }




  @IBAction func createDataOfServerKey(_ sender: Any) {


    var serverKey : String = self.serverKey.text!

    serverKey = serverKey.replacingOccurrences(of: "+", with: "%2d", options: .literal, range: nil)
    serverKey  = serverKey.replacingOccurrences(of: "/", with: "%2d", options: .literal, range: nil)


//    let datakey = NSData(base64Encoded: serverKey, options: .init(rawValue: 0))

    let dataKey = serverKey.data(using: .utf8)

    if let data = dataKey {

      serverKeyData = data
    }


  }


  @IBAction func encryptJson(_ sender: Any) {

    let json = jsonData.text

    let s = json!.data(using: .utf8)

    do {
    let encrypt =   try CC.RSA.encrypt(s!, derKey: serverKeyData!, tag: s!, padding: .pkcs1, digest: .md5)

      publicPem64.text = encrypt.base64EncodedString(options: [])

      print("this is encrypt json : \(publicPem64.text)")
      print(publicPem64.text.characters.count)
      view.layoutIfNeeded()
    }

    catch {

      print(error.localizedDescription)
    }



  }
这段代码正在工作并返回加密数据,然后我将此结果base64string发送到服务器,但我无法得到任何响应

@IBAction func createDataOfServerKey(_ sender: Any) {


    var serverKey : String = self.serverKey.text!


 let datakey = NSData(base64Encoded: serverKey, options: .init(rawValue: 0))


    if let data = dataKey {

      serverKeyData = data
    }


  }

此外,android使用un_wrap base64解码

为什么不在客户端和服务器之间使用HTTPS?@zaph对于支付交易,我们应该使用RSA来提高安全性。像RSA这样的非对称加密并不比像AES这样的对称加密更安全。HTTPS同时使用非对称和对称加密,数据加密采用对称加密,一般为AES。一般来说,密钥采用非对称加密,daqta采用对称加密。如果你在付款,你需要符合PCI。@zaph我们的后端团队使用RSA和https,所以我必须按照他们的要求去做。现在我不知道我在将服务器密钥转换为数据时遇到了什么问题?RSA加密方法或在我的最终base64编码中。。。