我使用模数和分量生成私钥,但是当我将其转换为SecKey类型时,会得到nil值。
这是我的代码:
let pubKey = instanceOfCustomObject.generateRSAPrivateKey(withModulus: modData! as Data, exponent: expData! as Data)
print(pubKey! as NSData)
let keyDict:[NSObject:NSObject] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits: NSNumber(value: 128),
kSecReturnPersistentRef: true as NSObject
]
let privateSecKey = SecKeyCreateWithData(pubKey! as Data as CFData, keyDict as CFDictionary, nil)
let blockSize = SecKeyGetBlockSize(pubSecKey!)
var encrypted = [UInt8](repeating: 0, count: blockSize)
var encSize = blockSize
let status = SecKeyDecrypt(privateSecKey!,
SecPadding.PKCS1, textToEncrypt,
textToEncrypt.count, &encrypted,
&encSize)
当我将kSecAttrKeyClassPrivate设置为kSecAttrKeyClassPublic并使用函数SecKeyDecrypt时,状态值为-50。