将 ECPublicKey(SecKey) 转换为 swift 中的 PEM 字符串
Convert ECPublicKey(SecKey) to PEM String in swift
我想将 ECPublicKey 转换为 PEM 格式,如下所示
"-----BEGIN PUBLIC
KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEo0WcFrFClvBE8iZ1Gdlj+mTgZoJWMNE3kDKTfRny2iaguwuSYxo+jCnXqzkR+kyFK9CR3D+pypD1sGb1BnfWAA==-----END
PUBLIC KEY-----"
- 生成了密钥类型为“ECC/ECDSA/spec256r1”的 ECPublicKey。下面是打印
<SecKeyRef curve type: kSecECCurveSecp256r1, algorithm id: 3, key
type: ECPublicKey, version: 4, block size: 256 bits, y:
B6EEBB3791933312E10BD7C3D65C950AB7E1C325BCDB57A8663EB7B1E29BFA77, x:
1D41FDAD2137316D415B117227BCC465566E56A54E7B2B9B3A4B66C15A067611,
addr: 0x7f818850f6d0>
- 据我所知,PEM 格式只是 base64 编码的字符串以及页眉和页脚。试过下面的代码,但没有成功
var error:Unmanaged<CFError>?
guard let cfdata = SecKeyCopyExternalRepresentation(publicKey, &error)
else { return }
let data:Data = cfdata as Data
let b64String = data.base64EncodedString()
在此感谢任何帮助
首先转换为 ASN.1 格式:
(请参阅 ASN.1 Decoder 中的解码示例)
let publicKeyData: CFData = ...
let ecHeader: [UInt8] = [
/* sequence */ 0x30, 0x59,
/* |-> sequence */ 0x30, 0x13,
/* |---> ecPublicKey */ 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, // (ANSI X9.62 public key type)
/* |---> prime256v1 */ 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, // (ANSI X9.62 named elliptic curve)
/* |-> bit headers */ 0x07, 0x03, 0x42, 0x00
]
var asn1 = Data()
asn1.append(Data(ecHeader))
asn1.append(publicKeyData as Data)
然后进行 Base64 编码并添加 PEM 页眉和页脚:
let encoded = asn1.base64EncodedString(options: .lineLength64Characters)
let pemString = "-----BEGIN PUBLIC KEY-----\r\n\(encoded)\r\n-----END PUBLIC KEY-----\r\n"
我想将 ECPublicKey 转换为 PEM 格式,如下所示
"-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEo0WcFrFClvBE8iZ1Gdlj+mTgZoJWMNE3kDKTfRny2iaguwuSYxo+jCnXqzkR+kyFK9CR3D+pypD1sGb1BnfWAA==-----END PUBLIC KEY-----"
- 生成了密钥类型为“ECC/ECDSA/spec256r1”的 ECPublicKey。下面是打印
<SecKeyRef curve type: kSecECCurveSecp256r1, algorithm id: 3, key type: ECPublicKey, version: 4, block size: 256 bits, y: B6EEBB3791933312E10BD7C3D65C950AB7E1C325BCDB57A8663EB7B1E29BFA77, x: 1D41FDAD2137316D415B117227BCC465566E56A54E7B2B9B3A4B66C15A067611, addr: 0x7f818850f6d0>
- 据我所知,PEM 格式只是 base64 编码的字符串以及页眉和页脚。试过下面的代码,但没有成功
var error:Unmanaged<CFError>?
guard let cfdata = SecKeyCopyExternalRepresentation(publicKey, &error)
else { return }
let data:Data = cfdata as Data
let b64String = data.base64EncodedString()
在此感谢任何帮助
首先转换为 ASN.1 格式: (请参阅 ASN.1 Decoder 中的解码示例)
let publicKeyData: CFData = ...
let ecHeader: [UInt8] = [
/* sequence */ 0x30, 0x59,
/* |-> sequence */ 0x30, 0x13,
/* |---> ecPublicKey */ 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, // (ANSI X9.62 public key type)
/* |---> prime256v1 */ 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, // (ANSI X9.62 named elliptic curve)
/* |-> bit headers */ 0x07, 0x03, 0x42, 0x00
]
var asn1 = Data()
asn1.append(Data(ecHeader))
asn1.append(publicKeyData as Data)
然后进行 Base64 编码并添加 PEM 页眉和页脚:
let encoded = asn1.base64EncodedString(options: .lineLength64Characters)
let pemString = "-----BEGIN PUBLIC KEY-----\r\n\(encoded)\r\n-----END PUBLIC KEY-----\r\n"