0

我查看了 StackOverflow 上的其他答案,但没有找到我想要的(IOS11)。我有一个 SecKey (privateKey),当我打印时:

SecKeyRef算法id:1,密钥类型:RSAPrivateKey,版本:4,块大小:2048位,地址:0x1d0223f60

我试图将其转换为 Data 并从那里转换为 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))

苹果文档说 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这或多或少是我想要的,但它在objective-c中,我无法将它转换为swift

4

0 回答 0