作者:oooooo1995_395 | 来源:互联网 | 2023-05-27 17:15
我使用SecKeyGeneratePair生成了一个守护者。
var publicKeyPtr, privateKeyPtr: Unmanaged?
let publicKeyParameters: [String: AnyObject] = [
kSecAttrIsPermanent: true,
kSecAttrApplicationTag: "com.example.site.public"
]
let privateKeyParameters: [String: AnyObject] = [
kSecAttrIsPermanent: true,
kSecAttrApplicationTag: "com.example.site.private"
]
let parameters: [String: AnyObject] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 2048,
kSecPublicKeyAttrs.takeUnretainedValue() as String: publicKeyParameters,
kSecPrivateKeyAttrs.takeUnretainedValue() as String: privateKeyParameters
]
let result = SecKeyGeneratePair(parameters, &publicKeyPtr, &privateKeyPtr)
let publicKey = publicKeyPtr!.takeRetainedValue()
let privateKey = privateKeyPtr!.takeRetainedValue()
let blockSize = SecKeyGetBlockSize(publicKey)
如果我打印出publicKey,我可以看到模数,我很确定这是我所需要的:
publicKey:
但是我无法弄清楚如何导出密钥,因此可以将其发送到服务器以供使用。
根据我的理解。一个SecKey存储在钥匙串中,并且是指向它的指针,块大小是密钥在内存中的长度。因此,从理论上讲,我可以将其提取为NSData,然后将其转换为服务器可以读取的内容。从理论上讲,我认为这是行得通的,但我在实践中碰壁了。所有帮助将不胜感激。
1> rintaro..:
SecItemCopyMatching
是给你的:
var dataPtr:Unmanaged?
let query: [String:AnyObject] = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: "com.example.site.public",
kSecReturnData: kCFBooleanTrue
]
let qResult = SecItemCopyMatching(query, &dataPtr)
// error handling with `qResult` ...
let publicKeyData = dataPtr!.takeRetainedValue() as NSData
// convert to Base64 string
let base64PublicKey = publicKeyData.base64EncodedStringWithOptions(nil)
斯威夫特4:
var dataPtr:CFTypeRef?
let query: [String: Any] = [
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: "com.example.site.public",
kSecReturnData as String: true
]
let qResult = SecItemCopyMatching(query as CFDictionary, &dataPtr)
// error handling with `qResult` ...
let data = dataPtr as! Data
let base64PublicKey = data.base64EncodedString()
请注意,数据大小为270,与密钥的块大小不同。在crypto.stackexchange.com上查看此问题。