[Swift._NSContiguousString 字节]:发送到实例的无法识别的选择器

[Swift._NSContiguousString bytes]: unrecognized selector sent to instance

我正在开发 Xcode 7.1 版 (7B91b),我正在使用 Swift 并构建一个 OSX 应用程序 (10.11.2)。我正在尝试生成并存储用于数据加密/解密的 RSA 密钥对。这是从以下位置获取的实现:'AsymmetricCrypto Github

// Constants
private let kAsymmetricCryptoManagerApplicationTag = "com.AsymmetricCrypto.keypair"
private let kAsymmetricCryptoManagerKeyType = kSecAttrKeyTypeRSA
private let kAsymmetricCryptoManagerKeySize = 2048
private let kAsymmetricCryptoManagerCypheredBufferSize = 1024
private let kAsymmetricCryptoManagerSecPadding: SecPadding = .PKCS1

func createSecureKeyPair(completion: ((success: Bool, error: AsymmetricCryptoException?) -> Void)? = nil) {
    // private key parameters
    let privateKeyParams: [String: AnyObject] = [
        kSecAttrIsPermanent as String: false,
        kSecAttrApplicationTag as String: kAsymmetricCryptoManagerApplicationTag
    ]

    // private key parameters
    let publicKeyParams: [String: AnyObject] = [
        kSecAttrIsPermanent as String: false,
        kSecAttrApplicationTag as String: kAsymmetricCryptoManagerApplicationTag
    ]

    // global parameters for our key generation
    let parameters: [String: AnyObject] = [
        kSecAttrKeyType as String:          kAsymmetricCryptoManagerKeyType,
        kSecAttrKeySizeInBits as String:    kAsymmetricCryptoManagerKeySize,
        kSecPublicKeyAttrs as String:       publicKeyParams,
        kSecPrivateKeyAttrs as String:      privateKeyParams,
    ]

    // asynchronously generate the key pair and call the completion block
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
        var pubKey, privKey: SecKeyRef?
        let status = SecKeyGeneratePair(parameters, &pubKey, &privKey)

        if status == errSecSuccess {
            dispatch_async(dispatch_get_main_queue(), { completion?(success: true, error: nil) })
        } else {
            var error = AsymmetricCryptoException.UnknownError
            switch (status) {
            case errSecDuplicateItem: error = .DuplicateFoundWhileTryingToCreateKey
            case errSecItemNotFound: error = .KeyNotFound
            case errSecAuthFailed: error = .AuthFailed
            default: break
            }
            dispatch_async(dispatch_get_main_queue(), { completion?(success: false, error: error) })
        }
    }
}

调用createSecureKeyPair()后,如下:

AsymmetricCryptoManager.sharedInstance.createSecureKeyPair({
    (success, error) -> Void in
        if (success) {
            print("keys created")
        } else {
            print("error: \(error)")
        }
    })

我收到这个错误:

2015-12-26 03:55:46.113 sectest[17165:20928431] -[Swift._NSContiguousString bytes]: unrecognized selector sent to instance 0x610000045880

当然,会返回异常 AsymmetricCryptoException.UnknownError(这意味着错误的性质未知),如果有帮助,SecKeyGeneratePair() returns 值 -2070

奇怪的是密钥(public 和私有)实际上是在钥匙串中创建的:

这是什么错误以及如何从上面的代码中得到预期的行为?

我从苹果开发者论坛上得到了问题的答案。问题在于:

private let kAsymmetricCryptoManagerApplicationTag = "com.AsymmetricCrypto.keypair"

params 字典的键 kSecAttrApplicationTag 的值需要是 CFDataRef,而不是字符串。下面一行,解决了这个问题。

private let kAsymmetricCryptoManagerApplicationTag = "com.sectest.keypair".dataUsingEncoding(NSUTF8StringEncoding)!