首页 > 解决方案 > 打印存储为 unsigned long long 数组的大整数

问题描述

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

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

此工具使用 的数组来存储 RSA (和)unsigned long long所需的大数: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;

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

我尝试将它们像字符串一样连接起来,但这似乎不正确。

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

标签: ccryptographyrsabigint

解决方案


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

按照他的解决方案,我实现了这个功能,效果很好:

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

请注意,此解决方案可能仅适用于 little-endian 系统(大多数 PC)。


推荐阅读