Land
Land

Reputation: 189

BN_print_fp() function always output a BIGNUM object by hex in OpenSSL?

I am using OpenSSL library. I write a piece of code as following:

    BIGNUM* a;
    BIGNUM* b;
    BIGNUM* p;
    
    a = BN_new();
    b = BN_new();
    p = BN_new();
    
    BN_dec2bn(&a, "1");
    BN_dec2bn(&b, "1");
    BN_dec2bn(&p, "23");

    printf("a=");
    BN_print_fp(stdout, a);
    printf("\n");
    printf("b=");
    BN_print_fp(stdout, b);
    printf("\n");
    printf("p=");
    BN_print_fp(stdout, p);
    printf("\n");

When run it , it always output as following:

a=1
b=1
p=17

As I excepted, p should be 23. But hex(23) = 0x17, where is wrong ?

Upvotes: -1

Views: 896

Answers (1)

Shawn
Shawn

Reputation: 52509

From the documentation:

BN_print() and BN_print_fp() write the hexadecimal encoding of a, with a leading '-' for negative numbers, to the BIO or FILE fp.

So nothing's wrong; it's working as expected. If you want to print out a bignum in base 10:

char *as_str = BN_bn2dec(p);
printf("p=%s\n", as_str);
OPENSSL_free(as_str);

Upvotes: 1

Related Questions