如何存储大量数字?

编程入门 行业动态 更新时间:2024-10-13 20:15:29
本文介绍了如何存储大量数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在32位板上的C中制作RSA签名(在状态机上).我的内存有限,所以我不能在矢量或类似的东西中存储小数.

I have to make a RSA signature (on a state machine) in C on a 32bit board. I am limited on memory so I can not store decimals in a vector or something like that.

最好的办法是,我可以存储位并方便地访问它们;哪种存储方法最好?

The best thing would be if I could store bits and to have easy access to them; what storage method would be best?

我做了这个:

#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

这似乎很难使用.我该如何简化呢?

This seems hard to use. How can I simplify it?

推荐答案

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

You may simply use the BigNumber API from OpenSSL. You can find the full API here.

而且,您可以将此代码示例作为开始:

And, you can use this code sample as a start:

#include <stdio.h> #include <openssl/crypto.h> #include <openssl/bn.h> 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 = %s\n", num1, num2, result_str); OPENSSL_free(result_str); BN_free(bn1); BN_free(bn2); BN_CTX_free(ctx); return 0; }

使用以下命令进行编译:

Compile it with:

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

在执行它时,您应该得到类似这样的东西:

You should get something like this when executing it:

18446744073709551616 + 36893488147419103232 = 55340232221128654848

更多推荐

如何存储大量数字?

本文发布于:2023-10-18 15:04:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!