2017-04-26 2 views
4

Azure Schlüssel Gewölbes sendet e und n Parameter, die RSA-Modul ist gleich (n ) und RSA öffentlichen Exponenten (e ). Es ist einfach, mit Bibliotheken von Drittanbietern zu erstellen, aber der Ausgabewert wäre ohne Modul und Exponent nicht gültig.öffentlichen RSA-Schlüsselerzeugung - Swift

Ich muss RSA öffentlichen Schlüssel dieser beiden Teile erstellen. Dann muss ich RSA OAEP erstellen. Hat jemand ein ähnliches Problem festgestellt? Irgendeine Idee, wie man das erreicht?

Ich habe versucht, SCZ-BasicEncodingRules-iOS zu verwenden, aber es gibt falschen Code aus.

Hier Code:

let moduleString: String = "mK-g0TLMqtefmosgBSTQi3dWh8h-rn4lQA8sQgNs_Gkf5TvgKWtYGJ4jRGUU-eK2bmyAAomVUojYBBlRYBkTRekm99DlD9T6U9yI3v11pZVl3yQgVXBEkiTZug3Inn_IAOGPQ3Q5OT6fEj1sRRxmMv93CQukQguSKuU4v2tmElgvyhg_eKIQbNx0JRCI4-1Z5GazxNjtwk7tWcA6PAbV0zZe2AaW0TlYVil_U8NckgHBguBoTHqVAbzb-MNa-HRa4QlBmdemcSaiDr5GikoOdmQ1-Lu6koqUkepx16pgqPvVw3o_NuXAZCS37c7bfgtXWTJcAIjiQaEyRcIV2bXsUQ" 

    let exponent: String = "AQAB" 

    let moduleData: NSData! = (moduleString 
     as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! 
    let expData: NSData! = (exponent 
     as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! 
    let arrayaaa: NSArray = [moduleData, expData] 
    let key: NSData = arrayaaa.berData() 

Antwort

2

Also, ich habe eine Lösung gefunden zu haben. Leider SCZ-BasicEncodingRules-iOS hat falsche Decodierungsalgorithmus für iOS 8 und höher. Es gibt Schlüssel mit falschem Präfix aus. Wenn Sie mit dem gleichen Problem konfrontiert sind, hier ist eine Lösung, die mir geholfen hat, aber es ist Objective-C Code. Original source:

+ (NSData *)generateRSAPublicKeyWithModulus:(NSData*)modulus exponent:(NSData*)exponent 
{ 
    const uint8_t DEFAULT_EXPONENT[] = {0x01, 0x00, 0x01,}; //default: 65537 
    const uint8_t UNSIGNED_FLAG_FOR_BYTE = 0x81; 
    const uint8_t UNSIGNED_FLAG_FOR_BYTE2 = 0x82; 
    const uint8_t UNSIGNED_FLAG_FOR_BIGNUM = 0x00; 
    const uint8_t SEQUENCE_TAG = 0x30; 
    const uint8_t INTEGER_TAG = 0x02; 

    uint8_t* modulusBytes = (uint8_t*)[modulus bytes]; 
    uint8_t* exponentBytes = (uint8_t*)(exponent == nil ? DEFAULT_EXPONENT : [exponent bytes]); 

    //(1) calculate lengths 
    //- length of modulus 
    int lenMod = (int)[modulus length]; 
    if(modulusBytes[0] >= 0x80) 
     lenMod ++; //place for UNSIGNED_FLAG_FOR_BIGNUM 
    int lenModHeader = 2 + (lenMod >= 0x80 ? 1 : 0) + (lenMod >= 0x0100 ? 1 : 0); 
    //- length of exponent 
    int lenExp = exponent == nil ? sizeof(DEFAULT_EXPONENT) : (int)[exponent length]; 
    int lenExpHeader = 2; 
    //- length of body 
    int lenBody = lenModHeader + lenMod + lenExpHeader + lenExp; 
    //- length of total 
    int lenTotal = 2 + (lenBody >= 0x80 ? 1 : 0) + (lenBody >= 0x0100 ? 1 : 0) + lenBody; 

    int index = 0; 
    uint8_t* byteBuffer = malloc(sizeof(uint8_t) * lenTotal); 
    memset(byteBuffer, 0x00, sizeof(uint8_t) * lenTotal); 

    //(2) fill up byte buffer 
    //- sequence tag 
    byteBuffer[index ++] = SEQUENCE_TAG; 
    //- total length 
    if(lenBody >= 0x80) 
     byteBuffer[index ++] = (lenBody >= 0x0100 ? UNSIGNED_FLAG_FOR_BYTE2 : UNSIGNED_FLAG_FOR_BYTE); 
    if(lenBody >= 0x0100) 
    { 
     byteBuffer[index ++] = (uint8_t)(lenBody/0x0100); 
     byteBuffer[index ++] = lenBody % 0x0100; 
    } 
    else 
     byteBuffer[index ++] = lenBody; 
    //- integer tag 
    byteBuffer[index ++] = INTEGER_TAG; 
    //- modulus length 
    if(lenMod >= 0x80) 
     byteBuffer[index ++] = (lenMod >= 0x0100 ? UNSIGNED_FLAG_FOR_BYTE2 : UNSIGNED_FLAG_FOR_BYTE); 
    if(lenMod >= 0x0100) 
    { 
     byteBuffer[index ++] = (int)(lenMod/0x0100); 
     byteBuffer[index ++] = lenMod % 0x0100; 
    } 
    else 
     byteBuffer[index ++] = lenMod; 
    //- modulus value 
    if(modulusBytes[0] >= 0x80) 
     byteBuffer[index ++] = UNSIGNED_FLAG_FOR_BIGNUM; 
    memcpy(byteBuffer + index, modulusBytes, sizeof(uint8_t) * [modulus length]); 
    index += [modulus length]; 
    //- exponent length 
    byteBuffer[index ++] = INTEGER_TAG; 
    byteBuffer[index ++] = lenExp; 
    //- exponent value 
    memcpy(byteBuffer + index, exponentBytes, sizeof(uint8_t) * lenExp); 
    index += lenExp; 

    if(index != lenTotal) 
     NSLog(@"lengths mismatch: index = %d, lenTotal = %d", index, lenTotal); 

    NSMutableData* buffer = [NSMutableData dataWithBytes:byteBuffer length:lenTotal]; 
    free(byteBuffer); 

    return buffer; 
} 

Dieser Algorithmus passt mit Standard-Java-KeyFactory Generation Klasse.

+0

Ich habe das versucht. Ich habe versucht, Daten zu drucken, ich habe 140 Bytes. Wie kann ich meinen String damit verschlüsseln? Kannst du mich dabei unterstützen? –

+0

Ich bekomme immer noch falsche Daten! Funktioniert es noch auf iOS11 und höher? – Rudi

Verwandte Themen