Apple API 并没有完全遵循标准,无法与其他平台很好地配合使用。在 iOS 上导出 EC 公钥不包含固定的标头标识符。因此,导入函数还期望缺少标头。这意味着您必须删除 Android 在导入 Android EC 密钥时创建的标头。
这是一个便携式示例:
CFMutableDataRef mutableData = CFDataCreateMutable(kCFAllocatorDefault, 0);
if (mutableData)
{
//Fixed schema header
const UInt8 headerBytes[] = { 0x30,0x59,0x30,0x13,0x06,0x07,0x2a,0x86,
0x48,0xce,0x3d,0x02,0x01,0x06,0x08,0x2a,
0x86,0x48,0xce,0x3d,0x03,0x01,0x07,0x03,
0x42,0x00 };
const CFIndex headerSize = sizeof(headerBytes);
//Uncomment for exporting (such as via SecKeyCopyExternalRepresentation)
//CFDataAppendBytes(mutableData, headerBytes, headerSize);
//CFDataAppendBytes(mutableData, CFDataGetBytePtr(iosKeyData), CFDataGetLength(iosKeyData));
//For importing Android key data
CFDataAppendBytes(mutableData, CFDataGetBytePtr(androidKeyData), CFDataGetLength(androidKeyData));
CFDataDeleteBytes(mutableData, CFRangeMake(0, headerSize));
//Use the mutableData here (SecKeyCreateWithData)
CFRelease(mutableData);
}
斯威夫特版本:
let mutableData = CFDataCreateMutable(kCFAllocatorDefault, CFIndex(0))
if mutableData != nil
{
//Fixed schema header
//var headerBytes = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00] as [UInt8]
let headerSize = 26
//Uncomment for exporting (such as via SecKeyCopyExternalRepresentation)
//CFDataAppendBytes(mutableData, &headerBytes, headerSize)
//CFDataAppendBytes(mutableData, CFDataGetBytePtr(iosKeyData), CFDataGetLength(iosKeyData))
//For importing Android key data
CFDataAppendBytes(mutableData, CFDataGetBytePtr(androidKeyData), CFDataGetLength(androidKeyData))
CFDataDeleteBytes(mutableData, CFRangeMake(CFIndex(0), headerSize))
//Use the mutableData here (SecKeyCreateWithData)
}