簡體   English   中英

通過openssl / c / c ++將RSA公鑰讀取為字節

[英]read RSA public key as bytes via openssl / c / c++

我正在嘗試向服務器發送2048位(256字節)的RSA公鑰。 我需要將數據作為字節流讀取。 我似乎沒有找到辦法。

BIO *memBio = BIO_new(BIO_s_mem());

ASN1_PCTX *asn1=ASN1_PCTX_new();//useless unless I know how to use it

BIO_set_flags(memBio, BIO_FLAGS_WRITE);
int ret=EVP_PKEY_print_private(memBio,rsaAppKeys,0, asn1 );

BUF_MEM *bptr;
BIO_get_mem_ptr(memBio, &bptr);
BIO_set_close(memBio, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */
BIO_free(memBio);

它在內存中以DER格式打印密鑰,十六進制字節格式化為字符串,每個字節之間有“:”和標題。 我可以解析它,但我認為必須有一個更好的方法。 在文檔中寫道,通過使用ASN1_PCTX我可以微調輸出,但我找不到它的任何文檔。

有沒有想過將密鑰作為原始字節獲取的簡單方法? 謝謝

好的......我等不及了,所以我設計了一個骯臟的黑客:

  • 首先,我生成PEM格式的密鑰
  • 然后解析它以提取ASN.1結構
  • 然后從ASN.1結構中提取2048位密鑰

至少可以說,ASN.1解析很臟。 建議歡迎

我還使用了優秀的http://lapo.it/asn1js/來探索ASN.1結構。 https://shanetully.com/2012/04/simple-public-key-encryption-with-rsa-and-openssl/以打印鍵到base64形式的字符串。

//generic method to extract data from an EVP_PKEY
//it's very UGLY. I'm ashamed especially of the ASN.1 parsing (!!)
-(NSData *)getPublicKeyBytes:(EVP_PKEY *)rsaKey{
    ///try to write bytes
    BIO *pub = BIO_new(BIO_s_mem());

    //write pub key as this format:
    //-----BEGIN RSA PUBLIC KEY-----
    //MIIBCgKCAQEA3J7MfnosapxZH9ibxm9Gz88X+ryEEk616BtXGFx3SH1T7ssjdTvv
    //pL8FRAvnmHegtNm0JsCFbEWdGzFr1F7BFYu1lj6h7JFPIhlalMMSlGsRP5dzzj8q
    //....
    //-----END RSA PUBLIC KEY-----
    //
    PEM_write_bio_RSAPublicKey(pub, rsaKey->pkey.rsa);

    size_t pub_len = BIO_pending(pub);

    char *pub_key = malloc(pub_len + 1);

    BIO_read(pub, pub_key, pub_len);
    //zero terminated string
    pub_key[pub_len] = '\0';

    //transform to nsstring
    NSString *plainKey=[[NSString alloc]initWithCString:pub_key encoding:NSASCIIStringEncoding];
    //search for header
    NSRange range=[plainKey rangeOfString:@"-----BEGIN RSA PUBLIC KEY-----"];
    if(range.location==NSNotFound){
        DLog(@"Error, RSA pub key in wrong format: %@",plainKey);
        return nil;
    }
    //strip header
    plainKey=[plainKey substringFromIndex:range.location+range.length];

    //search footer
    range=[plainKey rangeOfString:@"-----END RSA PUBLIC KEY-----"];
    if(range.location==NSNotFound){
        DLog(@"Error, RSA pub key in wrong format: %@",plainKey);
        return nil;
    }

    //strip footer
    plainKey=[plainKey substringToIndex:range.location];

    //now remove \n
    plainKey=[plainKey stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    plainKey=[plainKey stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    //DLog(@"Plain key stripped %@",plainKey);

    //now read as byte
    NSData *rsaBytes=[NSData dataWithBase64EncodedString:plainKey];
    //DLog(@"Data is %d len %@",rsaBytes.length,rsaBytes);
    //unfortunately data is in a ASN1 rame;;
    //it's a sequence of 2 element, integer and exponent (65537).
    //the key starts from byte 9, or 10.. (byte 9 is a zero).
    NSData *pubKeyBytes=[rsaBytes subdataWithRange:NSMakeRange(9, 256)];
    DLog(@"pubKeyBytes is %d len %@",pubKeyBytes.length,pubKeyBytes);

    return pubKeyBytes;
}

使用, d2i_RSA_PUBKEY()從編碼的字節流創建RSA公鑰

然后使用BN_bn2bin()函數將RSA公鑰模數轉換為大端字節流。

例如。

RSA * pubKey = d2i_RSA_PUBKEY(NULL, <der encoded byte stream pointer>, <num bytes>);
BN_bn2bin(pubKey->n, <where to put the result>);

暫無
暫無

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

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