c/c++语言开发共享如何存储大数字?

我必须在32位电路板上用C进行RSA签名(在状态机上)。 我的内存有限,所以我不能将小数存储在矢量或类似的东西中。

最好的办法是,如果我可以存储位并轻松访问它们; 什么存储方法最好?

我做了这个:

 #if (CPU_TYPE == CPU_TYPE_32) typedef uint32_t word; #define word_length 32 typedef struct BigNumber { word words[64]; } BigNumber; #elif (CPU_TYPE == CPU_TYPE_16) typedef uint16_t word; #define word_length 16 typedef struct BigNumber { word words[128]; } BigNumber; #else #error Unsupported CPU_TYPE #endif 

这似乎很难使用。 我该如何简化它?

    您可以简单地使用OpenSSL中的BigNumber API。 您可以在此处找到完整的API。

    并且,您可以使用此代码示例作为开头:

     #include  #include  #include  int main(int argc, char *argv[]) { static const char num1[] = "18446744073709551616"; static const char num2[] = "36893488147419103232"; BIGNUM *bn1 = NULL; BIGNUM *bn2 = NULL; BN_CTX *ctx = BN_CTX_new(); BN_dec2bn(&bn1, num1); // convert the string to BIGNUM BN_dec2bn(&bn2, num2); BN_add(bn1, bn1, bn2); // bn1 = bn1 + bn2 char *result_str = BN_bn2dec(bn1); // convert the BIGNUM back to string printf("%s + %s = %sn", num1, num2, result_str); OPENSSL_free(result_str); BN_free(bn1); BN_free(bn2); BN_CTX_free(ctx); return 0; } 

    编译它:

     #> gcc -Wall -Wextra -g -o sample sample.c -lcrypto 

    执行时你应该得到这样的东西:

     18446744073709551616 + 36893488147419103232 = 55340232221128654848 

      以上就是c/c++开发分享如何存储大数字?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/562656.html

      (0)
      上一篇 2021年2月5日
      下一篇 2021年2月5日

      精彩推荐