我已经在StackOverflow上查看了其他答案,但我找不到我要找的东西(IOS11)。我打印时有一个SecKey(privateKey):
SecKeyRef算法id:1,密钥类型:RSAPrivateKey,版本:4,块大小:2048位,地址:0x1d0223f60
我试图将其转换为数据并从那里转换为Base64
let password = "1234"
let p12data = NSData(contentsOfFile: urls[0].path)!
var importResult: CFArray? = nil
let err = SecPKCS12Import(p12data as NSData,[kSecImportExportPassphrase as String: password] as NSDictionary,&importResult )
//GET IDENTITY
let identityDictionaries = importResult as! [[String:Any]]
var privateKey: SecKey?
//GET SECKEY
SecIdentityCopyPrivateKey(identityDictionaries[0][kSecImportItemIdentity as String] as! SecIdentity, &privateKey);
print(privateKey)
//Return data in PCKS1
let dataPrivate = SecKeyCopyExternalRepresentation(privateKey!, nil)
let b64Key:Data = dataPrivate as! Data
print(b64Key.base64EncodedString(options: .lineLength64Characters))
Apple文档说SecKeyCopyExternalRepresentation返回PCKS1数据(https://developer.apple.com/documentation/security/1643698-seckeycopyexternalrepresentation),但我需要PCKS8。
结果是来自PCKS1的base64,但我必须将它发送到JAVA服务器进行处理,预期格式为PCKS8的base64。
有没有办法从PCKS1转换为PCKS8然后转换为base64?
我发现这篇文章:https://blog.wingsofhermes.org/?p=42或多或少是我想要的,但它在目标c中我无法将其转换为swift