2015-05-06 11 views
5

Sto provando a convertire una stringa p_str che rappresenta un grande numero intero a BIGNUMp utilizzando la libreria OpenSSL.Convertire un numero grande assegnato come stringa a un OpenSSL BIGNUM

#include <stdio.h> 
#include <openssl/bn.h> 

int main() 
{ 
    /* I shortened the integer */ 
    unsigned char *p_str = "82019154470699086128524248488673846867876336512717"; 

    BIGNUM *p = BN_bin2bn(p_str, sizeof(p_str), NULL); 

    BN_print_fp(stdout, p); 
    puts(""); 

    BN_free(p); 
    return 0; 
} 

compilato con:

gcc -Wall -Wextra -g -o convert convert.c -lcrypto 

Ma, quando eseguo, ottengo il seguente risultato:

3832303139313534 

risposta

8
unsigned char *p_str = "82019154470699086128524248488673846867876336512717"; 

BIGNUM *p = BN_bin2bn(p_str, sizeof(p_str), NULL); 

Usa int BN_dec2bn(BIGNUM **a, const char *str) invece.

Si utilizza quando si dispone di un array di bytes (e non di una stringa ASCII terminata NULL).

Le pagine man si trovano a BN_bin2bn(3).

Il codice corretto sarebbe simile a questa:

#include <stdio.h> 
#include <openssl/bn.h> 

int main() 
{ 
    static const 
    char p_str[] = "82019154470699086128524248488673846867876336512717"; 

    BIGNUM *p = BN_new(); 
    BN_dec2bn(&p, p_str); 

    char * number_str = BN_bn2hex(p); 
    printf("%s\n", number_str); 

    OPENSSL_free(number_str); 
    BN_free(p); 

    return 0; 
}