将 BIGNUM 转换为原始二进制文件

Convert BIGNUM into raw binary file

我想使用 BIGNUM 的 BN_bn2bin 函数将这个十六进制字符串转换成原始二进制文件

    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);

但为什么只有“30 31 30 0D 06 09 60 86 48 01 65 03 04 02 01 05”输出到文件中?

感谢修复代码

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);