二进制添加大型字符数组?

发布于 2025-01-07 21:58:16 字数 436 浏览 1 评论 0原文

所以我有一个 16 个字符的数组定义如下。

char CBlock[16];

在我的程序中,我正在实现 CTR 模式加密(不是很重要,只是说),并且 CBlock 被初始化为随机的 128 位十六进制值。我的程序需要做的是在循环执行加密的消息块时将该块递增 1 多次。即

for (i = 0; i < SOME_BIG_NUMBER; i++){
    CBlock = CBlock + 1; /*i know this isn't legal*/
    /*do some stuff*/
}

有没有一种简单的方法来进行此添加?有没有一种方法可以简单地将 CBlock 视为一个连续的数据块?或者我是否必须使用进位等手动实现二进制(十六进制)加法。

编辑:抱歉,CBlock 声明是错误的。

so i have an array of 16 char's defined as follows..

char CBlock[16];

in my program, I'm implementing CTR mode encryption (not really important, just saying), and CBlock gets initialized to a random 128-bit hex value. What my program needs to do is increment this block by 1 a number of times while it loops over a message block doing encryption. i.e.

for (i = 0; i < SOME_BIG_NUMBER; i++){
    CBlock = CBlock + 1; /*i know this isn't legal*/
    /*do some stuff*/
}

is there an easy way to do this addition? is there a way i can simply treat CBlock as one contiguous data block? or do i have to manually implement binary (hex) addition with carry's etc..

EDIT: sorry the CBlock declaration was wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必在意 2025-01-14 21:58:16

一般来说,您可以通过跟踪进位来自己实现加法。

unsigned char CBlock[16]; /* unsigned char is assumed to be at least an 8 bit type. */
/* Increment CBlock by 1. */

int carry = 1;
int i;

for (i=0; i<16; i++) {
    int sum = CBlock[i] + carry;
    CBlock[i] = (unsigned char) (sum & 0xff);
    carry = sum >> 8;
    if (carry == 0) {
        break;
    }
}

/* if (carry > 0) { we have overflowed} */

In general, you can implement the addition yourself by keeping track of the carries.

unsigned char CBlock[16]; /* unsigned char is assumed to be at least an 8 bit type. */
/* Increment CBlock by 1. */

int carry = 1;
int i;

for (i=0; i<16; i++) {
    int sum = CBlock[i] + carry;
    CBlock[i] = (unsigned char) (sum & 0xff);
    carry = sum >> 8;
    if (carry == 0) {
        break;
    }
}

/* if (carry > 0) { we have overflowed} */
走野 2025-01-14 21:58:16

您的 C 版本是否具有本机 128 位数字 int128?否则,也许可以尝试用两个 int64 来创建 128 位块。这将大大减少携带问题。

Does your version of C have native 128 bit numbers, int128? Otherwise perhaps try making your 128 bit block from two int64s. That would reduce the carry problem by a large factor.

爱要勇敢去追 2025-01-14 21:58:16

C 没有对大于(大多数)微处理器上的机器寄存器的数据类型的内在支持。

虽然 128 位正在变得“接近”,但它仍然不是普遍支持的类型。因此,最好的选择是使用现有的“bignum”库,或者编写自己的代码。

如果您需要增加它,那么自己做并不难。对于这种特殊情况,您也许可以只使用 中的一对 uint64_t

C does not have intrinsic support for data types larger than the machine registers on (most) microprocessors.

While 128 bits is becoming "close", it's still not a type commonly supported. So your best bet is to either use an existing "bignum" library, or code your own.

If you only need to increment it, that's not too hard to do yourself. For this special case, you could perhaps just use a pair of uint64_t from <stdint.h>.

锦爱 2025-01-14 21:58:16

如果您可以假设 SOME_BIG_NUMBER 足够小以适合 uint64_t,并且您的系统是小端字节序(几乎可以肯定是),那么您可以这样做:

*(uint64_t *)&CBlock += 1;

或者

*(uint64_t *)&CBlock = i;

If you can assume that SOME_BIG_NUMBER is small enough to fit in a uint64_t, and that your system is little endian (which it almost certainly is), then you can just do:

*(uint64_t *)&CBlock += 1;

or

*(uint64_t *)&CBlock = i;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文