如何编码非托管<SecKey>到base64发送到另一台服务器?

2024-04-14移动开发问题
6

本文介绍了如何编码非托管<SecKey>到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发送到另一台服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’"MKS
quot;No visible @interface for #39;NSData#39; declares the selector #39;base64EncodedStringquot; error in MKStoreKit(“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’MKStoreKit 中的错误)...
2024-08-12 移动开发问题
3

无法将文件从捆绑包复制到 iOS 中的文档目录
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)...
2024-04-15 移动开发问题
4

如何复制“字典"在斯威夫特?
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)...
2024-04-15 移动开发问题
10

Swift - 迭代结构对象时如何对其进行变异
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)...
2024-04-15 移动开发问题
7

如何使用 Swift 将文本复制到剪贴板/粘贴板
How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)...
2024-04-15 移动开发问题
9

Swift 无法使用类型为“([Score],Score)"的参数列表调用“find",其中 Sco
Swift Cannot invoke #39;find#39; with an argument list of type #39;([Score], Score)#39; where Score is a struct(Swift 无法使用类型为“([Score],Score)的参数列表调用“find,其中 Score 是一个结构)...
2024-04-15 移动开发问题
6