BIGNUM *a = NULL;
BIGNUM *b = NULL;
BIGNUM *c = NULL;
unsigned char *xychar= NULL;
int i;
unsigned char data[20];
const unsigned char testk[20] =
{
0x00, 0x01, 0x43, 0xbd, 0x74, 0x9d, 0xd4, 0x99, 0x00, 0xdb, 0xaa, 0xa1, 0xeb, 0xd2, 0xc9, 0xcb, 0xed, 0x2e, 0xbe, 0x0c
};
a = BN_new();
b = BN_new();
c = BN_new();
BN_one( a );
BN_bin2bn( testk, 9, b );
BN_bin2bn( testk, 20, c );
//a
printf("one ->dmax = %d, a:\n", a->dmax);
xychar = (unsigned char *)a->d;
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", xychar[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
memset(data, 0, 20);
BN_bn2bin( a, data );
printf("a data:\n");
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", data[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
printf("a data BN_num_bits:%d\n", BN_num_bits(a));
//b
printf("b ->dmax = %d, b:\n", b->dmax);
xychar = (unsigned char *)b->d;
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", xychar[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
memset(data, 0, 20);
BN_bn2bin( b, data );
printf("b data:\n");
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", data[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
printf("b data BN_num_bits:%d\n", BN_num_bits(b));
//c
printf("c ->dmax = %d, c:\n", c->dmax);
xychar = (unsigned char *)c->d;
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", xychar[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
memset(data, 0, 20);
BN_bn2bin( c, data );
printf("c data:\n");
for (i = 0; i < 20; i ++ )
{
printf( "%02x, ", data[i] );
if ( ( i + 1 ) % 10 == 0 )
{
printf( "\n" );
}
}
printf("c data BN_num_bits:%d\n", BN_num_bits(c));
运算上面code输出如下:
one ->dmax = 1, a:
01, 00, 00, 00, 00, 00, 00, 00, 00, 00,
00, 00, 11, 00, 00, 00, 00, 99, d4, 9d,
a data:
01, 00, 00, 00, 00, 00, 00, 00, 00, 00,
00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
a data BN_num_bits:1
b ->dmax = 3, b:
00, 99, d4, 9d, 74, bd, 43, 01, 00, 00,
00, 00, 19, 00, 00, 00, 0c, be, 2e, ed,
b data:
01, 43, bd, 74, 9d, d4, 99, 00, 00, 00,
00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
b data BN_num_bits:57
c ->dmax = 5, c:
0c, be, 2e, ed, cb, c9, d2, eb, a1, aa,
db, 00, 99, d4, 9d, 74, bd, 43, 01, 00,
c data:
01, 43, bd, 74, 9d, d4, 99, 00, db, aa,
a1, eb, d2, c9, cb, ed, 2e, be, 0c, 00,
c data BN_num_bits:145
one ->dmax = 1, a:
01, 00, 00, 00, 00, 00, 00, 00, 00, 00,
00, 00, 11, 00, 00, 00, 00, 99, d4, 9d,
a data:
01, 00, 00, 00, 00, 00, 00, 00, 00, 00,
00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
a data BN_num_bits:1
b ->dmax = 3, b:
00, 99, d4, 9d, 74, bd, 43, 01, 00, 00,
00, 00, 19, 00, 00, 00, 0c, be, 2e, ed,
b data:
01, 43, bd, 74, 9d, d4, 99, 00, 00, 00,
00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
b data BN_num_bits:57
c ->dmax = 5, c:
0c, be, 2e, ed, cb, c9, d2, eb, a1, aa,
db, 00, 99, d4, 9d, 74, bd, 43, 01, 00,
c data:
01, 43, bd, 74, 9d, d4, 99, 00, db, aa,
a1, eb, d2, c9, cb, ed, 2e, be, 0c, 00,
c data BN_num_bits:145
总之就是BN_bn2bin转换后的值是把高位0去掉的, 输出的字节长度不一定就是20个字节, 一定不能忽视。