簡體   English   中英

我試圖將 EC 公鑰從 android 導出到 iOS,這樣做的最佳方法是什么?

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

我正在嘗試在我的應用程序的 iOS 和 Android 客戶端之間執行 EC 密鑰交換。 我已經成功地將它們從 iOS 傳輸並生成到 Android。 但是我無法在 iOS 中使用在 android 應用程序中生成的密鑰。

我在 swift 中使用SecKeyCreateWithData方法從“數據”類型生成密鑰,但我收到此錯誤:

錯誤域=NSOSStatusErrorDomain 代碼=-50 “從數據創建 EC 公鑰失敗”

我在 android 客戶端中使用了以下編碼,它生成一個 base64 字符串,我將其處理並在 swift 中作為 Data SecKeyCreateWithData傳遞

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

我想生成一個SecKeyRef公鑰,請幫忙

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM