简体   繁体   English

我试图将 EC 公钥从 android 导出到 iOS,这样做的最佳方法是什么?

[英]Im trying to export EC Public keys from android to iOS, what is the best way to do this?

I'm trying to perform an EC key exchange between iOS and Android clients of my app.我正在尝试在我的应用程序的 iOS 和 Android 客户端之间执行 EC 密钥交换。 I have successfully transported and generated them from iOS to Android.我已经成功地将它们从 iOS 传输并生成到 Android。 But I'm unable to use the keys generated in the android app in iOS.但是我无法在 iOS 中使用在 android 应用程序中生成的密钥。

I'm using the SecKeyCreateWithData method in swift to generate keys from 'Data' type, but I'm getting this error:我在 swift 中使用SecKeyCreateWithData方法从“数据”类型生成密钥,但我收到此错误:

Error Domain=NSOSStatusErrorDomain Code=-50 "EC public key creation from data failed"错误域=NSOSStatusErrorDomain 代码=-50 “从数据创建 EC 公钥失败”

I used the follwing encoding in the android client, it produces a base64 string which I process and pass as Data SecKeyCreateWithData in swift我在 android 客户端中使用了以下编码,它生成一个 base64 字符串,我将其处理并在 swift 中作为 Data SecKeyCreateWithData传递

byte [] encodedPublicKey = PubKey.getEncoded();
String b64PublicKey = Base64.encodeToString(encodedPublicKey,Base64.DEFAULT);

I'd like to generate a SecKeyRef public key, please help我想生成一个SecKeyRef公钥,请帮忙

Apple APIs don't follow the standards exactly to play nicely with other platforms. Apple API 并没有完全遵循标准来与其他平台很好地配合使用。 Exporting an EC public key on iOS doesn't include a fixed header identifier.在 iOS 上导出 EC 公钥不包含固定的标头标识符。 Therefore, the import functions also expect that the header is missing.因此,导入函数也希望标头丢失。 This means you must remove the header that Android creates when importing the Android EC key.这意味着您必须删除 Android 在导入 Android EC 密钥时创建的标头。

Here's a portable example:这是一个便携式示例:

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);
}

Swift version:迅捷版:

    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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM