0

I want to convert this hex string into raw binary file using BIGNUM's BN_bn2bin function

    BN_hex2bn(&asn1hash, "3031300D060960864801650304020105000420BC5F9353CBB9DCAE86B9F8F68C1C95856DB836ACA2E00C9319716CDF4DD0F5BA");

    char *buf = (unsigned char *)malloc(BN_num_bytes(asn1hash));

    BN_bn2bin(asn1hash, buf);
    FILE *fp;
    fp = fopen("ASn1Hash","wb+");

        fputs(buf, fp);
        fclose(fp);

But why is it that only "30 31 30 0D 06 09 60 86 48 01 65 03 04 02 01 05" is ever outputted into the file?

Deus Ex
  • 117
  • 1
  • 8
  • 1
    `fputs` is not the best tool for the job here. It operates on strings and does not handle binary data. Have a look at the byte after `05` and think how `fputs` will treat that. – kaylum Dec 23 '19 at 00:40

1 Answers1

0

thanks fixed the code

BN_hex2bn(&asn1hash, "3031300D060960864801650304020105000420BC5F9353CBB9DCAE86B9F8F68C1C95856DB836ACA2E00C9319716CDF4DD0F5BA");

    int num_bytes = BN_num_bytes(asn1hash);
    char *buf = (unsigned char *)malloc(num_bytes);

    BN_bn2bin(asn1hash, buf);
    FILE *fp;
    fp = fopen("ASn1Hash","wb+");

        fwrite(buf, 1, num_bytes, fp);
        fclose(fp);
Deus Ex
  • 117
  • 1
  • 8