• <small id='cWmTg'></small><noframes id='cWmTg'>

  • <legend id='cWmTg'><style id='cWmTg'><dir id='cWmTg'><q id='cWmTg'></q></dir></style></legend><tfoot id='cWmTg'></tfoot>

          <bdo id='cWmTg'></bdo><ul id='cWmTg'></ul>
        <i id='cWmTg'><tr id='cWmTg'><dt id='cWmTg'><q id='cWmTg'><span id='cWmTg'><b id='cWmTg'><form id='cWmTg'><ins id='cWmTg'></ins><ul id='cWmTg'></ul><sub id='cWmTg'></sub></form><legend id='cWmTg'></legend><bdo id='cWmTg'><pre id='cWmTg'><center id='cWmTg'></center></pre></bdo></b><th id='cWmTg'></th></span></q></dt></tr></i><div id='cWmTg'><tfoot id='cWmTg'></tfoot><dl id='cWmTg'><fieldset id='cWmTg'></fieldset></dl></div>

        如何编码非托管&lt;SecKey&gt;到base64发送到另一台服务器?

        时间:2024-04-14

          <i id='OGoE2'><tr id='OGoE2'><dt id='OGoE2'><q id='OGoE2'><span id='OGoE2'><b id='OGoE2'><form id='OGoE2'><ins id='OGoE2'></ins><ul id='OGoE2'></ul><sub id='OGoE2'></sub></form><legend id='OGoE2'></legend><bdo id='OGoE2'><pre id='OGoE2'><center id='OGoE2'></center></pre></bdo></b><th id='OGoE2'></th></span></q></dt></tr></i><div id='OGoE2'><tfoot id='OGoE2'></tfoot><dl id='OGoE2'><fieldset id='OGoE2'></fieldset></dl></div>

          • <small id='OGoE2'></small><noframes id='OGoE2'>

            • <bdo id='OGoE2'></bdo><ul id='OGoE2'></ul>

              <tfoot id='OGoE2'></tfoot>
              <legend id='OGoE2'><style id='OGoE2'><dir id='OGoE2'><q id='OGoE2'></q></dir></style></legend>
                    <tbody id='OGoE2'></tbody>
                  本文介绍了如何编码非托管&lt;SecKey&gt;到base64发送到另一台服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用密钥对加密来验证我的应用程序和我的 PHP 服务器之间的身份.为此,我需要在我的应用中生成公钥后将其发送到服务器.

                  I'm trying to use key pair encryption to validate identity between my app and my PHP server. To do this I need to send the public key over to the server after I generate it in my app.

                  if let pubKey = NSData(base64EncodedData: publicKey, options: NSDataBase64DecodingOptions.allZeros)! {
                      println(pubKey)
                  }
                  

                  publicKey 属于 Unmanaged 类型.

                  我在上面的代码中遇到的错误是:Extra argument 'base64EncodedData' in call

                  The error I'm getting in the above code is: Extra argument 'base64EncodedData' in call

                  我该怎么做?有没有更好的办法?

                  How would I do this? Is there a better way?

                  这是生成密钥对的方式:

                  This is how the keypair is generated:

                  var publicKeyPtr, privateKeyPtr: Unmanaged<SecKey>?
                  let parameters = [
                      String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
                      String(kSecAttrKeySizeInBits): 2048
                  ]
                  let result = SecKeyGeneratePair(parameters, &publicKeyPtr, &privateKeyPtr)
                  let publicKey = publicKeyPtr!.takeRetainedValue()
                  let privateKey = privateKeyPtr!.takeRetainedValue()
                  let blockSize = SecKeyGetBlockSize(publicKey)
                  

                  编辑 2: 所以问题是 SecKey 不是 NSData,所以我的问题应该是:如何将 publicKey:SecKey 转换为 NSData?

                  Edit 2: So the issue is that SecKey is not NSData, so my question here should be: How do I convert a publicKey:SecKey to NSData?

                  推荐答案

                  好像可以暂时把key存到keychain里,然后取回来转成数据:

                  It seems that you can temporary store the key to keychain and then get it back and convert it to data:

                  func convertSecKeyToBase64(inputKey: SecKey) ->String? {
                      // First Temp add to keychain
                      let tempTag = "de.a-bundle-id.temp"
                      let addParameters :[String:AnyObject] = [
                          String(kSecClass): kSecClassKey,
                          String(kSecAttrApplicationTag): tempTag,
                          String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
                          String(kSecValueRef): inputKey,
                          String(kSecReturnData):kCFBooleanTrue
                      ]
                  
                      var keyPtr: Unmanaged<AnyObject>?
                      let result = SecItemAdd(addParameters, &keyPtr)
                      switch result {
                      case noErr:
                          let data = keyPtr!.takeRetainedValue() as! NSData
                  
                          // Remove from Keychain again:
                          SecItemDelete(addParameters)
                          let encodingParameter = NSDataBase64EncodingOptions(rawValue: 0)
                          return data.base64EncodedStringWithOptions(encodingParameter)
                  
                      case errSecDuplicateItem:
                          println("Duplicate Item")
                          SecItemDelete(addParameters)
                          return nil
                  
                      case errSecItemNotFound:
                          println("Not found!")
                          return nil
                  
                      default:
                          println("Error: (result)")
                          return nil
                      }
                  }
                  

                  这篇关于如何编码非托管&lt;SecKey&gt;到base64发送到另一台服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在Android中将base64字符串转换为图像 下一篇:如何在 ECB 模式下使用 DES 加密 Objective C 中的 NSString?

                  相关文章

                  <small id='g5AH3'></small><noframes id='g5AH3'>

                  1. <i id='g5AH3'><tr id='g5AH3'><dt id='g5AH3'><q id='g5AH3'><span id='g5AH3'><b id='g5AH3'><form id='g5AH3'><ins id='g5AH3'></ins><ul id='g5AH3'></ul><sub id='g5AH3'></sub></form><legend id='g5AH3'></legend><bdo id='g5AH3'><pre id='g5AH3'><center id='g5AH3'></center></pre></bdo></b><th id='g5AH3'></th></span></q></dt></tr></i><div id='g5AH3'><tfoot id='g5AH3'></tfoot><dl id='g5AH3'><fieldset id='g5AH3'></fieldset></dl></div>

                      <legend id='g5AH3'><style id='g5AH3'><dir id='g5AH3'><q id='g5AH3'></q></dir></style></legend>
                        <bdo id='g5AH3'></bdo><ul id='g5AH3'></ul>
                      <tfoot id='g5AH3'></tfoot>