c-打印一个存储为无符号长数组的大整数



我正在审查一个大学项目的应用程序的安全性,该应用程序使用RSA加密文件,特别是它使用这个库:https://github.com/ilansmith/rsa(请勿使用,它有严重的漏洞(。

(如果你想看一看,这些数字之间的大多数操作都是在rsa_num.c文件中实现的。(

此工具使用unsigned long long数组来存储RSA所需的大数字(ned(:

typedef struct {
u64 arr[17]; //u64 is defined as unsigned long long
int top;     //points to the last occupied slot of the array
} u1024_t;

问题是我不明白这些数字是如何以这种格式存储的。我需要的是能够以某种方式打印实数,或者至少能够从数组的组件中恢复数字。

我试着把它们像字符串一样连接起来,但似乎不对。

感谢任何能够提供帮助的人!

谢谢@Matthieu!你的评论奏效了。我需要以相反的顺序连接unsigned long long,并由于endianness而反转它们的字节。

按照他的解决方案,我实现了这个功能,它运行得很好:

void print_u1024(u1024_t number) {
int size = (number.top + 1) * sizeof(u64);
for (int i = size-1; i >= 0; i--) {
printf("%02x", ((unsigned char*)number.arr)[i]);
}
printf("n");
}

请注意,此解决方案可能仅适用于小端系统(大多数PC(。

最新更新