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

时间:2015-12-26 01:08:34

标签: swift macos encryption foundation

我正在使用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()会返回值-2070

奇怪的是,钥匙(公共和私人)实际上是在钥匙串中创建的:

Keys generated in the keychain from the code above

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

1 个答案:

答案 0 :(得分:0)

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

private let kAsymmetricCryptoManagerApplicationTag = "com.AsymmetricCrypto.keypair"

params字典的密钥kSecAttrApplicationTag的值必须是CFDataRef,而不是String。以下行解决了问题。

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