二进制添加大型字符数组?
所以我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,您可以通过跟踪进位来自己实现加法。
In general, you can implement the addition yourself by keeping track of the carries.
您的 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 twoint64
s. That would reduce the carry problem by a large factor.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>
.如果您可以假设 SOME_BIG_NUMBER 足够小以适合 uint64_t,并且您的系统是小端字节序(几乎可以肯定是),那么您可以这样做:
或者
If you can assume that
SOME_BIG_NUMBER
is small enough to fit in auint64_t
, and that your system is little endian (which it almost certainly is), then you can just do:or